Skip to content

Instantly share code, notes, and snippets.

@jeffreycrow
Last active April 26, 2016 01:08
Show Gist options
  • Select an option

  • Save jeffreycrow/1c991d69cebaddd6fbaf7e17f55a2087 to your computer and use it in GitHub Desktop.

Select an option

Save jeffreycrow/1c991d69cebaddd6fbaf7e17f55a2087 to your computer and use it in GitHub Desktop.
(function(){
// the minimum version of jQuery we want
// totally arbitrary version, swiped from Resizer
var jQueryVersion = "1.7.1";
var jQueryUIVersion = "1.8.17";
/* This will be our global jQuery variable. We'll use it with
noConflict() to make sure we don't step on any existing jQuery in
a page. */
var jQSegment;
var Rearrange = {
fontObj: {},
traverse: function( element ) {
// Exclude non-body tags
if( Rearrange.isBodyTag(element) ) {
// If an element contains text (in itself, not children)
if (!$(element).text().trim().length) {
var fontSize = $(element).css('font-size');
if( !Rearrange.fontObj.hasOwnProperty(fontSize) ) {
Rearrange.fontObj[fontSize] = [];
}
Rearrange.fontObj[fontSize].push(element);
}
var children = $(element).children();
If( children.length > 0 ) {
this(children);
}
}
},
hide: function() {
console.log(Rearrange.fontObj);
},
init: function() {
/*
// These are the classes Rearrange will apply to all body elements
var classes = [];
classes.sortableClassItem = 'segmenter-sortable';
classes.sortableClassContainer = 'segmenter-sortable-container'
classes.unsortableClass = 'segmenter-unsortable';
classes.deleteClass = 'delete';
// Lets make them unique if they're already used on the page.
classes = Rearrange.getUniqueNames(classes);
*/
var bodyElem = jQSegment('body');
var childlessElements = jQSegment('body *:not(:has(*)):visible');
Rearrange.traverse(bodyElem);
Rearrange.hide();
/*
jQSegment(childlessElements).each(function() {
Rearrange.traverse(this, classes);
});
*/
//Rearrange.displayLoadedMessage();
},
displayLoadedMessage: function(element) {
/* var loadedMessage = "<div id=\"loadedMessage\" style=\"display:none;style="border-radius:8px;width:200px;height:20px;margin-right:auto;position:fixed;top:5px;left:40%;background-color:#ADD8E6;padding:8px;font-size:10pt;text-align:center;\">Page can now be rearranged.</div>";
// jQSegment('body').append(loadedMessage);
// jQSegment('#loadedMessage').fadeIn("fast").delay(5000).fadeOut("slow");
*/
},
// This function returns true if an element is not a header tag.
isBodyTag: function(element) {
switch(element.nodeName.toLowerCase()) {
case 'html':
return false;
case 'head':
return false;
case 'title':
return false;
case 'script':
return false;
case 'style':
return false;
case 'meta':
return false;
case 'link':
return false;
case 'noscript':
return false;
default:
return true;
}
},
/* This function takes in the array of class names the script will apply to
all the elements based on the page. It makes sure they do not currently exist
and if they do, it appends random integers to the end of them until they're unique. */
getUniqueNames: function(classes) {
jQSegment(classes).each(function() {
while( jQSegment("."+this.toString()).size() > 0 ) {
jQSegment(this) = this.toString() + Math.floor(Math.random()*10);
}
});
return classes;
},
};
// check prior inclusion and version for jQuery
// and load from Google if not already there
if (window.jQuery === undefined || window.jQuery.fn.jquery < jQueryVersion) {
var done = false;
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + jQueryVersion + "/jquery.min.js";
console.log(script);
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
if (jQuery.ui === undefined || jQuery.ui.version < jQueryUIVersion) {
var uiDone = false;
var uiScript = document.createElement("script");
uiScript.src = "http://ajax.googleapis.com/ajax/libs/jqueryui/" + jQueryUIVersion + "/jquery-ui.min.js";
uiScript.onload = script.onreadystatechange = function(){
if (!uiDone && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
uiDone = true;
jQSegment = jQuery.noConflict();
Rearrange.init();
}
};
document.getElementsByTagName("head")[0].appendChild(uiScript);
}
}
};
document.getElementsByTagName("head")[0].appendChild(script);
} else {
if (jQuery.ui === undefined || jQuery.ui.version < jQueryUIVersion) {
var uiDone = false;
var uiScript = document.createElement("script");
uiScript.src = "http://ajax.googleapis.com/ajax/libs/jqueryui/" + jQueryUIVersion + "/jquery-ui.min.js";
uiScript.onload = uiScript.onreadystatechange = function(){
if (!uiDone && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
uiDone = true;
jQSegment = jQuery.noConflict();
Rearrange.init();
}
};
document.getElementsByTagName("head")[0].appendChild(uiScript);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment