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.

603 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.ns('Ext.ux.form');
  8. /**
  9. * @class Ext.ux.form.MultiSelect
  10. * @extends Ext.form.Field
  11. * A control that allows selection and form submission of multiple list items.
  12. *
  13. * @history
  14. * 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
  15. * 2008-06-19 bpm Docs and demo code clean up
  16. *
  17. * @constructor
  18. * Create a new MultiSelect
  19. * @param {Object} config Configuration options
  20. * @xtype multiselect
  21. */
  22. Ext.ux.form.MultiSelect = Ext.extend(Ext.form.Field, {
  23. /**
  24. * @cfg {String} legend Wraps the object with a fieldset and specified legend.
  25. */
  26. /**
  27. * @cfg {Ext.ListView} view The {@link Ext.ListView} used to render the multiselect list.
  28. */
  29. /**
  30. * @cfg {String/Array} dragGroup The ddgroup name(s) for the MultiSelect DragZone (defaults to undefined).
  31. */
  32. /**
  33. * @cfg {String/Array} dropGroup The ddgroup name(s) for the MultiSelect DropZone (defaults to undefined).
  34. */
  35. /**
  36. * @cfg {Boolean} ddReorder Whether the items in the MultiSelect list are drag/drop reorderable (defaults to false).
  37. */
  38. ddReorder:false,
  39. /**
  40. * @cfg {Object/Array} tbar The top toolbar of the control. This can be a {@link Ext.Toolbar} object, a
  41. * toolbar config, or an array of buttons/button configs to be added to the toolbar.
  42. */
  43. /**
  44. * @cfg {String} appendOnly True if the list should only allow append drops when drag/drop is enabled
  45. * (use for lists which are sorted, defaults to false).
  46. */
  47. appendOnly:false,
  48. /**
  49. * @cfg {Number} width Width in pixels of the control (defaults to 100).
  50. */
  51. width:100,
  52. /**
  53. * @cfg {Number} height Height in pixels of the control (defaults to 100).
  54. */
  55. height:100,
  56. /**
  57. * @cfg {String/Number} displayField Name/Index of the desired display field in the dataset (defaults to 0).
  58. */
  59. displayField:0,
  60. /**
  61. * @cfg {String/Number} valueField Name/Index of the desired value field in the dataset (defaults to 1).
  62. */
  63. valueField:1,
  64. /**
  65. * @cfg {Boolean} allowBlank False to require at least one item in the list to be selected, true to allow no
  66. * selection (defaults to true).
  67. */
  68. allowBlank:true,
  69. /**
  70. * @cfg {Number} minSelections Minimum number of selections allowed (defaults to 0).
  71. */
  72. minSelections:0,
  73. /**
  74. * @cfg {Number} maxSelections Maximum number of selections allowed (defaults to Number.MAX_VALUE).
  75. */
  76. maxSelections:Number.MAX_VALUE,
  77. /**
  78. * @cfg {String} blankText Default text displayed when the control contains no items (defaults to the same value as
  79. * {@link Ext.form.TextField#blankText}.
  80. */
  81. blankText:Ext.form.TextField.prototype.blankText,
  82. /**
  83. * @cfg {String} minSelectionsText Validation message displayed when {@link #minSelections} is not met (defaults to 'Minimum {0}
  84. * item(s) required'). The {0} token will be replaced by the value of {@link #minSelections}.
  85. */
  86. minSelectionsText:'Minimum {0} item(s) required',
  87. /**
  88. * @cfg {String} maxSelectionsText Validation message displayed when {@link #maxSelections} is not met (defaults to 'Maximum {0}
  89. * item(s) allowed'). The {0} token will be replaced by the value of {@link #maxSelections}.
  90. */
  91. maxSelectionsText:'Maximum {0} item(s) allowed',
  92. /**
  93. * @cfg {String} delimiter The string used to delimit between items when set or returned as a string of values
  94. * (defaults to ',').
  95. */
  96. delimiter:',',
  97. /**
  98. * @cfg {Ext.data.Store/Array} store The data source to which this MultiSelect is bound (defaults to <tt>undefined</tt>).
  99. * Acceptable values for this property are:
  100. * <div class="mdetail-params"><ul>
  101. * <li><b>any {@link Ext.data.Store Store} subclass</b></li>
  102. * <li><b>an Array</b> : Arrays will be converted to a {@link Ext.data.ArrayStore} internally.
  103. * <div class="mdetail-params"><ul>
  104. * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
  105. * A 1-dimensional array will automatically be expanded (each array item will be the combo
  106. * {@link #valueField value} and {@link #displayField text})</div></li>
  107. * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
  108. * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
  109. * {@link #valueField value}, while the value at index 1 is assumed to be the combo {@link #displayField text}.
  110. * </div></li></ul></div></li></ul></div>
  111. */
  112. // private
  113. defaultAutoCreate : {tag: "div"},
  114. // private
  115. initComponent: function(){
  116. Ext.ux.form.MultiSelect.superclass.initComponent.call(this);
  117. if(Ext.isArray(this.store)){
  118. if (Ext.isArray(this.store[0])){
  119. this.store = new Ext.data.ArrayStore({
  120. fields: ['value','text'],
  121. data: this.store
  122. });
  123. this.valueField = 'value';
  124. }else{
  125. this.store = new Ext.data.ArrayStore({
  126. fields: ['text'],
  127. data: this.store,
  128. expandData: true
  129. });
  130. this.valueField = 'text';
  131. }
  132. this.displayField = 'text';
  133. } else {
  134. this.store = Ext.StoreMgr.lookup(this.store);
  135. }
  136. this.addEvents({
  137. 'dblclick' : true,
  138. 'click' : true,
  139. 'change' : true,
  140. 'drop' : true
  141. });
  142. },
  143. // private
  144. onRender: function(ct, position){
  145. Ext.ux.form.MultiSelect.superclass.onRender.call(this, ct, position);
  146. var fs = this.fs = new Ext.form.FieldSet({
  147. renderTo: this.el,
  148. title: this.legend,
  149. height: this.height,
  150. width: this.width,
  151. style: "padding:0;",
  152. tbar: this.tbar
  153. });
  154. fs.body.addClass('ux-mselect');
  155. this.view = new Ext.ListView({
  156. multiSelect: true,
  157. store: this.store,
  158. columns: [{ header: 'Value', width: 1, dataIndex: this.displayField }],
  159. hideHeaders: true
  160. });
  161. fs.add(this.view);
  162. this.view.on('click', this.onViewClick, this);
  163. this.view.on('beforeclick', this.onViewBeforeClick, this);
  164. this.view.on('dblclick', this.onViewDblClick, this);
  165. this.hiddenName = this.name || Ext.id();
  166. var hiddenTag = { tag: "input", type: "hidden", value: "", name: this.hiddenName };
  167. this.hiddenField = this.el.createChild(hiddenTag);
  168. this.hiddenField.dom.disabled = this.hiddenName != this.name;
  169. fs.doLayout();
  170. },
  171. // private
  172. afterRender: function(){
  173. Ext.ux.form.MultiSelect.superclass.afterRender.call(this);
  174. if (this.ddReorder && !this.dragGroup && !this.dropGroup){
  175. this.dragGroup = this.dropGroup = 'MultiselectDD-' + Ext.id();
  176. }
  177. if (this.draggable || this.dragGroup){
  178. this.dragZone = new Ext.ux.form.MultiSelect.DragZone(this, {
  179. ddGroup: this.dragGroup
  180. });
  181. }
  182. if (this.droppable || this.dropGroup){
  183. this.dropZone = new Ext.ux.form.MultiSelect.DropZone(this, {
  184. ddGroup: this.dropGroup
  185. });
  186. }
  187. },
  188. // private
  189. onViewClick: function(vw, index, node, e) {
  190. this.fireEvent('change', this, this.getValue(), this.hiddenField.dom.value);
  191. this.hiddenField.dom.value = this.getValue();
  192. this.fireEvent('click', this, e);
  193. this.validate();
  194. },
  195. // private
  196. onViewBeforeClick: function(vw, index, node, e) {
  197. if (this.disabled || this.readOnly) {
  198. return false;
  199. }
  200. },
  201. // private
  202. onViewDblClick : function(vw, index, node, e) {
  203. return this.fireEvent('dblclick', vw, index, node, e);
  204. },
  205. /**
  206. * Returns an array of data values for the selected items in the list. The values will be separated
  207. * by {@link #delimiter}.
  208. * @return {Array} value An array of string data values
  209. */
  210. getValue: function(valueField){
  211. var returnArray = [];
  212. var selectionsArray = this.view.getSelectedIndexes();
  213. if (selectionsArray.length == 0) {return '';}
  214. for (var i=0; i<selectionsArray.length; i++) {
  215. returnArray.push(this.store.getAt(selectionsArray[i]).get((valueField != null) ? valueField : this.valueField));
  216. }
  217. return returnArray.join(this.delimiter);
  218. },
  219. /**
  220. * Sets a delimited string (using {@link #delimiter}) or array of data values into the list.
  221. * @param {String/Array} values The values to set
  222. */
  223. setValue: function(values) {
  224. var index;
  225. var selections = [];
  226. this.view.clearSelections();
  227. this.hiddenField.dom.value = '';
  228. if (!values || (values == '')) { return; }
  229. if (!Ext.isArray(values)) { values = values.split(this.delimiter); }
  230. for (var i=0; i<values.length; i++) {
  231. index = this.view.store.indexOf(this.view.store.query(this.valueField,
  232. new RegExp('^' + values[i] + '$', "i")).itemAt(0));
  233. selections.push(index);
  234. }
  235. this.view.select(selections);
  236. this.hiddenField.dom.value = this.getValue();
  237. this.validate();
  238. },
  239. // inherit docs
  240. reset : function() {
  241. this.setValue('');
  242. },
  243. // inherit docs
  244. getRawValue: function(valueField) {
  245. var tmp = this.getValue(valueField);
  246. if (tmp.length) {
  247. tmp = tmp.split(this.delimiter);
  248. }
  249. else {
  250. tmp = [];
  251. }
  252. return tmp;
  253. },
  254. // inherit docs
  255. setRawValue: function(values){
  256. setValue(values);
  257. },
  258. // inherit docs
  259. validateValue : function(value){
  260. if (value.length < 1) { // if it has no value
  261. if (this.allowBlank) {
  262. this.clearInvalid();
  263. return true;
  264. } else {
  265. this.markInvalid(this.blankText);
  266. return false;
  267. }
  268. }
  269. if (value.length < this.minSelections) {
  270. this.markInvalid(String.format(this.minSelectionsText, this.minSelections));
  271. return false;
  272. }
  273. if (value.length > this.maxSelections) {
  274. this.markInvalid(String.format(this.maxSelectionsText, this.maxSelections));
  275. return false;
  276. }
  277. return true;
  278. },
  279. // inherit docs
  280. disable: function(){
  281. this.disabled = true;
  282. this.hiddenField.dom.disabled = true;
  283. this.fs.disable();
  284. },
  285. // inherit docs
  286. enable: function(){
  287. this.disabled = false;
  288. this.hiddenField.dom.disabled = false;
  289. this.fs.enable();
  290. },
  291. // inherit docs
  292. destroy: function(){
  293. Ext.destroy(this.fs, this.dragZone, this.dropZone);
  294. Ext.ux.form.MultiSelect.superclass.destroy.call(this);
  295. }
  296. });
  297. Ext.reg('multiselect', Ext.ux.form.MultiSelect);
  298. //backwards compat
  299. Ext.ux.Multiselect = Ext.ux.form.MultiSelect;
  300. Ext.ux.form.MultiSelect.DragZone = function(ms, config){
  301. this.ms = ms;
  302. this.view = ms.view;
  303. var ddGroup = config.ddGroup || 'MultiselectDD';
  304. var dd;
  305. if (Ext.isArray(ddGroup)){
  306. dd = ddGroup.shift();
  307. } else {
  308. dd = ddGroup;
  309. ddGroup = null;
  310. }
  311. Ext.ux.form.MultiSelect.DragZone.superclass.constructor.call(this, this.ms.fs.body, { containerScroll: true, ddGroup: dd });
  312. this.setDraggable(ddGroup);
  313. };
  314. Ext.extend(Ext.ux.form.MultiSelect.DragZone, Ext.dd.DragZone, {
  315. onInitDrag : function(x, y){
  316. var el = Ext.get(this.dragData.ddel.cloneNode(true));
  317. this.proxy.update(el.dom);
  318. el.setWidth(el.child('em').getWidth());
  319. this.onStartDrag(x, y);
  320. return true;
  321. },
  322. // private
  323. collectSelection: function(data) {
  324. data.repairXY = Ext.fly(this.view.getSelectedNodes()[0]).getXY();
  325. var i = 0;
  326. this.view.store.each(function(rec){
  327. if (this.view.isSelected(i)) {
  328. var n = this.view.getNode(i);
  329. var dragNode = n.cloneNode(true);
  330. dragNode.id = Ext.id();
  331. data.ddel.appendChild(dragNode);
  332. data.records.push(this.view.store.getAt(i));
  333. data.viewNodes.push(n);
  334. }
  335. i++;
  336. }, this);
  337. },
  338. // override
  339. onEndDrag: function(data, e) {
  340. var d = Ext.get(this.dragData.ddel);
  341. if (d && d.hasClass("multi-proxy")) {
  342. d.remove();
  343. }
  344. },
  345. // override
  346. getDragData: function(e){
  347. var target = this.view.findItemFromChild(e.getTarget());
  348. if(target) {
  349. if (!this.view.isSelected(target) && !e.ctrlKey && !e.shiftKey) {
  350. this.view.select(target);
  351. this.ms.setValue(this.ms.getValue());
  352. }
  353. if (this.view.getSelectionCount() == 0 || e.ctrlKey || e.shiftKey) return false;
  354. var dragData = {
  355. sourceView: this.view,
  356. viewNodes: [],
  357. records: []
  358. };
  359. if (this.view.getSelectionCount() == 1) {
  360. var i = this.view.getSelectedIndexes()[0];
  361. var n = this.view.getNode(i);
  362. dragData.viewNodes.push(dragData.ddel = n);
  363. dragData.records.push(this.view.store.getAt(i));
  364. dragData.repairXY = Ext.fly(n).getXY();
  365. } else {
  366. dragData.ddel = document.createElement('div');
  367. dragData.ddel.className = 'multi-proxy';
  368. this.collectSelection(dragData);
  369. }
  370. return dragData;
  371. }
  372. return false;
  373. },
  374. // override the default repairXY.
  375. getRepairXY : function(e){
  376. return this.dragData.repairXY;
  377. },
  378. // private
  379. setDraggable: function(ddGroup){
  380. if (!ddGroup) return;
  381. if (Ext.isArray(ddGroup)) {
  382. Ext.each(ddGroup, this.setDraggable, this);
  383. return;
  384. }
  385. this.addToGroup(ddGroup);
  386. }
  387. });
  388. Ext.ux.form.MultiSelect.DropZone = function(ms, config){
  389. this.ms = ms;
  390. this.view = ms.view;
  391. var ddGroup = config.ddGroup || 'MultiselectDD';
  392. var dd;
  393. if (Ext.isArray(ddGroup)){
  394. dd = ddGroup.shift();
  395. } else {
  396. dd = ddGroup;
  397. ddGroup = null;
  398. }
  399. Ext.ux.form.MultiSelect.DropZone.superclass.constructor.call(this, this.ms.fs.body, { containerScroll: true, ddGroup: dd });
  400. this.setDroppable(ddGroup);
  401. };
  402. Ext.extend(Ext.ux.form.MultiSelect.DropZone, Ext.dd.DropZone, {
  403. /**
  404. * Part of the Ext.dd.DropZone interface. If no target node is found, the
  405. * whole Element becomes the target, and this causes the drop gesture to append.
  406. */
  407. getTargetFromEvent : function(e) {
  408. var target = e.getTarget();
  409. return target;
  410. },
  411. // private
  412. getDropPoint : function(e, n, dd){
  413. if (n == this.ms.fs.body.dom) { return "below"; }
  414. var t = Ext.lib.Dom.getY(n), b = t + n.offsetHeight;
  415. var c = t + (b - t) / 2;
  416. var y = Ext.lib.Event.getPageY(e);
  417. if(y <= c) {
  418. return "above";
  419. }else{
  420. return "below";
  421. }
  422. },
  423. // private
  424. isValidDropPoint: function(pt, n, data) {
  425. if (!data.viewNodes || (data.viewNodes.length != 1)) {
  426. return true;
  427. }
  428. var d = data.viewNodes[0];
  429. if (d == n) {
  430. return false;
  431. }
  432. if ((pt == "below") && (n.nextSibling == d)) {
  433. return false;
  434. }
  435. if ((pt == "above") && (n.previousSibling == d)) {
  436. return false;
  437. }
  438. return true;
  439. },
  440. // override
  441. onNodeEnter : function(n, dd, e, data){
  442. return false;
  443. },
  444. // override
  445. onNodeOver : function(n, dd, e, data){
  446. var dragElClass = this.dropNotAllowed;
  447. var pt = this.getDropPoint(e, n, dd);
  448. if (this.isValidDropPoint(pt, n, data)) {
  449. if (this.ms.appendOnly) {
  450. return "x-tree-drop-ok-below";
  451. }
  452. // set the insert point style on the target node
  453. if (pt) {
  454. var targetElClass;
  455. if (pt == "above"){
  456. dragElClass = n.previousSibling ? "x-tree-drop-ok-between" : "x-tree-drop-ok-above";
  457. targetElClass = "x-view-drag-insert-above";
  458. } else {
  459. dragElClass = n.nextSibling ? "x-tree-drop-ok-between" : "x-tree-drop-ok-below";
  460. targetElClass = "x-view-drag-insert-below";
  461. }
  462. if (this.lastInsertClass != targetElClass){
  463. Ext.fly(n).replaceClass(this.lastInsertClass, targetElClass);
  464. this.lastInsertClass = targetElClass;
  465. }
  466. }
  467. }
  468. return dragElClass;
  469. },
  470. // private
  471. onNodeOut : function(n, dd, e, data){
  472. this.removeDropIndicators(n);
  473. },
  474. // private
  475. onNodeDrop : function(n, dd, e, data){
  476. if (this.ms.fireEvent("drop", this, n, dd, e, data) === false) {
  477. return false;
  478. }
  479. var pt = this.getDropPoint(e, n, dd);
  480. if (n != this.ms.fs.body.dom)
  481. n = this.view.findItemFromChild(n);
  482. if(this.ms.appendOnly) {
  483. insertAt = this.view.store.getCount();
  484. } else {
  485. insertAt = n == this.ms.fs.body.dom ? this.view.store.getCount() - 1 : this.view.indexOf(n);
  486. if (pt == "below") {
  487. insertAt++;
  488. }
  489. }
  490. var dir = false;
  491. // Validate if dragging within the same MultiSelect
  492. if (data.sourceView == this.view) {
  493. // If the first element to be inserted below is the target node, remove it
  494. if (pt == "below") {
  495. if (data.viewNodes[0] == n) {
  496. data.viewNodes.shift();
  497. }
  498. } else { // If the last element to be inserted above is the target node, remove it
  499. if (data.viewNodes[data.viewNodes.length - 1] == n) {
  500. data.viewNodes.pop();
  501. }
  502. }
  503. // Nothing to drop...
  504. if (!data.viewNodes.length) {
  505. return false;
  506. }
  507. // If we are moving DOWN, then because a store.remove() takes place first,
  508. // the insertAt must be decremented.
  509. if (insertAt > this.view.store.indexOf(data.records[0])) {
  510. dir = 'down';
  511. insertAt--;
  512. }
  513. }
  514. for (var i = 0; i < data.records.length; i++) {
  515. var r = data.records[i];
  516. if (data.sourceView) {
  517. data.sourceView.store.remove(r);
  518. }
  519. this.view.store.insert(dir == 'down' ? insertAt : insertAt++, r);
  520. var si = this.view.store.sortInfo;
  521. if(si){
  522. this.view.store.sort(si.field, si.direction);
  523. }
  524. }
  525. return true;
  526. },
  527. // private
  528. removeDropIndicators : function(n){
  529. if(n){
  530. Ext.fly(n).removeClass([
  531. "x-view-drag-insert-above",
  532. "x-view-drag-insert-left",
  533. "x-view-drag-insert-right",
  534. "x-view-drag-insert-below"]);
  535. this.lastInsertClass = "_noclass";
  536. }
  537. },
  538. // private
  539. setDroppable: function(ddGroup){
  540. if (!ddGroup) return;
  541. if (Ext.isArray(ddGroup)) {
  542. Ext.each(ddGroup, this.setDroppable, this);
  543. return;
  544. }
  545. this.addToGroup(ddGroup);
  546. }
  547. });