jquery.nestable.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*!
  2. * Nestable jQuery Plugin - Copyright (c) 2012 David Bushell - http://dbushell.com/
  3. * Dual-licensed under the BSD or MIT licenses
  4. */
  5. ;(function ($, window, document, undefined) {
  6. var hasTouch = 'ontouchstart' in document;
  7. /**
  8. * Detect CSS pointer-events property
  9. * events are normally disabled on the dragging element to avoid conflicts
  10. * https://github.com/ausi/Feature-detection-technique-for-pointer-events/blob/master/modernizr-pointerevents.js
  11. */
  12. var hasPointerEvents = (function () {
  13. var el = document.createElement('div'),
  14. docEl = document.documentElement;
  15. if (!('pointerEvents' in el.style)) {
  16. return false;
  17. }
  18. el.style.pointerEvents = 'auto';
  19. el.style.pointerEvents = 'x';
  20. docEl.appendChild(el);
  21. var supports = window.getComputedStyle && window.getComputedStyle(el, '').pointerEvents === 'auto';
  22. docEl.removeChild(el);
  23. return !!supports;
  24. })();
  25. var defaults = {
  26. listNodeName: 'ol',
  27. itemNodeName: 'li',
  28. rootClass: 'dd',
  29. listClass: 'dd-list',
  30. itemClass: 'dd-item',
  31. dragClass: 'dd-dragel',
  32. handleClass: 'dd-handle',
  33. collapsedClass: 'dd-collapsed',
  34. placeClass: 'dd-placeholder',
  35. noDragClass: 'dd-nodrag',
  36. emptyClass: 'dd-empty',
  37. expandBtnHTML: '<button data-action="expand" type="button">Expand</button>',
  38. collapseBtnHTML: '<button data-action="collapse" type="button">Collapse</button>',
  39. group: 0,
  40. maxDepth: 5,
  41. threshold: 20
  42. };
  43. function Plugin(element, options) {
  44. this.w = $(document);
  45. this.el = $(element);
  46. this.options = $.extend({}, defaults, options);
  47. this.init();
  48. }
  49. Plugin.prototype = {
  50. init: function () {
  51. var list = this;
  52. list.reset();
  53. list.el.data('nestable-group', this.options.group);
  54. list.placeEl = $('<div class="' + list.options.placeClass + '"/>');
  55. $.each(this.el.find(list.options.itemNodeName), function (k, el) {
  56. list.setParent($(el));
  57. });
  58. list.el.on('click', 'button', function (e) {
  59. if (list.dragEl) {
  60. return;
  61. }
  62. var target = $(e.currentTarget),
  63. action = target.data('action'),
  64. item = target.parent(list.options.itemNodeName);
  65. if (action === 'collapse') {
  66. list.collapseItem(item);
  67. }
  68. if (action === 'expand') {
  69. list.expandItem(item);
  70. }
  71. });
  72. var onStartEvent = function (e) {
  73. var handle = $(e.target);
  74. if (!handle.hasClass(list.options.handleClass)) {
  75. if (handle.closest('.' + list.options.noDragClass).length) {
  76. return;
  77. }
  78. handle = handle.closest('.' + list.options.handleClass);
  79. }
  80. if (!handle.length || list.dragEl) {
  81. return;
  82. }
  83. list.isTouch = /^touch/.test(e.type);
  84. if (list.isTouch && e.touches.length !== 1) {
  85. return;
  86. }
  87. e.preventDefault();
  88. list.dragStart(e.touches ? e.touches[0] : e);
  89. };
  90. var onMoveEvent = function (e) {
  91. if (list.dragEl) {
  92. e.preventDefault();
  93. list.dragMove(e.touches ? e.touches[0] : e);
  94. }
  95. };
  96. var onEndEvent = function (e) {
  97. if (list.dragEl) {
  98. e.preventDefault();
  99. list.dragStop(e.touches ? e.touches[0] : e);
  100. }
  101. };
  102. if (hasTouch) {
  103. list.el[0].addEventListener('touchstart', onStartEvent, false);
  104. window.addEventListener('touchmove', onMoveEvent, false);
  105. window.addEventListener('touchend', onEndEvent, false);
  106. window.addEventListener('touchcancel', onEndEvent, false);
  107. }
  108. list.el.on('mousedown', onStartEvent);
  109. list.w.on('mousemove', onMoveEvent);
  110. list.w.on('mouseup', onEndEvent);
  111. },
  112. serialize: function () {
  113. var data,
  114. depth = 0,
  115. list = this;
  116. step = function (level, depth) {
  117. var array = [],
  118. items = level.children(list.options.itemNodeName);
  119. items.each(function () {
  120. var li = $(this),
  121. item = $.extend({}, li.data()),
  122. sub = li.children(list.options.listNodeName);
  123. if (sub.length) {
  124. item.children = step(sub, depth + 1);
  125. }
  126. array.push(item);
  127. });
  128. return array;
  129. };
  130. data = step(list.el.find(list.options.listNodeName).first(), depth);
  131. return data;
  132. },
  133. serialise: function () {
  134. return this.serialize();
  135. },
  136. reset: function () {
  137. this.mouse = {
  138. offsetX: 0,
  139. offsetY: 0,
  140. startX: 0,
  141. startY: 0,
  142. lastX: 0,
  143. lastY: 0,
  144. nowX: 0,
  145. nowY: 0,
  146. distX: 0,
  147. distY: 0,
  148. dirAx: 0,
  149. dirX: 0,
  150. dirY: 0,
  151. lastDirX: 0,
  152. lastDirY: 0,
  153. distAxX: 0,
  154. distAxY: 0
  155. };
  156. this.isTouch = false;
  157. this.moving = false;
  158. this.dragEl = null;
  159. this.dragRootEl = null;
  160. this.dragDepth = 0;
  161. this.hasNewRoot = false;
  162. this.pointEl = null;
  163. },
  164. expandItem: function (li) {
  165. li.removeClass(this.options.collapsedClass);
  166. li.children('[data-action="expand"]').hide();
  167. li.children('[data-action="collapse"]').show();
  168. li.children(this.options.listNodeName).show();
  169. },
  170. collapseItem: function (li) {
  171. var lists = li.children(this.options.listNodeName);
  172. if (lists.length) {
  173. li.addClass(this.options.collapsedClass);
  174. li.children('[data-action="collapse"]').hide();
  175. li.children('[data-action="expand"]').show();
  176. li.children(this.options.listNodeName).hide();
  177. }
  178. },
  179. expandAll: function () {
  180. var list = this;
  181. list.el.find(list.options.itemNodeName).each(function () {
  182. list.expandItem($(this));
  183. });
  184. },
  185. collapseAll: function () {
  186. var list = this;
  187. list.el.find(list.options.itemNodeName).each(function () {
  188. list.collapseItem($(this));
  189. });
  190. },
  191. setParent: function (li) {
  192. if (li.children(this.options.listNodeName).length) {
  193. li.prepend($(this.options.expandBtnHTML));
  194. li.prepend($(this.options.collapseBtnHTML));
  195. }
  196. li.children('[data-action="expand"]').hide();
  197. },
  198. unsetParent: function (li) {
  199. li.removeClass(this.options.collapsedClass);
  200. li.children('[data-action]').remove();
  201. li.children(this.options.listNodeName).remove();
  202. },
  203. dragStart: function (e) {
  204. var mouse = this.mouse,
  205. target = $(e.target),
  206. dragItem = target.closest(this.options.itemNodeName);
  207. this.placeEl.css('height', dragItem.height());
  208. mouse.offsetX = e.offsetX !== undefined ? e.offsetX : e.pageX - target.offset().left;
  209. mouse.offsetY = e.offsetY !== undefined ? e.offsetY : e.pageY - target.offset().top;
  210. mouse.startX = mouse.lastX = e.pageX;
  211. mouse.startY = mouse.lastY = e.pageY;
  212. this.dragRootEl = this.el;
  213. this.dragEl = $(document.createElement(this.options.listNodeName)).addClass(this.options.listClass + ' ' + this.options.dragClass);
  214. this.dragEl.css('width', dragItem.width());
  215. dragItem.after(this.placeEl);
  216. dragItem[0].parentNode.removeChild(dragItem[0]);
  217. dragItem.appendTo(this.dragEl);
  218. $(document.body).append(this.dragEl);
  219. this.dragEl.css({
  220. 'left': e.pageX - mouse.offsetX,
  221. 'top': e.pageY - mouse.offsetY
  222. });
  223. // total depth of dragging item
  224. var i, depth,
  225. items = this.dragEl.find(this.options.itemNodeName);
  226. for (i = 0; i < items.length; i++) {
  227. depth = $(items[i]).parents(this.options.listNodeName).length;
  228. if (depth > this.dragDepth) {
  229. this.dragDepth = depth;
  230. }
  231. }
  232. },
  233. dragStop: function (e) {
  234. var el = this.dragEl.children(this.options.itemNodeName).first();
  235. el[0].parentNode.removeChild(el[0]);
  236. this.placeEl.replaceWith(el);
  237. this.dragEl.remove();
  238. this.el.trigger('change');
  239. if (this.hasNewRoot) {
  240. this.dragRootEl.trigger('change');
  241. }
  242. this.reset();
  243. },
  244. dragMove: function (e) {
  245. var list, parent, prev, next, depth,
  246. opt = this.options,
  247. mouse = this.mouse;
  248. this.dragEl.css({
  249. 'left': e.pageX - mouse.offsetX,
  250. 'top': e.pageY - mouse.offsetY
  251. });
  252. // mouse position last events
  253. mouse.lastX = mouse.nowX;
  254. mouse.lastY = mouse.nowY;
  255. // mouse position this events
  256. mouse.nowX = e.pageX;
  257. mouse.nowY = e.pageY;
  258. // distance mouse moved between events
  259. mouse.distX = mouse.nowX - mouse.lastX;
  260. mouse.distY = mouse.nowY - mouse.lastY;
  261. // direction mouse was moving
  262. mouse.lastDirX = mouse.dirX;
  263. mouse.lastDirY = mouse.dirY;
  264. // direction mouse is now moving (on both axis)
  265. mouse.dirX = mouse.distX === 0 ? 0 : mouse.distX > 0 ? 1 : -1;
  266. mouse.dirY = mouse.distY === 0 ? 0 : mouse.distY > 0 ? 1 : -1;
  267. // axis mouse is now moving on
  268. var newAx = Math.abs(mouse.distX) > Math.abs(mouse.distY) ? 1 : 0;
  269. // do nothing on first move
  270. if (!mouse.moving) {
  271. mouse.dirAx = newAx;
  272. mouse.moving = true;
  273. return;
  274. }
  275. // calc distance moved on this axis (and direction)
  276. if (mouse.dirAx !== newAx) {
  277. mouse.distAxX = 0;
  278. mouse.distAxY = 0;
  279. } else {
  280. mouse.distAxX += Math.abs(mouse.distX);
  281. if (mouse.dirX !== 0 && mouse.dirX !== mouse.lastDirX) {
  282. mouse.distAxX = 0;
  283. }
  284. mouse.distAxY += Math.abs(mouse.distY);
  285. if (mouse.dirY !== 0 && mouse.dirY !== mouse.lastDirY) {
  286. mouse.distAxY = 0;
  287. }
  288. }
  289. mouse.dirAx = newAx;
  290. /**
  291. * move horizontal
  292. */
  293. if (mouse.dirAx && mouse.distAxX >= opt.threshold) {
  294. // reset move distance on x-axis for new phase
  295. mouse.distAxX = 0;
  296. prev = this.placeEl.prev(opt.itemNodeName);
  297. // increase horizontal level if previous sibling exists and is not collapsed
  298. if (mouse.distX > 0 && prev.length && !prev.hasClass(opt.collapsedClass)) {
  299. // cannot increase level when item above is collapsed
  300. list = prev.find(opt.listNodeName).last();
  301. // check if depth limit has reached
  302. depth = this.placeEl.parents(opt.listNodeName).length;
  303. if (depth + this.dragDepth <= opt.maxDepth) {
  304. // create new sub-level if one doesn't exist
  305. if (!list.length) {
  306. list = $('<' + opt.listNodeName + '/>').addClass(opt.listClass);
  307. list.append(this.placeEl);
  308. prev.append(list);
  309. this.setParent(prev);
  310. } else {
  311. // else append to next level up
  312. list = prev.children(opt.listNodeName).last();
  313. list.append(this.placeEl);
  314. }
  315. }
  316. }
  317. // decrease horizontal level
  318. if (mouse.distX < 0) {
  319. // we can't decrease a level if an item preceeds the current one
  320. next = this.placeEl.next(opt.itemNodeName);
  321. if (!next.length) {
  322. parent = this.placeEl.parent();
  323. this.placeEl.closest(opt.itemNodeName).after(this.placeEl);
  324. if (!parent.children().length) {
  325. this.unsetParent(parent.parent());
  326. }
  327. }
  328. }
  329. }
  330. var isEmpty = false;
  331. // find list item under cursor
  332. if (!hasPointerEvents) {
  333. this.dragEl[0].style.visibility = 'hidden';
  334. }
  335. this.pointEl = $(document.elementFromPoint(e.pageX - document.body.scrollLeft, e.pageY - (window.pageYOffset || document.documentElement.scrollTop)));
  336. if (!hasPointerEvents) {
  337. this.dragEl[0].style.visibility = 'visible';
  338. }
  339. if (this.pointEl.hasClass(opt.handleClass)) {
  340. this.pointEl = this.pointEl.parent(opt.itemNodeName);
  341. }
  342. if (this.pointEl.hasClass(opt.emptyClass)) {
  343. isEmpty = true;
  344. }
  345. else if (!this.pointEl.length || !this.pointEl.hasClass(opt.itemClass)) {
  346. return;
  347. }
  348. // find parent list of item under cursor
  349. var pointElRoot = this.pointEl.closest('.' + opt.rootClass),
  350. isNewRoot = this.dragRootEl.data('nestable-id') !== pointElRoot.data('nestable-id');
  351. /**
  352. * move vertical
  353. */
  354. if (!mouse.dirAx || isNewRoot || isEmpty) {
  355. // check if groups match if dragging over new root
  356. if (isNewRoot && opt.group !== pointElRoot.data('nestable-group')) {
  357. return;
  358. }
  359. // check depth limit
  360. depth = this.dragDepth - 1 + this.pointEl.parents(opt.listNodeName).length;
  361. if (depth > opt.maxDepth) {
  362. return;
  363. }
  364. var before = e.pageY < (this.pointEl.offset().top + this.pointEl.height() / 2);
  365. parent = this.placeEl.parent();
  366. // if empty create new list to replace empty placeholder
  367. if (isEmpty) {
  368. list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
  369. list.append(this.placeEl);
  370. this.pointEl.replaceWith(list);
  371. }
  372. else if (before) {
  373. this.pointEl.before(this.placeEl);
  374. }
  375. else {
  376. this.pointEl.after(this.placeEl);
  377. }
  378. if (!parent.children().length) {
  379. this.unsetParent(parent.parent());
  380. }
  381. if (!this.dragRootEl.find(opt.itemNodeName).length) {
  382. this.dragRootEl.append('<div class="' + opt.emptyClass + '"/>');
  383. }
  384. // parent root list has changed
  385. if (isNewRoot) {
  386. this.dragRootEl = pointElRoot;
  387. this.hasNewRoot = this.el[0] !== this.dragRootEl[0];
  388. }
  389. }
  390. }
  391. };
  392. $.fn.nestable = function (params) {
  393. var lists = this,
  394. retval = this;
  395. lists.each(function () {
  396. var plugin = $(this).data("nestable");
  397. if (!plugin) {
  398. $(this).data("nestable", new Plugin(this, params));
  399. $(this).data("nestable-id", new Date().getTime());
  400. } else {
  401. if (typeof params === 'string' && typeof plugin[params] === 'function') {
  402. retval = plugin[params]();
  403. }
  404. }
  405. });
  406. return retval || lists;
  407. };
  408. })(window.jQuery || window.Zepto, window, document);