Skip to content

Instantly share code, notes, and snippets.

@sarink
Last active August 29, 2015 14:05
Show Gist options
  • Save sarink/50586ab276af81f7526c to your computer and use it in GitHub Desktop.
Save sarink/50586ab276af81f7526c to your computer and use it in GitHub Desktop.
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(["jquery"], function($) {
return factory($);
});
}
else if (typeof exports !== "undefined") {
var $ = require("jquery");
module.exports = factory($);
}
else {
factory(root.jQuery);
}
}(this, function($) {
"use strict";
// Decodes/strips all html tags (similar to `$.text()`, but preserves line breaks)
// Usage: `$(el).decodeHtmlEntities();`
$.fn.decodeHtmlEntities = (function() {
var element = document.createElement("div");
return function decodeHtmlEntities() {
var text = $(this).html();
text = text.replace(/<br>|<br\/>|<br\s\/>|<br\s>/gmi, "\n");
text = text.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, "");
text = text.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, "");
element.innerHTML = text;
text = element.textContent;
element.textContent = "";
return text;
};
}());
// Swap two elements' positions in the DOM
// Usage: `$(el).swap($someOtherEl);`
$.fn.swap = function(b) {
b = $(b)[0];
var a = this[0];
var t = a.parentNode.insertBefore(document.createTextNode(""), a);
b.parentNode.insertBefore(a, b);
t.parentNode.insertBefore(b, t);
t.parentNode.removeChild(t);
return this;
};
// Inserts an element at position `index` within `parent`
// Usage: `$(el).insertAt(2, $newParentEl);`
$.fn.insertAt = function(index, parent) {
return this.each(function() {
(index === 0) ? $(parent).prepend(this) : $(parent).children().eq(index-1).after(this);
});
};
// A simple lightweight way to enable dragging
// Usage: `$(el).drags();`
$.fn.drags = function(options) {
// Modified from: http://css-tricks.com/snippets/jquery/draggable-without-jquery-ui/
options = $.extend({handle:"", cursor:"move"}, options);
var noHandle = options.handle === "";
var $el = (noHandle) ? this : this.find(options.handle);
return $el.css("cursor", options.cursor).on("mousedown", function(event) {
event.preventDefault(); // Disables selection
var $drag = (noHandle) ? $(this).addClass("draggable") : $(this).addClass("active-handle").parent().addClass("draggable");
var zIndex = $drag.css("z-index"),
dragH = $drag.outerHeight(),
dragW = $drag.outerWidth(),
posY = $drag.offset().top + dragH - event.pageY,
posX = $drag.offset().left + dragW - event.pageX;
$drag.css("z-index", 1000).parents().on("mousemove", function(event) {
$(".draggable").offset({
top: event.pageY + posY - dragH,
left: event.pageX + posX - dragW
}).on("mouseup", function(event) {
$(this).removeClass("draggable").css("z-index", zIndex);
});
});
}).on("mouseup", function() {
(noHandle) ? $(this).removeClass("draggable") : $(this).removeClass("active-handle").parent().removeClass("draggable");
});
}
return $;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment