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.

932 lines
28 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. Ext.ns('Ext.ux');
  16. /**
  17. * Ext.ux.MultiCombo
  18. */
  19. Ext.ux.MultiCombo = Ext.extend(Ext.form.ComboBox, {
  20. /**
  21. * @cfg {String} overClass [x-grid3-row-over]
  22. */
  23. overClass : 'x-grid3-row-over',
  24. /**
  25. * @cfg {Boolean} enableKeyEvents for typeAhead
  26. */
  27. enableKeyEvents: true,
  28. /**
  29. * @cfg {String} selectedClass [x-grid3-row-selected]
  30. */
  31. selectedClass: 'x-grid3-row-selected',
  32. /**
  33. * @cfg {String} highlightClass The css class applied to rows which are hovered with mouse
  34. * selected via key-nav, or highlighted when a text-query matches a single item.
  35. */
  36. highlightClass: 'x-grid3-row-over',
  37. /**
  38. * @cfg {Number} autoSelectKey [44] COMMA Sets the key used to auto-select an auto-suggest
  39. * highlighted query. When pressed, the highlighted text-item will be selected as if the user
  40. * selected the row with a mouse click.
  41. */
  42. autoSelectKey : 44,
  43. /**
  44. * @cfg {String} allSelectedText Text to display when all items are selected
  45. */
  46. allSelectedText : 'All selected',
  47. /**
  48. * @cfg {Number} maxDisplayRows The maximum number of rows to show before applying vscroll
  49. */
  50. maxDisplayRows: null,
  51. mode: 'local',
  52. triggerAction: 'all',
  53. typeAhead: true,
  54. // private
  55. highlightIndex : null,
  56. highlightIndexPrev : null,
  57. query : null,
  58. /**
  59. * @cfg {Array} value CheckboxCombo expresses its value as an array.
  60. */
  61. value: [],
  62. /**
  63. * @cfg {Integer} minChars [0]
  64. */
  65. minChars: 0,
  66. initComponent : function() {
  67. var cls = 'x-combo-list';
  68. // when blurring out of field, ensure that rawValue contains ONLY items contained in Store.
  69. this.on('blur', this.validateSelections.createDelegate(this));
  70. // create an auto-select key handler, like *nix-based console [tab] key behaviour
  71. this.on('keypress', function(field, ev) {
  72. if (ev.getKey() == this.autoSelectKey) { // COMMA
  73. this.onAutoSelect();
  74. }
  75. },this);
  76. this.addEvents(
  77. /**
  78. * @event initview Fires when Combo#initView is called.
  79. * gives plugins a chance to interact with DataView
  80. * @author Chris Scott
  81. * @param {Combo} this
  82. * @param {DataView} dv
  83. */
  84. 'initview',
  85. 'clearall'
  86. );
  87. // when list expands, constrain the height with @cfg maxDisplayRows
  88. if (this.maxDisplayRows) {
  89. this.on('expand', function(){
  90. var cnt = this.store.getCount();
  91. if (cnt > this.maxDisplayRows) {
  92. var children = this.view.getNodes();
  93. var h = 0;
  94. for (var n = 0; n < this.maxDisplayRows; n++) {
  95. h += Ext.fly(children[n]).getHeight();
  96. }
  97. this.maxHeight = h;
  98. }
  99. }, this, {
  100. single: true
  101. });
  102. }
  103. this.on('beforequery', this.onQuery, this);
  104. // Enforce that plugins is an Array.
  105. if (typeof(this.plugins) == 'undefined'){
  106. this.plugins = [];
  107. }
  108. else if (!Ext.isArray(this.plugins)) {
  109. this.plugins = [this.plugins];
  110. }
  111. var tmp = this.value; // for case where transform is set.
  112. Ext.ux.MultiCombo.superclass.initComponent.call(this);
  113. if (this.transform) {
  114. if (typeof(tmp) == 'undefined') {
  115. tmp = [];
  116. }
  117. this.setValue(tmp);
  118. }
  119. },
  120. // private
  121. onViewClick : function(dv, index, node, ev){
  122. var rec = this.store.getAt(index);
  123. this.onSelect(rec, index);
  124. this.el.focus();
  125. /*
  126. if(doFocus !== false){
  127. this.el.focus();
  128. }
  129. */
  130. },
  131. // onTriggerClick, overrides Ext.form.ComboBox#onTriggerClick
  132. onTriggerClick: function() {
  133. if (this.highlightIndex != -1) {
  134. this.clearHighlight();
  135. }
  136. this.highlightIndex = -1;
  137. if(this.disabled){
  138. return;
  139. }
  140. if(this.isExpanded()){
  141. this.collapse();
  142. this.el.focus();
  143. }else {
  144. this.onFocus({});
  145. if(this.triggerAction == 'all') {
  146. this.doQuery(this.getRawValue(), true);
  147. var vlen = this.getValue().length, slen = this.view.getSelectedRecords().length;
  148. if (vlen != slen || vlen == 0) {
  149. this.selectByValue(this.value, true);
  150. }
  151. } else {
  152. this.expand();
  153. this.doQuery(this.getRawValue());
  154. }
  155. this.highlightIndex = -1
  156. this.highlightIndexPrev = null;
  157. this.selectNext();
  158. this.scrollIntoView();
  159. this.el.focus();
  160. }
  161. },
  162. // onQuery, beforequery listener, @return false
  163. onQuery : function(qe) {
  164. q = qe.query;
  165. forceAll = qe.forceAll;
  166. if(forceAll === true || (q.length >= this.minChars)){
  167. if(this.lastQuery !== q){
  168. if (typeof(this.lastQuery) != 'undefined') {
  169. if (q.match(new RegExp('^'+this.allSelectedText))) {
  170. this.query = this.store.data;
  171. }
  172. else if (this.lastQuery.length > q.length) {
  173. var items = q.replace(/\s+/g, '').split(',');
  174. if (items[items.length-1].length == 0) {
  175. items.pop();
  176. }
  177. this.query = this.store.data.filterBy(this.store.createFilterFn(this.displayField, new RegExp('^'+items.join('$|^')+'$', "i"), false, false));
  178. }
  179. else {
  180. this.query = null;
  181. }
  182. }
  183. this.lastQuery = q;
  184. if(this.mode == 'local'){
  185. var raw = this.getRawValue();
  186. if (raw == this.allSelectedText) {
  187. }
  188. var items = raw.replace(/\s+/g, '').split(',');
  189. var last = items.pop();
  190. this.matches = this.store.data.filterBy(this.store.createFilterFn(this.displayField, new RegExp('^'+last, "i"), false, false)).filterBy(this.createTypeAheadFilterFn(items));
  191. if (this.matches.getCount() == 0) {
  192. this.clearHighlight();
  193. }
  194. if (q.length == 0) {
  195. this.view.clearSelections();
  196. this.updateValue([]);
  197. }
  198. this.onLoad();
  199. } else {
  200. this.store.baseParams[this.queryParam] = q;
  201. this.store.load({
  202. params: this.getParams(q)
  203. });
  204. this.expand();
  205. }
  206. }else{
  207. this.selectedIndex = -1;
  208. this.onLoad();
  209. }
  210. }
  211. return false;
  212. },
  213. // onLoad, overrides Ext.form.ComboBox#onLoad
  214. onLoad : function(){
  215. if(!this.hasFocus){
  216. return;
  217. }
  218. if(this.store.getCount() > 0){
  219. if (!this.isExpanded()) {
  220. this.expand();
  221. this.restrictHeight();
  222. }
  223. if(this.lastQuery == this.allQuery){
  224. if(this.editable){
  225. this.el.dom.select();
  226. }
  227. }else{
  228. if (this.query != null) {
  229. var values = [], indexes = [];
  230. this.query.each(function(r){
  231. values.push(r.data[this.valueField]);
  232. indexes.push(this.store.indexOf(r));
  233. }, this);
  234. this.view.clearSelections();
  235. this.updateValue(values, this.getRawValue());
  236. this.view.select(indexes);
  237. }
  238. if (this.matches != null) {
  239. if (this.matches.getCount() == 1) {
  240. this.highlight(this.store.indexOf(this.matches.first()));
  241. this.scrollIntoView();
  242. }
  243. }
  244. else {
  245. // @HACK: If store was configured with a proxy, set its mode to local now that its populated with data.
  246. // Re-execute the query now.
  247. this.mode = 'local';
  248. this.lastQuery = undefined;
  249. this.doQuery(this.getRawValue(), true);
  250. }
  251. if(this.typeAhead && this.lastKey != Ext.EventObject.DOWN && this.lastKey != Ext.EventObject.BACKSPACE && this.lastKey != Ext.EventObject.DELETE){
  252. this.taTask.delay(this.typeAheadDelay);
  253. }
  254. }
  255. }else{
  256. this.onEmptyResults();
  257. }
  258. },
  259. onSelect : function(record, index) {
  260. if (index == -1) {
  261. throw new Error('MultiCombo#onSelect did not receive a valid index');
  262. }
  263. // select only when user clicks [apply] button
  264. if (this.selectOnApply == true) {
  265. return;
  266. }
  267. if (this.fireEvent('beforeselect', this, record, index) !== false) {
  268. var text = [];
  269. var value = [];
  270. var rs = this.view.getSelectedRecords();
  271. for (var n = 0, len = rs.length; n < len; n++) {
  272. text.push(rs[n].data[this.displayField]);
  273. value.push(rs[n].data[this.valueField]);
  274. }
  275. this.updateValue(value, (value.length != this.store.getCount()) ? text.join(', ') : this.allSelectedText);
  276. var node = this.view.getNode(index);
  277. this.innerList.scrollChildIntoView(node, false);
  278. this.fireEvent('select', this, record, index);
  279. }
  280. },
  281. // private
  282. onViewOver : function(ev, node){
  283. var t = ev.getTarget(this.view.itemSelector);
  284. if (t == null) {
  285. return;
  286. }
  287. this.highlightIndex = this.store.indexOf(this.view.getRecord(t));
  288. this.clearHighlight();
  289. this.highlight(this.highlightIndex);
  290. if(this.inKeyMode){ // prevent key nav and mouse over conflicts
  291. return;null
  292. }
  293. return;
  294. },
  295. // private
  296. onTypeAhead : function(){
  297. if(this.store.getCount() > 0){
  298. this.inKeyMode = false;
  299. var raw = this.getRawValue();
  300. var pos = this.getCaretPosition(raw);
  301. var items = [];
  302. var query = '';
  303. if (pos !== false && pos < raw.length) {
  304. items = raw.substr(0, pos).replace(/\s+/g, '').split(',');
  305. query = items.pop();
  306. } else {
  307. items = raw.replace(/\s+/g, '').split(',');
  308. query = items.pop();
  309. }
  310. var rs = this.store.data.filterBy(this.store.createFilterFn(this.displayField, new RegExp(query, "i"), false, false)).filterBy(this.createTypeAheadFilterFn(items));
  311. if (rs.getCount() == 1) {
  312. var r = rs.first();
  313. var rindex = this.store.indexOf(r)
  314. if (!this.view.isSelected(rindex)) {
  315. this.typeAheadSelected = true;
  316. var selStart = raw.length;
  317. var len = items.join(',').length;
  318. var selEnd = null;
  319. var newValue = r.data[this.displayField];
  320. if (pos !== false && pos < raw.length) {
  321. var insertIdx = items.length;
  322. var selStart = pos;
  323. items = raw.replace(/\s+/g, '').split(',');
  324. items.splice(insertIdx, 1, newValue);
  325. selEnd = items.slice(0, insertIdx+1).join(', ').length;
  326. this.highlight(rindex);
  327. this.scrollIntoView();
  328. }
  329. else {
  330. items.push(newValue);
  331. }
  332. var len = items.join(',').length;
  333. if(selStart != len){
  334. var lastWord = raw.split(',').pop();
  335. if (items.length >1 && lastWord.match(/^\s+/) == null) {
  336. selStart++;
  337. }
  338. this.setRawValue(items.join(', '));
  339. this.selectText(selStart, (selEnd!=null) ? selEnd : this.getRawValue().length);
  340. }
  341. }
  342. }
  343. }
  344. },
  345. apply : function() {
  346. var selected = this.view.getSelectedRecords();
  347. var value = [];
  348. for (var n=0,len=selected.length;n<len;n++) {
  349. value.push(selected[n].data[this.valueField]);
  350. }
  351. this.setValue(value);
  352. },
  353. getCaretPosition : function(raw) {
  354. raw = raw || this.getRawValue();
  355. if(document.selection) { // <-- IE, ugh: http://parentnode.org/javascript/working-with-the-cursor-position/
  356. var range = document.selection.createRange();
  357. //Save the current value. We will need this value later to find out, where the text has been changed
  358. var orig = obj.value.replace(/rn/g, "n");
  359. // replace the text
  360. range.text = text;
  361. // Now get the new content and save it into a temporary variable
  362. var actual = tmp = obj.value.replace(/rn/g, "n");
  363. /* Find the first occurance, where the original differs
  364. from the actual content. This could be the startposition
  365. of our text selection, but it has not to be. Think of the
  366. selection "ab" and replacing it with "ac". The first
  367. difference would be the "c", while the start position
  368. is the "a"
  369. */
  370. for(var diff = 0; diff < orig.length; diff++) {
  371. if(orig.charAt(diff) != actual.charAt(diff)) break;
  372. }
  373. /* To get the real start position, we iterate through
  374. the string searching for the whole replacement
  375. text - "abc", as long as the first difference is not
  376. reached. If you do not understand that logic - no
  377. blame to you, just copy & paste it ;)
  378. */
  379. for(var index = 0, start = 0; tmp.match(text) && (tmp = tmp.replace(text, "")) && index <= diff; index = start + text.length) {
  380. start = actual.indexOf(text, index);
  381. }
  382. } else if(this.el.dom.selectionStart) { // <-- Go the Gecko way
  383. return this.el.dom.selectionStart;
  384. } else {
  385. // Fallback for any other browser
  386. return false;
  387. }
  388. },
  389. onAutoSelect : function() {
  390. if (!this.isExpanded()) {
  391. var vlen = this.getValue().length, slen = this.view.getSelectedRecords().length;
  392. if (vlen != slen || vlen == 0) {
  393. this.selectByValue(this.value, true);
  394. }
  395. }
  396. var raw = this.getRawValue();
  397. this.selectText(raw.length, raw.length);
  398. var pos = this.getCaretPosition(raw);
  399. var word = '';
  400. if (pos !== false && pos < raw.length) {
  401. word = Ext.util.Format.trim(raw.substr(0, pos).split(',').pop());
  402. } else {
  403. word = Ext.util.Format.trim(raw.split(',').pop());
  404. }
  405. var idx = this.store.find(this.displayField, word);
  406. if (idx > -1 && !this.view.isSelected(idx)) {
  407. var rec = this.store.getAt(idx);
  408. this.select(idx);
  409. }
  410. },
  411. // filters-out already-selected items from type-ahead queries.
  412. // e.g.: if store contains: "betty, barney, bart" and betty is already selected,
  413. // when user types "b", only "bart" and "barney" should be returned as possible matches,
  414. // since betty is *already* selected
  415. createTypeAheadFilterFn : function(items) {
  416. var key = this.displayField;
  417. return function(rec) {
  418. var re = new RegExp(rec.data[key], "i");
  419. var add = true;
  420. for (var n=0,len=items.length;n<len;n++) {
  421. if (re.test(items[n])) {
  422. add = false;
  423. break;
  424. }
  425. }
  426. return add;
  427. }
  428. },
  429. updateValue : function(value, text) {
  430. this.value = value;
  431. if(this.hiddenField){
  432. this.hiddenField.value = value.join(',');
  433. }
  434. if (typeof(text) == 'string') {
  435. this.setRawValue(text);
  436. }
  437. },
  438. /**
  439. * setValue
  440. * Accepts a comma-separated list of ids or an array. if given a string, will conver to Array.
  441. * @param {Array, String} v
  442. */
  443. setValue : function(v) {
  444. var text = [];
  445. var value = [];
  446. if (typeof(v) == 'string') { // <-- "1,2,3"
  447. value = v.match(/\d+/g); // <-- strip multiple spaces and split on ","
  448. if(value){
  449. for (var n=0,len=value.length;n<len;n++) {
  450. value[n] = parseInt(value[n]);
  451. }
  452. }
  453. }
  454. else if (Ext.isArray(v)) { // <-- [1,2,3]
  455. value = v;
  456. }
  457. if (value && value.length) {
  458. if (this.mode == 'local') {
  459. this.updateValue(value);
  460. this.setRawValue(this.getTextValue());
  461. }
  462. else {
  463. this.updateValue(value);
  464. this.store.load({
  465. callback: function() {
  466. this.setRawValue(this.getTextValue());
  467. },
  468. scope: this
  469. });
  470. this.mode = 'local';
  471. }
  472. }
  473. },
  474. getTextValue : function() {
  475. if (this.value.length == this.store.getCount()) {
  476. return this.allSelectedText;
  477. }
  478. else {
  479. var text = [];
  480. this.store.data.filterBy(this.store.createFilterFn(this.valueField, new RegExp(this.value.join('|'), "i"), false, false)).each(function(r){
  481. text.push(r.data[this.displayField]);
  482. }, this);
  483. return text.join(', ');
  484. }
  485. },
  486. /**
  487. * Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
  488. * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
  489. * @param {Number} index The zero-based index of the list item to select
  490. * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
  491. * selected item if it is not currently in view (defaults to true)
  492. */
  493. select : function(index, scrollIntoView){
  494. if (!typeof(index) == 'number') {
  495. throw new Error('MultiCombo#select expected @param {Number} index but got: ' + typeof(index));
  496. }
  497. this.view.isSelected(index) ? this.view.deselect(index, true) : this.view.select(index, true);
  498. this.onSelect(this.store.getAt(index), index);
  499. this.matches = null;
  500. if(scrollIntoView !== false){
  501. var el = this.view.getNode(index);
  502. if(el){
  503. this.innerList.scrollChildIntoView(el, false);
  504. }
  505. }
  506. },
  507. getLastValue : function() {
  508. return Ext.util.Format.trim(this.getRawValue().split(',').pop());
  509. },
  510. /**
  511. * Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
  512. * The store must be loaded and the list expanded for this function to work, otherwise use setValue.
  513. * @param {String} value The data value of the item to select
  514. * @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
  515. * selected item if it is not currently in view (defaults to true)
  516. * @return {Boolean} True if the value matched an item in the list, else false
  517. */
  518. selectByValue : function(v, scrollIntoView){
  519. if (v.length) {
  520. var indexes = [];
  521. var rs = this.store.data.filterBy(this.store.createFilterFn(this.valueField, new RegExp(v.join('|'), "i"))).each(function(r){
  522. indexes.push(this.store.indexOf(r));
  523. }, this);
  524. if (indexes.length) {
  525. this.view.select(indexes);
  526. return true;
  527. }
  528. }
  529. else {
  530. this.view.clearSelections();
  531. this.setRawValue('');
  532. return false;
  533. }
  534. },
  535. // private
  536. initEvents : function(){
  537. Ext.form.ComboBox.superclass.initEvents.call(this);
  538. this.keyNav = new Ext.KeyNav(this.el, {
  539. "up" : function(e){
  540. this.lastKey = Ext.EventObject.UP;
  541. this.inKeyMode = true;
  542. this.selectPrev();
  543. this.scrollIntoView();
  544. },
  545. "down" : function(e){
  546. this.inKeyMode = true;
  547. if(!this.isExpanded()){
  548. this.lastKey = Ext.EventObject.DOWN;
  549. this.onTriggerClick();
  550. }else{
  551. this.selectNext();
  552. this.scrollIntoView();
  553. }
  554. },
  555. "enter" : function(e){
  556. var idx = this.highlightIndex;
  557. if (this.inKeyMode === true) {
  558. if (this.plugins.length && (idx <= -1)) {
  559. if (this.plugins[idx + 1]) {
  560. this.plugins[idx + 1].onEnter(this);
  561. }
  562. }
  563. else
  564. if (this.plugins.length && this.highlightIndex == 0 && this.highlightIndexPrev == -1) {
  565. if (this.plugins[idx]) {
  566. this.plugins[idx].onEnter(this);
  567. }
  568. }
  569. else {
  570. var idx = this.getHighlightedIndex() || 0;
  571. if (this.highlightIndex != null && idx != null) {
  572. this.select(idx, true);
  573. //this.delayedCheck = true;
  574. //this.unsetDelayCheck.defer(10, this);
  575. }
  576. }
  577. }
  578. else {
  579. var v = this.getLastValue();
  580. var raw = this.getRawValue();
  581. /** this block should be moved to method getCurrentWord
  582. *
  583. */
  584. var pos = this.getCaretPosition(raw);
  585. var word = '';
  586. if (pos !== false && pos < raw.length) {
  587. word = Ext.util.Format.trim(raw.substr(0, pos).split(',').pop());
  588. } else {
  589. word = Ext.util.Format.trim(raw.split(',').pop());
  590. }
  591. /*******************************************************/
  592. var idx = this.store.find(this.displayField, word);
  593. if (idx != -1) {
  594. var rec = this.store.getAt(idx);
  595. this.select(idx, true);
  596. }
  597. raw = this.getRawValue();
  598. this.selectText(raw.length, raw.length);
  599. this.collapse();
  600. }
  601. },
  602. "esc" : function(e){
  603. this.collapse();
  604. },
  605. "tab" : function(e){
  606. if (this.matches != null && this.matches.getCount() == 1) {
  607. var idx = this.store.indexOf(this.matches.first());
  608. if (!this.view.isSelected(idx)) {
  609. this.select(this.store.indexOf(this.matches.first()), true);
  610. }
  611. }
  612. else if (this.value.length == 0 && this.getRawValue().length > 0) {
  613. this.setRawValue('');
  614. }
  615. this.collapse();
  616. return true;
  617. },
  618. scope : this,
  619. doRelay : function(foo, bar, hname){
  620. if(hname == 'down' || this.scope.isExpanded()){
  621. return Ext.KeyNav.prototype.doRelay.apply(this, arguments);
  622. }
  623. return true;
  624. },
  625. forceKeyDown : true
  626. });
  627. this.queryDelay = Math.max(this.queryDelay || 10,
  628. this.mode == 'local' ? 10 : 250);
  629. this.dqTask = new Ext.util.DelayedTask(this.initQuery, this);
  630. if(this.typeAhead){
  631. this.taTask = new Ext.util.DelayedTask(this.onTypeAhead, this);
  632. }
  633. if(this.editable !== false){
  634. this.el.on("keyup", this.onKeyUp, this);
  635. }
  636. if(this.forceSelection){
  637. this.on('blur', this.doForce, this);
  638. }
  639. },
  640. // private, blur-handler to ensure that rawValue contains only values from selections, in the same order as selected
  641. validateSelections : function(field) {
  642. var v = this.getValue();
  643. var text = [];
  644. for (var i=0,len=v.length;i<len;i++) {
  645. var idx = this.store.find(this.valueField, v[i]);
  646. if (idx >=0) {
  647. text.push(this.store.getAt(idx).data[this.displayField]);
  648. }
  649. }
  650. this.setRawValue(text.join(', '));
  651. },
  652. scrollIntoView : function() {
  653. var el = this.getHighlightedNode();
  654. if (el) {
  655. this.innerList.scrollChildIntoView(el);
  656. }
  657. },
  658. // private
  659. selectNext : function(){
  660. this.clearHighlight();
  661. if (this.highlightIndex == null) {
  662. this.highlightIndex = -1;
  663. }
  664. if (this.highlightIndex <= -1 && this.highlightIndexPrev != -1) {
  665. if (this.plugins.length > 0) {
  666. var idx = Math.abs(this.highlightIndex)-1;
  667. if (this.plugins.length >= Math.abs(this.highlightIndex)) {
  668. this.plugins[idx].selectNext(this);
  669. this.highlightIndexPrev = this.highlightIndex;
  670. this.highlightIndex++;
  671. return false;
  672. }
  673. }
  674. }
  675. if (this.highlightIndexPrev == -1 && this.highlightIndex == 0) {
  676. this.highlightIndex = -1;
  677. }
  678. var ct = this.store.getCount();
  679. if(ct > 0){
  680. if (this.highlightIndex == -1 || this.highlightIndex+1 < ct) {
  681. if (this.highlightIndex == -1) {
  682. this.highlightIndexPrev = 0;
  683. }
  684. else {
  685. this.highlightIndexPrev = this.highlightIndex -1;
  686. }
  687. this.highlight(++this.highlightIndex);
  688. }
  689. else {
  690. this.highlight(ct-1);
  691. }
  692. }
  693. },
  694. // private
  695. selectPrev : function(){
  696. this.clearHighlight();
  697. if (this.highlightIndex <= 0) {
  698. var idx = Math.abs(this.highlightIndex);
  699. if (this.plugins.length >= idx+1 && this.highlightIndexPrev >= 0) {
  700. this.clearHighlight();
  701. this.plugins[idx].selectPrev(this);
  702. this.highlightIndexPrev = this.highlightIndex;
  703. this.highlightIndex--;
  704. if (this.highlightIndex == -1) {
  705. this.highlightIndexPrev = -1;
  706. }
  707. return false;
  708. }
  709. else {
  710. this.highlightIndex = -1;
  711. this.highlightIndexPrev = -1;
  712. this.collapse();
  713. return;
  714. }
  715. }
  716. this.highlightIndexPrev = this.highlightIndex;
  717. var ct = this.store.getCount();
  718. if(ct > 0){
  719. if (this.highlighIndex == -1) {
  720. this.highlightIndex = 0;
  721. }
  722. else if (this.highlightIndex != 0) {
  723. this.highlightIndex--;
  724. }
  725. else if (this.highlightIndex == 0) {
  726. this.collapse();
  727. }
  728. this.highlight(this.highlightIndex);
  729. }
  730. },
  731. collapse : function() {
  732. if (this.isExpanded()) {
  733. this.highlightIndex = null;
  734. this.highlightIndexPrev = null;
  735. }
  736. Ext.ux.MultiCombo.superclass.collapse.call(this);
  737. },
  738. highlight : function(index) {
  739. this.view.el.select('.'+this.highlightClass).removeClass(this.highlightClass);
  740. var node = Ext.fly(this.view.getNode(index));
  741. if (node) {
  742. node.addClass(this.highlightClass);
  743. }
  744. },
  745. getHighlightedIndex : function() {
  746. var node = this.view.el.child('.' + this.highlightClass, true);
  747. return (node) ? this.store.indexOf(this.view.getRecord(node)) : this.highlightIndex;
  748. },
  749. getHighlightedNode : function() {
  750. return this.view.el.child('.'+this.highlightClass, true);
  751. },
  752. clearHighlight : function() {
  753. if (typeof(this.view) != 'object') { return false; }
  754. var el = this.view.el.select('.'+this.highlightClass);
  755. if (el) {
  756. el.removeClass(this.highlightClass);
  757. }
  758. },
  759. // private
  760. initList : function(){
  761. if(!this.list){
  762. var cls = 'x-combo-list';
  763. this.list = new Ext.Layer({
  764. shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
  765. });
  766. var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
  767. this.list.setWidth(lw);
  768. this.list.swallowEvent('mousewheel');
  769. this.assetHeight = 0;
  770. if(this.syncFont !== false){
  771. this.list.setStyle('font-size', this.el.getStyle('font-size'));
  772. }
  773. if(this.title){
  774. this.header = this.list.createChild({cls:cls+'-hd', html: this.title});
  775. this.assetHeight += this.header.getHeight();
  776. }
  777. this.innerList = this.list.createChild({cls:cls+'-inner'});
  778. this.innerList.on('mouseover', this.onViewOver, this);
  779. this.innerList.on('mousemove', this.onViewMove, this);
  780. this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));
  781. if(this.pageSize){
  782. this.footer = this.list.createChild({cls:cls+'-ft'});
  783. this.pageTb = new Ext.PagingToolbar({
  784. store:this.store,
  785. pageSize: this.pageSize,
  786. renderTo:this.footer
  787. });
  788. this.assetHeight += this.footer.getHeight();
  789. }
  790. if(!this.tpl){
  791. /**
  792. * @cfg {String/Ext.XTemplate} tpl The template string, or {@link Ext.XTemplate}
  793. * instance to use to display each item in the dropdown list. Use
  794. * this to create custom UI layouts for items in the list.
  795. * <p>
  796. * If you wish to preserve the default visual look of list items, add the CSS
  797. * class name <pre>x-combo-list-item</pre> to the template's container element.
  798. * <p>
  799. * <b>The template must contain one or more substitution parameters using field
  800. * names from the Combo's</b> {@link #store Store}. An example of a custom template
  801. * would be adding an <pre>ext:qtip</pre> attribute which might display other fields
  802. * from the Store.
  803. * <p>
  804. * The dropdown list is displayed in a DataView. See {@link Ext.DataView} for details.
  805. */
  806. this.tpl = '<tpl for="."><div class="'+cls+'-item">{' + this.displayField + '}</div></tpl>';
  807. /**
  808. * @cfg {String} itemSelector
  809. * <b>This setting is required if a custom XTemplate has been specified in {@link #tpl}
  810. * which assigns a class other than <pre>'x-combo-list-item'</pre> to dropdown list items</b>.
  811. * A simple CSS selector (e.g. div.some-class or span:first-child) that will be
  812. * used to determine what nodes the DataView which handles the dropdown display will
  813. * be working with.
  814. */
  815. }
  816. /**
  817. * The {@link Ext.DataView DataView} used to display the ComboBox's options.
  818. * @type Ext.DataView
  819. */
  820. this.view = new Ext.DataView({
  821. applyTo: this.innerList,
  822. tpl: this.tpl,
  823. simpleSelect: true,
  824. multiSelect: true,
  825. overClass: this.overClass,
  826. selectedClass: this.selectedClass,
  827. itemSelector: this.itemSelector || '.' + cls + '-item'
  828. });
  829. this.view.on('click', this.onViewClick, this);
  830. this.fireEvent('initview', this, this.view);
  831. this.bindStore(this.store, true);
  832. if(this.resizable){
  833. this.resizer = new Ext.Resizable(this.list, {
  834. pinned:true, handles:'se'
  835. });
  836. this.resizer.on('resize', function(r, w, h){
  837. this.maxHeight = h-this.handleHeight-this.list.getFrameWidth('tb')-this.assetHeight;
  838. this.listWidth = w;
  839. this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
  840. this.restrictHeight();
  841. }, this);
  842. this[this.pageSize?'footer':'innerList'].setStyle('margin-bottom', this.handleHeight+'px');
  843. }
  844. }
  845. }
  846. });
  847. Ext.reg('multicombo', Ext.ux.MultiCombo);