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.

335 lines
13 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. /*
  8. * Note that this control will most likely remain as an example, and not as a core Ext form
  9. * control. However, the API will be changing in a future release and so should not yet be
  10. * treated as a final, stable API at this time.
  11. */
  12. /**
  13. * @class Ext.ux.form.ItemSelector
  14. * @extends Ext.form.Field
  15. * A control that allows selection of between two Ext.ux.form.MultiSelect controls.
  16. *
  17. * @history
  18. * 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
  19. *
  20. * @constructor
  21. * Create a new ItemSelector
  22. * @param {Object} config Configuration options
  23. * @xtype itemselector
  24. */
  25. Ext.ux.form.ItemSelector = Ext.extend(Ext.form.Field, {
  26. hideNavIcons:false,
  27. imagePath:"",
  28. iconUp:"up2.gif",
  29. iconDown:"down2.gif",
  30. iconLeft:"left2.gif",
  31. iconRight:"right2.gif",
  32. iconTop:"top2.gif",
  33. iconBottom:"bottom2.gif",
  34. drawUpIcon:true,
  35. drawDownIcon:true,
  36. drawLeftIcon:true,
  37. drawRightIcon:true,
  38. drawTopIcon:true,
  39. drawBotIcon:true,
  40. delimiter:',',
  41. bodyStyle:null,
  42. border:false,
  43. defaultAutoCreate:{tag: "div"},
  44. /**
  45. * @cfg {Array} multiselects An array of {@link Ext.ux.form.MultiSelect} config objects, with at least all required parameters (e.g., store)
  46. */
  47. multiselects:null,
  48. initComponent: function(){
  49. Ext.ux.form.ItemSelector.superclass.initComponent.call(this);
  50. this.addEvents({
  51. 'rowdblclick' : true,
  52. 'change' : true
  53. });
  54. },
  55. onRender: function(ct, position){
  56. Ext.ux.form.ItemSelector.superclass.onRender.call(this, ct, position);
  57. // Internal default configuration for both multiselects
  58. var msConfig = [{
  59. legend: 'Available',
  60. draggable: true,
  61. droppable: true,
  62. width: 100,
  63. height: 100
  64. },{
  65. legend: 'Selected',
  66. droppable: true,
  67. draggable: true,
  68. width: 100,
  69. height: 100
  70. }];
  71. this.fromMultiselect = new Ext.ux.form.MultiSelect(Ext.applyIf(this.multiselects[0], msConfig[0]));
  72. this.fromMultiselect.on('dblclick', this.onRowDblClick, this);
  73. this.toMultiselect = new Ext.ux.form.MultiSelect(Ext.applyIf(this.multiselects[1], msConfig[1]));
  74. this.toMultiselect.on('dblclick', this.onRowDblClick, this);
  75. var p = new Ext.Panel({
  76. bodyStyle:this.bodyStyle,
  77. border:this.border,
  78. layout:"table",
  79. layoutConfig:{columns:3}
  80. });
  81. p.add(this.fromMultiselect);
  82. var icons = new Ext.Panel({header:false});
  83. p.add(icons);
  84. p.add(this.toMultiselect);
  85. p.render(this.el);
  86. icons.el.down('.'+icons.bwrapCls).remove();
  87. // ICON HELL!!!
  88. if (this.imagePath!="" && this.imagePath.charAt(this.imagePath.length-1)!="/")
  89. this.imagePath+="/";
  90. this.iconUp = this.imagePath + (this.iconUp || 'up2.gif');
  91. this.iconDown = this.imagePath + (this.iconDown || 'down2.gif');
  92. this.iconLeft = this.imagePath + (this.iconLeft || 'left2.gif');
  93. this.iconRight = this.imagePath + (this.iconRight || 'right2.gif');
  94. this.iconTop = this.imagePath + (this.iconTop || 'top2.gif');
  95. this.iconBottom = this.imagePath + (this.iconBottom || 'bottom2.gif');
  96. var el=icons.getEl();
  97. this.toTopIcon = el.createChild({tag:'img', src:this.iconTop, style:{cursor:'pointer', margin:'2px'}});
  98. el.createChild({tag: 'br'});
  99. this.upIcon = el.createChild({tag:'img', src:this.iconUp, style:{cursor:'pointer', margin:'2px'}});
  100. el.createChild({tag: 'br'});
  101. this.addIcon = el.createChild({tag:'img', src:this.iconRight, style:{cursor:'pointer', margin:'2px'}});
  102. el.createChild({tag: 'br'});
  103. this.removeIcon = el.createChild({tag:'img', src:this.iconLeft, style:{cursor:'pointer', margin:'2px'}});
  104. el.createChild({tag: 'br'});
  105. this.downIcon = el.createChild({tag:'img', src:this.iconDown, style:{cursor:'pointer', margin:'2px'}});
  106. el.createChild({tag: 'br'});
  107. this.toBottomIcon = el.createChild({tag:'img', src:this.iconBottom, style:{cursor:'pointer', margin:'2px'}});
  108. this.toTopIcon.on('click', this.toTop, this);
  109. this.upIcon.on('click', this.up, this);
  110. this.downIcon.on('click', this.down, this);
  111. this.toBottomIcon.on('click', this.toBottom, this);
  112. this.addIcon.on('click', this.fromTo, this);
  113. this.removeIcon.on('click', this.toFrom, this);
  114. if (!this.drawUpIcon || this.hideNavIcons) { this.upIcon.dom.style.display='none'; }
  115. if (!this.drawDownIcon || this.hideNavIcons) { this.downIcon.dom.style.display='none'; }
  116. if (!this.drawLeftIcon || this.hideNavIcons) { this.addIcon.dom.style.display='none'; }
  117. if (!this.drawRightIcon || this.hideNavIcons) { this.removeIcon.dom.style.display='none'; }
  118. if (!this.drawTopIcon || this.hideNavIcons) { this.toTopIcon.dom.style.display='none'; }
  119. if (!this.drawBotIcon || this.hideNavIcons) { this.toBottomIcon.dom.style.display='none'; }
  120. var tb = p.body.first();
  121. this.el.setWidth(p.body.first().getWidth());
  122. p.body.removeClass();
  123. this.hiddenName = this.name;
  124. var hiddenTag = {tag: "input", type: "hidden", value: "", name: this.name};
  125. this.hiddenField = this.el.createChild(hiddenTag);
  126. },
  127. doLayout: function(){
  128. if(this.rendered){
  129. this.fromMultiselect.fs.doLayout();
  130. this.toMultiselect.fs.doLayout();
  131. }
  132. },
  133. afterRender: function(){
  134. Ext.ux.form.ItemSelector.superclass.afterRender.call(this);
  135. this.toStore = this.toMultiselect.store;
  136. this.toStore.on('add', this.valueChanged, this);
  137. this.toStore.on('remove', this.valueChanged, this);
  138. this.toStore.on('load', this.valueChanged, this);
  139. this.valueChanged(this.toStore);
  140. },
  141. toTop : function() {
  142. var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
  143. var records = [];
  144. if (selectionsArray.length > 0) {
  145. selectionsArray.sort();
  146. for (var i=0; i<selectionsArray.length; i++) {
  147. record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
  148. records.push(record);
  149. }
  150. selectionsArray = [];
  151. for (var i=records.length-1; i>-1; i--) {
  152. record = records[i];
  153. this.toMultiselect.view.store.remove(record);
  154. this.toMultiselect.view.store.insert(0, record);
  155. selectionsArray.push(((records.length - 1) - i));
  156. }
  157. }
  158. this.toMultiselect.view.refresh();
  159. this.toMultiselect.view.select(selectionsArray);
  160. },
  161. toBottom : function() {
  162. var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
  163. var records = [];
  164. if (selectionsArray.length > 0) {
  165. selectionsArray.sort();
  166. for (var i=0; i<selectionsArray.length; i++) {
  167. record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
  168. records.push(record);
  169. }
  170. selectionsArray = [];
  171. for (var i=0; i<records.length; i++) {
  172. record = records[i];
  173. this.toMultiselect.view.store.remove(record);
  174. this.toMultiselect.view.store.add(record);
  175. selectionsArray.push((this.toMultiselect.view.store.getCount()) - (records.length - i));
  176. }
  177. }
  178. this.toMultiselect.view.refresh();
  179. this.toMultiselect.view.select(selectionsArray);
  180. },
  181. up : function() {
  182. var record = null;
  183. var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
  184. selectionsArray.sort();
  185. var newSelectionsArray = [];
  186. if (selectionsArray.length > 0) {
  187. for (var i=0; i<selectionsArray.length; i++) {
  188. record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
  189. if ((selectionsArray[i] - 1) >= 0) {
  190. this.toMultiselect.view.store.remove(record);
  191. this.toMultiselect.view.store.insert(selectionsArray[i] - 1, record);
  192. newSelectionsArray.push(selectionsArray[i] - 1);
  193. }
  194. }
  195. this.toMultiselect.view.refresh();
  196. this.toMultiselect.view.select(newSelectionsArray);
  197. }
  198. },
  199. down : function() {
  200. var record = null;
  201. var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
  202. selectionsArray.sort();
  203. selectionsArray.reverse();
  204. var newSelectionsArray = [];
  205. if (selectionsArray.length > 0) {
  206. for (var i=0; i<selectionsArray.length; i++) {
  207. record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
  208. if ((selectionsArray[i] + 1) < this.toMultiselect.view.store.getCount()) {
  209. this.toMultiselect.view.store.remove(record);
  210. this.toMultiselect.view.store.insert(selectionsArray[i] + 1, record);
  211. newSelectionsArray.push(selectionsArray[i] + 1);
  212. }
  213. }
  214. this.toMultiselect.view.refresh();
  215. this.toMultiselect.view.select(newSelectionsArray);
  216. }
  217. },
  218. fromTo : function() {
  219. var selectionsArray = this.fromMultiselect.view.getSelectedIndexes();
  220. var records = [];
  221. if (selectionsArray.length > 0) {
  222. for (var i=0; i<selectionsArray.length; i++) {
  223. record = this.fromMultiselect.view.store.getAt(selectionsArray[i]);
  224. records.push(record);
  225. }
  226. if(!this.allowDup)selectionsArray = [];
  227. for (var i=0; i<records.length; i++) {
  228. record = records[i];
  229. if(this.allowDup){
  230. var x=new Ext.data.Record();
  231. record.id=x.id;
  232. delete x;
  233. this.toMultiselect.view.store.add(record);
  234. }else{
  235. this.fromMultiselect.view.store.remove(record);
  236. this.toMultiselect.view.store.add(record);
  237. selectionsArray.push((this.toMultiselect.view.store.getCount() - 1));
  238. }
  239. }
  240. }
  241. this.toMultiselect.view.refresh();
  242. this.fromMultiselect.view.refresh();
  243. var si = this.toMultiselect.store.sortInfo;
  244. if(si){
  245. this.toMultiselect.store.sort(si.field, si.direction);
  246. }
  247. this.toMultiselect.view.select(selectionsArray);
  248. },
  249. toFrom : function() {
  250. var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
  251. var records = [];
  252. if (selectionsArray.length > 0) {
  253. for (var i=0; i<selectionsArray.length; i++) {
  254. record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
  255. records.push(record);
  256. }
  257. selectionsArray = [];
  258. for (var i=0; i<records.length; i++) {
  259. record = records[i];
  260. this.toMultiselect.view.store.remove(record);
  261. if(!this.allowDup){
  262. this.fromMultiselect.view.store.add(record);
  263. selectionsArray.push((this.fromMultiselect.view.store.getCount() - 1));
  264. }
  265. }
  266. }
  267. this.fromMultiselect.view.refresh();
  268. this.toMultiselect.view.refresh();
  269. var si = this.fromMultiselect.store.sortInfo;
  270. if (si){
  271. this.fromMultiselect.store.sort(si.field, si.direction);
  272. }
  273. this.fromMultiselect.view.select(selectionsArray);
  274. },
  275. valueChanged: function(store) {
  276. var record = null;
  277. var values = [];
  278. for (var i=0; i<store.getCount(); i++) {
  279. record = store.getAt(i);
  280. values.push(record.get(this.toMultiselect.valueField));
  281. }
  282. this.hiddenField.dom.value = values.join(this.delimiter);
  283. this.fireEvent('change', this, this.getValue(), this.hiddenField.dom.value);
  284. },
  285. getValue : function() {
  286. return this.hiddenField.dom.value;
  287. },
  288. onRowDblClick : function(vw, index, node, e) {
  289. if (vw == this.toMultiselect.view){
  290. this.toFrom();
  291. } else if (vw == this.fromMultiselect.view) {
  292. this.fromTo();
  293. }
  294. return this.fireEvent('rowdblclick', vw, index, node, e);
  295. },
  296. reset: function(){
  297. range = this.toMultiselect.store.getRange();
  298. this.toMultiselect.store.removeAll();
  299. this.fromMultiselect.store.add(range);
  300. var si = this.fromMultiselect.store.sortInfo;
  301. if (si){
  302. this.fromMultiselect.store.sort(si.field, si.direction);
  303. }
  304. this.valueChanged(this.toMultiselect.store);
  305. }
  306. });
  307. Ext.reg('itemselector', Ext.ux.form.ItemSelector);
  308. //backwards compat
  309. Ext.ux.ItemSelector = Ext.ux.form.ItemSelector;