1// move element in DOM to the specified place 2;(function($) { 3 // options 4 function moveElement(options) { 5 this.options = $.extend({ 6 place: '', 7 move: 'inside', // 'after', 'before' or 'inside' 8 setClass: false // set class for element that is moved 9 }, options); 10 11 this.init(); 12 } 13 14 // prototype 15 moveElement.prototype = { 16 init: function() { 17 if (this.options.holder) { 18 this.findElements(); 19 this.setAnchor(); 20 this.move(); 21 this.setClass(); 22 } 23 }, 24 findElements: function() { 25 this.elements = { 26 holder: $(this.options.holder), 27 place: $(this.options.place) 28 }; 29 }, 30 setAnchor: function() { // used in destroy, helps to return elements to their initial places in DOM 31 this.anchorElem = $('<span>'); 32 33 this.anchorElem 34 .attr('data-anchor-id', this.elements.holder.data('moved-id')) 35 .insertBefore(this.elements.holder) 36 .css('display', 'none'); 37 }, 38 move: function() { // move elements in DOM to the specified place 39 var self = this; 40 41 var moveOptions = { 42 'after': function() { 43 self.elements.holder.insertAfter(self.elements.place); 44 }, 45 'before': function() { 46 self.elements.holder.insertBefore(self.elements.place); 47 }, 48 'inside': function() { 49 self.elements.place.prepend(self.elements.holder); 50 } 51 }; 52 53 if (typeof moveOptions[this.options.move] !== 'undefined') { 54 moveOptions[this.options.move](); 55 } else { 56 moveOptions.inside(); 57 } 58 }, 59 setClass: function() { 60 if (this.options.setClass) { 61 this.elements.holder.addClass(this.options.setClass); 62 } 63 }, 64 destroy: function() { 65 var self = this; 66 67 self.elements.holder 68 .insertAfter(self.anchorElem) 69 .removeClass(self.options.setClass) 70 .removeData('moveElement'); 71 72 self.anchorElem.remove(); 73 74 elemArray = []; // empty array to reset data-moved-id counter 75 } 76 }; 77 78 // jquery plugin 79 $.fn.moveElement = function(opt) { 80 return this.each(function() { 81 elemDataIdentifier(this); 82 83 $(this).data('moveElement', new moveElement($.extend(opt, { holder: this }))); 84 }); 85 }; 86 87 // set data id on elems to improve code reading 88 var elemArray = []; 89 90 function elemDataIdentifier(elems) { 91 elemArray.push(elems); 92 93 $.each(elemArray, function(i, elem) { 94 if (i === elemArray.length - 1) { 95 $(elem).attr('data-moved-id', (i + 1)); 96 } 97 }); 98 } 99})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.