Skip to content

Instantly share code, notes, and snippets.

@MaxPower15
Created May 8, 2013 01:46
Show Gist options
  • Save MaxPower15/5537604 to your computer and use it in GitHub Desktop.
Save MaxPower15/5537604 to your computer and use it in GitHub Desktop.
jQuery line/word dimensions
(function($) {
var deNthWordize, mode, nthWordize;
nthWordize = function(el) {
var $escaper, $this, $tns, textNode, textNodes, _i, _len;
$this = $(el);
textNodes = $this.find(":not(iframe)").andSelf().contents().filter(function() {
return this.nodeType === 3;
});
for (_i = 0, _len = textNodes.length; _i < _len; _i++) {
textNode = textNodes[_i];
if ($.trim(textNode.textContent)) {
$(textNode).wrap("<span class='text_node'></span>");
}
}
$tns = $([]);
$escaper = $("<div>");
$this.find(".text_node:visible").each(function() {
var $tn, savedHtml, word, words, wrappedWords;
$tn = $(this);
savedHtml = $tn.html();
words = $tn.text().split(/\s+/);
wrappedWords = ((function() {
var _j, _len2, _results;
_results = [];
for (_j = 0, _len2 = words.length; _j < _len2; _j++) {
word = words[_j];
_results.push((word ? "<span class='nth_word'>" + ($escaper.text(word).html()) + "</span>" : ""));
}
return _results;
})()).join(" ");
$tn.data("saved-html", savedHtml).html(wrappedWords);
return $tns = $tns.add($tn);
});
return $tns;
};
deNthWordize = function(els) {
return $(els).each(function() {
var $tn;
$tn = $(this);
$tn.html($tn.data("saved-html"));
$tn.removeData("saved-html");
return $tn.contents().unwrap();
});
};
$.fn.nthWordDimensions = function(n) {
var $target, $textNodes, $this, result;
$this = $(this);
$textNodes = nthWordize($this);
if (n === "last") {
$target = $this.find(".nth_word:last");
} else if (n === "first") {
$target = $this.find(".nth_word:first");
} else {
$target = $this.find(".nth_word:eq(" + n + ")");
}
if ($target.length) {
result = {
top: $target.offset().top,
right: $target.offset().left + $target.width(),
bottom: $target.offset().top + $target.height(),
left: $target.offset().left,
width: $target.width(),
height: $target.height()
};
} else {
result = {
top: 0,
right: 0,
bottom: 0,
left: 0,
width: 0,
height: 0
};
}
deNthWordize($textNodes);
return result;
};
$.fn.nthLineDimensions = function(n) {
var $firstInLine, $lastInLine, $lastWord, $textNodes, $this, $words, lastX, result;
$this = $(this);
$textNodes = nthWordize($this);
$words = $this.find(".nth_word");
lastX = 1000000;
$lastWord = null;
$words.each(function() {
var $word;
$word = $(this);
if ($word.text() && ($word.offset().left < lastX || ($lastWord && $word.offset().left === lastX && $word.offset().top >= $lastWord.offset().top + $lastWord.height()) || ($lastWord.width() >= $this.width() && $lastWord.height() >= $word.height() * 2))) {
$word.addClass("first_in_line");
if ($lastWord) {
$lastWord.addClass("last_in_line");
}
lastX = $word.offset().left;
}
return $lastWord = $word;
});
$words.filter(":last").addClass("last_in_line");
if (n === "first") {
$firstInLine = $this.find(".first_in_line:first");
} else if (n === "last") {
$firstInLine = $this.find(".first_in_line:last");
} else {
$firstInLine = $this.find(".first_in_line:eq(" + n + ")");
}
if ($firstInLine.is(".last_in_line")) {
$lastInLine = $firstInLine;
} else {
$lastInLine = $firstInLine.nextAll(".last_in_line:first");
}
if ($firstInLine.length) {
result = {
top: $firstInLine.offset().top,
right: $lastInLine.offset().left + $lastInLine.width(),
bottom: $firstInLine.offset().top + $firstInLine.height(),
left: $firstInLine.offset().left,
width: $lastInLine.offset().left + $lastInLine.width() - $firstInLine.offset().left,
height: $firstInLine.height()
};
} else {
result = {
top: $this.offset().top,
right: $this.offset().left,
bottom: $this.offset().top,
left: $this.offset().left,
width: 0,
height: 0
};
}
deNthWordize($textNodes);
return result;
};
return mode = function(array) {
var el, i, maxCount, maxEl, modeMap, _ref;
if (!(array && array.length)) {
return null;
}
modeMap = {};
maxEl = array[0];
maxCount = 1;
for (i = 0, _ref = array.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
el = array[i];
if (modeMap[el]) {
modeMap[el]++;
} else {
modeMap[el] = 1;
}
if (modeMap[el] > maxCount) {
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment