Advertisement
omnosis

jquery.ui.fixedsortable.js

May 25th, 2011
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. //this code is created to fix this problem: http://stackoverflow.com/questions/4299241/
  2.  
  3. (function( $, undefined ) {
  4.  
  5. $.widget("ui.fixedsortable", $.ui.sortable, {
  6.  
  7. options: $.extend({},$.ui.sortable.prototype.options,{fixed:[]}),
  8.  
  9. _create: function() {
  10. var o = this.options;
  11. this.containerCache = {};
  12. this.element.addClass("ui-sortable");
  13.  
  14. //Get the items
  15. $.ui.sortable.prototype.refresh.apply(this,arguments);
  16.  
  17. if( typeof this.options.fixed == "number") {
  18. var num = this.options.fixed
  19. this.options.fixed = [num];
  20. }
  21. else if( typeof this.options.fixed == "string" || typeof this.options.fixed == "object") {
  22. if(this.options.fixed.constructor != Array) {
  23. var selec = this.options.fixed;
  24. var temparr = [];
  25. var temp = $(this.element[0]).find(selec);
  26. var x = this;
  27.  
  28.  
  29. temp.each(function() {
  30. var i;
  31. for(i=0;i<x.items.length && x.items[i].item.get(0) != this;++i) {}
  32. if(i<x.items.length) temparr.push(i);
  33. });
  34. this.options.fixed = temparr;
  35. }
  36. }
  37.  
  38.  
  39. //Let's determine if the items are being displayed horizontally
  40. this.floating = this.items.length ? o.axis === 'x' || (/left|right/).test(this.items[0].item.css('float')) || (/inline|table-cell/).test(this.items[0].item.css('display')) : false;
  41.  
  42. //Let's determine the parent's offset
  43. this.offset = this.element.offset();
  44.  
  45. //Initialize mouse events for interaction
  46. $.ui.sortable.prototype._mouseInit.apply(this,arguments);
  47. },
  48.  
  49. _mouseCapture: function( event ) {
  50.  
  51. this._fixPrev = this._returnItems();
  52. return $.ui.sortable.prototype._mouseCapture.apply(this,arguments);
  53. },
  54.  
  55. _mouseStart: function( event ) {
  56.  
  57. for(var i=0;i<this.options.fixed.length;++i) {
  58. var num = this.options.fixed[i];
  59. var elem = this.items[num];
  60. if(event.target == elem.item.get(0)) return false;
  61. }
  62.  
  63. return $.ui.sortable.prototype._mouseStart.apply(this,arguments);
  64. },
  65.  
  66. _rearrange: function(event, i, a, hardRefresh) {
  67.  
  68. a ? a[0].appendChild(this.placeholder[0]) :
  69. i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling));
  70.  
  71. this._refix(i);
  72.  
  73.  
  74.  
  75. //Various things done here to improve the performance:
  76. // 1. we create a setTimeout, that calls refreshPositions
  77. // 2. on the instance, we have a counter variable, that get's higher after every append
  78. // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same
  79. // 4. this lets only the last addition to the timeout stack through
  80.  
  81.  
  82.  
  83. this.counter = this.counter ? ++this.counter : 1;
  84. var self = this, counter = this.counter;
  85.  
  86.  
  87. window.setTimeout(function() {
  88. if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
  89. },0);
  90.  
  91. },
  92.  
  93. _refix: function(a) {
  94. var prev = this._fixPrev;
  95. var curr = this._returnItems();
  96.  
  97. var Fixcodes = this.options.fixed;
  98.  
  99. var NoFixed = [];
  100. var Fixed = [];
  101. var Mixed = []
  102. var post = [];
  103.  
  104.  
  105. for(var i=0;i<Fixcodes.length;++i) {
  106. var fix_index = Fixcodes[i];
  107. var fix_item = prev[fix_index];
  108. var j = 0;
  109.  
  110. for(j=0;j<curr.length && curr[j].item.get(0) != fix_item.item.get(0);++j) {}
  111.  
  112. curr.splice(j,1);
  113.  
  114. Fixed.push(fix_item);
  115. }
  116.  
  117. for(var i=0;i<curr.length;++i) {
  118. if(curr[i].item.get(0) != this.currentItem.get(0)) {
  119. NoFixed.push(curr[i]);
  120. }
  121. }
  122.  
  123. var fix_count = 0;
  124. var nofix_count = 0;
  125.  
  126. for(var i=0;i<Fixed.length + NoFixed.length;++i) {
  127. if(Fixcodes.indexOf(i) >= 0) {
  128. Mixed.push(Fixed[fix_count++]);
  129. }
  130. else {
  131. Mixed.push(NoFixed[nofix_count++]);
  132. }
  133. }
  134.  
  135. var parent = this.currentItem.get(0).parentNode;
  136. var allchild = parent.children;
  137.  
  138. for(var i=0;i<Mixed.length;++i) {
  139. parent.removeChild(Mixed[i].item.get(0));
  140. parent.appendChild(Mixed[i].item.get(0));
  141. }
  142. },
  143.  
  144. _returnItems: function(event) {
  145.  
  146. this.containers = [this];
  147. var items = [];
  148. var self = this;
  149. var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]];
  150. var connectWith = $.ui.sortable.prototype._connectWith.apply;
  151.  
  152. if(connectWith) {
  153. for (var i = connectWith.length - 1; i >= 0; i--){
  154. var cur = $(connectWith[i]);
  155. for (var j = cur.length - 1; j >= 0; j--){
  156. var inst = $.data(cur[j], 'sortable');
  157. if(inst && inst != this && !inst.options.disabled) {
  158. queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]);
  159. this.containers.push(inst);
  160. }
  161. };
  162. };
  163. }
  164.  
  165. for (var i = queries.length - 1; i >= 0; i--) {
  166. var targetData = queries[i][1];
  167. var _queries = queries[i][0];
  168.  
  169. for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) {
  170. var item = $(_queries[j]);
  171.  
  172. item.data('sortable-item', targetData); // Data for target checking (mouse manager)
  173.  
  174. items.push({
  175. item: item,
  176. instance: targetData,
  177. width: 0, height: 0,
  178. left: 0, top: 0
  179. });
  180. };
  181. };
  182.  
  183. return items;
  184. },
  185.  
  186.  
  187. value: function(input) {
  188. //console.log("test");
  189. $.ui.sortable.prototype.value.apply(this,arguments);
  190. }
  191. });
  192.  
  193. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement