You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
16 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.ux.Reorderer
  17. * @extends Object
  18. * Generic base class for handling reordering of items. This base class must be extended to provide the
  19. * actual reordering functionality - the base class just sets up events and abstract logic functions.
  20. * It will fire events and set defaults, deferring the actual reordering to a doReorder implementation.
  21. * See Ext.ux.TabReorderer for an example.
  22. */
  23. Ext.ux.Reorderer = Ext.extend(Object, {
  24. /**
  25. * @property defaults
  26. * @type Object
  27. * Object containing default values for plugin configuration details. These can be overridden when
  28. * constructing the plugin
  29. */
  30. defaults: {
  31. /**
  32. * @cfg animate
  33. * @type Boolean
  34. * If set to true, the rearranging of the toolbar items is animated
  35. */
  36. animate: true,
  37. /**
  38. * @cfg animationDuration
  39. * @type Number
  40. * The duration of the animation used to move other toolbar items out of the way
  41. */
  42. animationDuration: 0.2,
  43. /**
  44. * @cfg defaultReorderable
  45. * @type Boolean
  46. * True to make every toolbar draggable unless reorderable is specifically set to false.
  47. * This defaults to false
  48. */
  49. defaultReorderable: false
  50. },
  51. /**
  52. * Creates the plugin instance, applies defaults
  53. * @constructor
  54. * @param {Object} config Optional config object
  55. */
  56. constructor: function(config) {
  57. Ext.apply(this, config || {}, this.defaults);
  58. },
  59. /**
  60. * Initializes the plugin, stores a reference to the target
  61. * @param {Mixed} target The target component which contains the reorderable items
  62. */
  63. init: function(target) {
  64. /**
  65. * @property target
  66. * @type Ext.Component
  67. * Reference to the target component which contains the reorderable items
  68. */
  69. this.target = target;
  70. this.initEvents();
  71. var items = this.getItems(),
  72. length = items.length,
  73. i;
  74. for (i = 0; i < length; i++) {
  75. this.createIfReorderable(items[i]);
  76. }
  77. },
  78. /**
  79. * Reorders the items in the target component according to the given mapping object. Example:
  80. * this.reorder({
  81. * 1: 5,
  82. * 3: 2
  83. * });
  84. * Would move the item at index 1 to index 5, and the item at index 3 to index 2
  85. * @param {Object} mappings Object containing current item index as key and new index as property
  86. */
  87. reorder: function(mappings) {
  88. var target = this.target;
  89. if (target.fireEvent('before-reorder', mappings, target, this) !== false) {
  90. this.doReorder(mappings);
  91. target.fireEvent('reorder', mappings, target, this);
  92. }
  93. },
  94. /**
  95. * Abstract function to perform the actual reordering. This MUST be overridden in a subclass
  96. * @param {Object} mappings Mappings of the old item indexes to new item indexes
  97. */
  98. doReorder: function(paramName) {
  99. throw new Error("doReorder must be implemented in the Ext.ux.Reorderer subclass");
  100. },
  101. /**
  102. * Should create and return an Ext.dd.DD for the given item. This MUST be overridden in a subclass
  103. * @param {Mixed} item The item to create a DD for. This could be a TabPanel tab, a Toolbar button, etc
  104. * @return {Ext.dd.DD} The DD for the given item
  105. */
  106. createItemDD: function(item) {
  107. throw new Error("createItemDD must be implemented in the Ext.ux.Reorderer subclass");
  108. },
  109. /**
  110. * Sets up the given Toolbar item as a draggable
  111. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  112. */
  113. createItemDD: function(button) {
  114. var el = button.getEl(),
  115. id = el.id,
  116. tbar = this.target,
  117. me = this;
  118. button.dd = new Ext.dd.DD(el, undefined, {
  119. isTarget: false
  120. });
  121. button.dd.constrainTo(tbar.getEl());
  122. button.dd.setYConstraint(0, 0, 0);
  123. Ext.apply(button.dd, {
  124. b4StartDrag: function() {
  125. this.startPosition = el.getXY();
  126. //bump up the z index of the button being dragged but keep a reference to the original
  127. this.startZIndex = el.getStyle('zIndex');
  128. el.setStyle('zIndex', 10000);
  129. button.suspendEvents();
  130. },
  131. onDrag: function(e) {
  132. //calculate the button's index within the toolbar and its current midpoint
  133. var buttonX = el.getXY()[0],
  134. deltaX = buttonX - this.startPosition[0],
  135. items = tbar.items.items,
  136. oldIndex = items.indexOf(button),
  137. newIndex;
  138. //find which item in the toolbar the midpoint is currently over
  139. for (var index = 0; index < items.length; index++) {
  140. var item = items[index];
  141. if (item.reorderable && item.id != button.id) {
  142. //find the midpoint of the button
  143. var box = item.getEl().getBox(),
  144. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  145. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  146. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  147. if (movedLeft || movedRight) {
  148. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  149. break;
  150. }
  151. }
  152. }
  153. },
  154. /**
  155. * After the drag has been completed, make sure the button being dragged makes it back to
  156. * the correct location and resets its z index
  157. */
  158. endDrag: function() {
  159. //we need to update the cache here for cases where the button was dragged but its
  160. //position in the toolbar did not change
  161. me.updateButtonXCache();
  162. el.moveTo(me.buttonXCache[button.id], undefined, {
  163. duration: me.animationDuration,
  164. scope : this,
  165. callback: function() {
  166. button.resumeEvents();
  167. tbar.fireEvent('reordered', button, tbar);
  168. }
  169. });
  170. el.setStyle('zIndex', this.startZIndex);
  171. }
  172. });
  173. },
  174. /**
  175. * @private
  176. * Creates a DD instance for a given item if it is reorderable
  177. * @param {Mixed} item The item
  178. */
  179. createIfReorderable: function(item) {
  180. if (this.defaultReorderable && item.reorderable == undefined) {
  181. item.reorderable = true;
  182. }
  183. if (item.reorderable && !item.dd) {
  184. if (item.rendered) {
  185. this.createItemDD(item);
  186. } else {
  187. item.on('render', this.createItemDD.createDelegate(this, [item]), this, {single: true});
  188. }
  189. }
  190. },
  191. /**
  192. * Returns an array of items which will be made draggable. This defaults to the contents of this.target.items,
  193. * but can be overridden - e.g. for TabPanels
  194. * @return {Array} The array of items which will be made draggable
  195. */
  196. getItems: function() {
  197. return this.target.items.items;
  198. },
  199. /**
  200. * Adds before-reorder and reorder events to the target component
  201. */
  202. initEvents: function() {
  203. this.target.addEvents(
  204. /**
  205. * @event before-reorder
  206. * Fires before a reorder occurs. Return false to cancel
  207. * @param {Object} mappings Mappings of the old item indexes to new item indexes
  208. * @param {Mixed} component The target component
  209. * @param {Ext.ux.TabReorderer} this The plugin instance
  210. */
  211. 'before-reorder',
  212. /**
  213. * @event reorder
  214. * Fires after a reorder has occured.
  215. * @param {Object} mappings Mappings of the old item indexes to the new item indexes
  216. * @param {Mixed} component The target component
  217. * @param {Ext.ux.TabReorderer} this The plugin instance
  218. */
  219. 'reorder'
  220. );
  221. }
  222. });
  223. /**
  224. * @class Ext.ux.HBoxReorderer
  225. * @extends Ext.ux.Reorderer
  226. * Description
  227. */
  228. Ext.ux.HBoxReorderer = Ext.extend(Ext.ux.Reorderer, {
  229. /**
  230. * Initializes the plugin, decorates the container with additional functionality
  231. */
  232. init: function(container) {
  233. /**
  234. * This is used to store the correct x value of each button in the array. We need to use this
  235. * instead of the button's reported x co-ordinate because the buttons are animated when they move -
  236. * if another onDrag is fired while the button is still moving, the comparison x value will be incorrect
  237. */
  238. this.buttonXCache = {};
  239. container.on({
  240. scope: this,
  241. add : function(container, item) {
  242. this.createIfReorderable(item);
  243. }
  244. });
  245. //super sets a reference to the toolbar in this.target
  246. Ext.ux.HBoxReorderer.superclass.init.apply(this, arguments);
  247. },
  248. /**
  249. * Sets up the given Toolbar item as a draggable
  250. * @param {Mixed} button The item to make draggable (usually an Ext.Button instance)
  251. */
  252. createItemDD: function(button) {
  253. if (button.dd != undefined) {
  254. return;
  255. }
  256. var el = button.getEl(),
  257. id = el.id,
  258. me = this,
  259. tbar = me.target;
  260. button.dd = new Ext.dd.DD(el, undefined, {
  261. isTarget: false
  262. });
  263. el.applyStyles({
  264. position: 'absolute'
  265. });
  266. //if a button has a menu, it is disabled while dragging with this function
  267. var menuDisabler = function() {
  268. return false;
  269. };
  270. Ext.apply(button.dd, {
  271. b4StartDrag: function() {
  272. this.startPosition = el.getXY();
  273. //bump up the z index of the button being dragged but keep a reference to the original
  274. this.startZIndex = el.getStyle('zIndex');
  275. el.setStyle('zIndex', 10000);
  276. button.suspendEvents();
  277. if (button.menu) {
  278. button.menu.on('beforeshow', menuDisabler, me);
  279. }
  280. },
  281. startDrag: function() {
  282. this.constrainTo(tbar.getEl());
  283. this.setYConstraint(0, 0, 0);
  284. },
  285. onDrag: function(e) {
  286. //calculate the button's index within the toolbar and its current midpoint
  287. var buttonX = el.getXY()[0],
  288. deltaX = buttonX - this.startPosition[0],
  289. items = tbar.items.items,
  290. length = items.length,
  291. oldIndex = items.indexOf(button),
  292. newIndex, index, item;
  293. //find which item in the toolbar the midpoint is currently over
  294. for (index = 0; index < length; index++) {
  295. item = items[index];
  296. if (item.reorderable && item.id != button.id) {
  297. //find the midpoint of the button
  298. var box = item.getEl().getBox(),
  299. midpoint = (me.buttonXCache[item.id] || box.x) + (box.width / 2),
  300. movedLeft = oldIndex > index && deltaX < 0 && buttonX < midpoint,
  301. movedRight = oldIndex < index && deltaX > 0 && (buttonX + el.getWidth()) > midpoint;
  302. if (movedLeft || movedRight) {
  303. me[movedLeft ? 'onMovedLeft' : 'onMovedRight'](button, index, oldIndex);
  304. break;
  305. }
  306. }
  307. }
  308. },
  309. /**
  310. * After the drag has been completed, make sure the button being dragged makes it back to
  311. * the correct location and resets its z index
  312. */
  313. endDrag: function() {
  314. //we need to update the cache here for cases where the button was dragged but its
  315. //position in the toolbar did not change
  316. me.updateButtonXCache();
  317. el.moveTo(me.buttonXCache[button.id], el.getY(), {
  318. duration: me.animationDuration,
  319. scope : this,
  320. callback: function() {
  321. button.resumeEvents();
  322. if (button.menu) {
  323. button.menu.un('beforeshow', menuDisabler, me);
  324. }
  325. tbar.fireEvent('reordered', button, tbar);
  326. }
  327. });
  328. el.setStyle('zIndex', this.startZIndex);
  329. }
  330. });
  331. },
  332. onMovedLeft: function(item, newIndex, oldIndex) {
  333. var tbar = this.target,
  334. items = tbar.items.items,
  335. length = items.length,
  336. index;
  337. if (newIndex != undefined && newIndex != oldIndex) {
  338. //move the button currently under drag to its new location
  339. tbar.remove(item, false);
  340. tbar.insert(newIndex, item);
  341. //set the correct x location of each item in the toolbar
  342. this.updateButtonXCache();
  343. for (index = 0; index < length; index++) {
  344. var obj = items[index],
  345. newX = this.buttonXCache[obj.id];
  346. if (item == obj) {
  347. item.dd.startPosition[0] = newX;
  348. } else {
  349. var el = obj.getEl();
  350. el.moveTo(newX, el.getY(), {
  351. duration: this.animationDuration
  352. });
  353. }
  354. }
  355. }
  356. },
  357. onMovedRight: function(item, newIndex, oldIndex) {
  358. this.onMovedLeft.apply(this, arguments);
  359. },
  360. /**
  361. * @private
  362. * Updates the internal cache of button X locations.
  363. */
  364. updateButtonXCache: function() {
  365. var tbar = this.target,
  366. items = tbar.items,
  367. totalX = tbar.getEl().getBox(true).x;
  368. items.each(function(item) {
  369. this.buttonXCache[item.id] = totalX;
  370. totalX += item.getEl().getWidth();
  371. }, this);
  372. }
  373. });