Skip to content

Instantly share code, notes, and snippets.

@Bellfalasch
Last active August 29, 2015 13:56
Show Gist options
  • Save Bellfalasch/8803689 to your computer and use it in GitHub Desktop.
Save Bellfalasch/8803689 to your computer and use it in GitHub Desktop.
Make it possible for content editors to insert tags inside of their content to get related content found and injected in its place.
$(document).ready( function() {
/*
* This code will on load go through the entire "Rutinebeskrivelse" and find every occurance
* of keyword (in this example "#trinn") and locate the trailing numbers. This is matched to
* an unique id on a table at another location in the source code. This chunk of html is fetched
* and moved inside the "Rutinebeskrivelse".
*
* Thus, Editors can inject related content into their text - happy days =)
*
*/
// Fetch the entire html-text for our Rutine.
var rutin = document.getElementById('rutinebeskrivelse');
// Work with the clone instead (way faster)
var clone = rutin.cloneNode(true).innerHTML;
// Setting for what keyword-trigger to look for, and do a first search for it with indexOf
var keyword = '#trinn';
var index = clone.indexOf(keyword);
// Much needed stack of variables and such to play with =)
var trinntable;
var trinnlist = [];
var found = [];
var number = '';
var next_num = '';
var walker = 0;
// While we find matching keywords keep working that text baby
while ( index >= 0 ) {
// Add the first number directly in place after the keyword
walker = index + keyword.length;
number = clone.substring( walker , walker + 1 );
// This is lazy, but it works, but only for 1-2 numeric values (so trinn100 will turn into trinn10)
next_num = clone.substring( walker + 1, walker + 2 );
// trim() didn't exsist before IE9, handle this with a regexp replace
if ( !String.prototype.trim ) {
next_num = next_num.replace(/^\s+|\s+$/g, '');
} else {
next_num = next_num.trim();
}
// If the second part is numeric, concat it (isNotaNumber thinks '' is a number x_x )
if ( !isNaN(parseInt(next_num)) && next_num != '' ) {
number = number.concat(next_num);
}
// Add the found Trinn ID to our trinn array
found.push(number);
// We found a Trinn, so get the matching element on ID
theelement = document.getElementById( keyword.replace('#','').concat(number) );
if ( typeof theelement == 'object' && theelement != null ) {
// The matching ID is a correctly formed element/object, so insert all of its html in the main flow
trinntable = theelement.outerHTML;
clone = clone.replace( keyword.concat(number) , trinntable );
// Since we found a match we need to store the reference for later so we can delete it
trinnlist.push( theelement );
} else {
// We didn't find any match, so we need to remove the keyword so we will not find it again
trinntable = null;
clone = clone.replace( keyword.concat(number) , '' );
}
// Find the next index (turns -1 if end of file is reached, which aborts the loop)
index = clone.indexOf( keyword, index + keyword.length );
}
// Debug
console.log("These different Trinns were found: " + found);
// Replace the old html with the newly built one
rutin.innerHTML = clone;
// Loop through our Array of matched elements (trinn tables) and remove them since they're not used.
for (var i = 0; i < trinnlist.length; i++) {
trinnlist[i].parentNode.removeChild( trinnlist[i] );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment