Combox.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.widget.Combox = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. "style": "default",
  8. "positionX": "left",
  9. "list": [],
  10. "optionsMethod": null,
  11. "splitStr": /,\s*|;\s*|,\s*|;\s*/g,
  12. "splitShow": ", ",
  13. "focusList": false,
  14. "noDataColor": true,
  15. "onlySelect": false,
  16. "count": 0
  17. },
  18. initialize: function(options){
  19. this.setOptions(options);
  20. this.splitRegExp = new RegExp(this.options.splitStr);
  21. this.path = o2.session.path+"/widget/$Combox/";
  22. this.cssPath = o2.session.path+"/widget/$Combox/"+this.options.style+"/css.wcss";
  23. this._loadCss();
  24. this.values = [];
  25. //this.lastValue();
  26. this.load();
  27. },
  28. getSelectList: function(value, callback, comboxValueObject){
  29. var list = [];
  30. if (this.options.list.length){
  31. if (this.options.onlySelect){
  32. list = this.options.list;
  33. }else{
  34. var listValues = this.options.list.filter(function(v, i){
  35. var key = (v.keyword || "")+v.text;
  36. return (key.indexOf(value)!==-1);
  37. });
  38. list = listValues;
  39. }
  40. }
  41. if (this.options.optionsMethod){
  42. this.options.optionsMethod(value, function(methodValues){
  43. if (methodValues.length){
  44. if (list.length){
  45. list = list.concat(methodValues);
  46. }else{
  47. list = methodValues;
  48. }
  49. }
  50. if (callback) callback(list);
  51. }.bind(this), comboxValueObject);
  52. //var methodValues = this.options.optionsMethod(value);
  53. }else{
  54. if (callback) callback(list);
  55. }
  56. //return list;
  57. },
  58. load: function(){
  59. if (this.fireEvent("queryLoad")){
  60. this.node = new Element("div", {"styles": this.css.comboxNode});
  61. this.copyPrototype();
  62. this.setEvent();
  63. this.fireEvent("postLoad");
  64. }
  65. },
  66. clear: function(){
  67. if (this.input) o2.release(this.input);
  68. this.input = null;
  69. this.node.empty();
  70. this.values = [];
  71. },
  72. getData: function(){
  73. var node = this.node.getFirst();
  74. var data = [];
  75. while (node){
  76. var item = node.retrieve("item");
  77. if (item){
  78. if (item.data || item.value){
  79. data.push(item.data || item.value);
  80. }
  81. }
  82. node = node.getNext();
  83. }
  84. return data;
  85. },
  86. copyPrototype: function(){
  87. this.inject = this.node.inject.bind(this.node);
  88. this.setStyle = this.node.setStyle.bind(this.node);
  89. this.setStyles = this.node.setStyles.bind(this.node);
  90. this.set = this.node.set.bind(this.node);
  91. // for (k in this.node.constructor.prototype){
  92. // if (typeOf(this.node[k])==="function"){
  93. // this[k] = this.node[k].bind(this.node);
  94. // }else{
  95. // this[k] = this.node[k];
  96. // }
  97. // }
  98. },
  99. setEvent:function(){
  100. this.node.addEvents({
  101. "focus": function(e){
  102. if (!this.selectStatus){
  103. if (!this.editItem) this.intoEdit(e);
  104. }
  105. }.bind(this),
  106. "mousedown": function(e){
  107. if (!this.selectStatus){
  108. if (!this.editItem) this.intoEdit(e);
  109. e.stopPropagation();
  110. e.preventDefault();
  111. }
  112. }.bind(this)
  113. // "selectstart": function(e){
  114. // if (e,event.buttons = "")
  115. // if (e.event.buttons!=0){
  116. // this.selectStatus = true;
  117. // this.stopEdit();
  118. // }
  119. // }.bind(this)
  120. // // "mouseup": function(e){
  121. // // this.selectStatus = false;
  122. // // }.bind(this),
  123. // "dblclick": function(e){
  124. // var range = document.createRange();
  125. // range.selectNodeContents(this.node);
  126. // alert(range.toString());
  127. // }.bind(this)
  128. });
  129. },
  130. stopEdit: function(){
  131. if (this.editItem){
  132. this.editItem.input.confirmValue();
  133. }else{
  134. if (this.input) this.input.confirmValue();
  135. }
  136. if (this.input) this.input.node.hide();
  137. },
  138. intoEdit: function(e){
  139. if (this.options.count){
  140. if (this.values.length>=this.options.count){
  141. // if (this.input) this.input.noBlur = true;
  142. if (this.input) this.input.node.hide();
  143. //this.getLast().edit();
  144. return false;
  145. }
  146. }
  147. if (!this.input){
  148. this.input = new o2.widget.Combox.Input(this, this, "");
  149. this.input.node.inject(this.node);
  150. this.input.node.setStyle("width", "1px");
  151. }
  152. if (e){
  153. if (e.type==="item"){
  154. var node = e.node;
  155. if (e.input){
  156. node = e.input.optionListNode || e.input.node;
  157. }
  158. this.input.node.inject(node, "after");
  159. }else{
  160. var value = this.input.node.get("value");
  161. if (value) this.commitInput();
  162. var node = this.node.getFirst();
  163. while (node){
  164. if (node.retrieve("item")){
  165. var p = node.getPosition();
  166. var s = node.getSize();
  167. if (p.x>e.page.x && (p.y-3)<e.page.y && (p.y+s.y+3)>e.page.y) break;
  168. }
  169. node = node.getNext();
  170. }
  171. if (node){
  172. this.input.node.inject(node, "before");
  173. }else{
  174. this.input.node.inject(this.node);
  175. }
  176. }
  177. }
  178. this.input.node.show();
  179. this.input.setInputNodeStyles();
  180. //this.input.node.set("value", "111");
  181. this.input.node.focus();
  182. this.input.setInputPosition();
  183. if (this.options.focusList) this.input.searchItems();
  184. },
  185. commitInput: function(data){
  186. var valueStr = this.input.node.get("value");
  187. this.input.node.set("value", "");
  188. if (valueStr){
  189. var values = valueStr.split(this.splitRegExp);
  190. if (this.options.count) values = values.slice(0, this.options.count);
  191. this.createItem(values, 0, data, function(){
  192. this.input.node.set("value", "");
  193. //this.input.hideOptionList();
  194. this.input.searchItems();
  195. this.input.setInputWidth();
  196. window.setTimeout(function(){
  197. // if (this.input){
  198. // this.input.node.blur();
  199. // this.input.node.focus();
  200. // }else{
  201. this.intoEdit();
  202. // }
  203. }.bind(this), 10);
  204. }.bind(this));
  205. //
  206. // values.each(function(value){
  207. // this.values.push(new o2.widget.Combox.Value(this, value, data));
  208. //
  209. // }.bind(this));
  210. //
  211. }
  212. },
  213. createItem: function(values, i, data, callback){
  214. if (values[i]){
  215. var value = values[i];
  216. var itemData = data;
  217. var itemText = value;
  218. if (!itemData){
  219. if (typeOf(value)==="object"){
  220. itemData = value.value;
  221. itemText = value.text;
  222. }
  223. }
  224. var v = new o2.widget.Combox.Value(this, itemText, itemData, {
  225. "onLoad": function(){
  226. i++;
  227. if (values[i]){
  228. this.createItem(values, i, data, callback);
  229. }else{
  230. if (callback) callback();
  231. }
  232. }.bind(this)
  233. }, true);
  234. this.values.push(v);
  235. v.load();
  236. }
  237. },
  238. addNewValues: function(values, callback){
  239. this.createItem(values, 0, null, callback);
  240. },
  241. addNewValue: function(value, data){
  242. if (value){
  243. this.values.push(new o2.widget.Combox.Value(this, value, data));
  244. if (this.input){
  245. this.input.node.set("value", "");
  246. this.input.hideOptionList();
  247. this.input.searchItems();
  248. this.input.setInputWidth();
  249. }
  250. }
  251. },
  252. getFirst: function(){
  253. var node = this.node.getFirst();
  254. if (node){
  255. while (node && !node.retrieve("item")){ node = node.getNext(); }
  256. if (node) return node.retrieve("item");
  257. }
  258. return null;
  259. },
  260. getLast: function(){
  261. var node = this.node.getLast();
  262. if (node){
  263. while (node && !node.retrieve("item")){ node = node.getPrevious(); }
  264. if (node) return node.retrieve("item");
  265. }
  266. return null;
  267. },
  268. getNextItem: function(){
  269. var node = this.input.node.getNext();
  270. if (node) return node.retrieve("item");
  271. return null;
  272. },
  273. getPreviousItem: function(){
  274. var node = this.input.node.getPrevious();
  275. while (node && !node.retrieve("item")){ node = node.getPrevious(); }
  276. if (node) return node.retrieve("item");
  277. return null;
  278. },
  279. deleteItem: function(item){
  280. this.values.erase(item);
  281. item.node.destroy();
  282. if (item.input) item.input.destroy();
  283. o2.release(item);
  284. }
  285. });
  286. o2.widget.Combox.Value = new Class({
  287. Implements: [Options, Events],
  288. initialize: function(combox, value, data, options, delayLoad){
  289. this.setOptions(options);
  290. this.combox = combox;
  291. this.css = this.combox.css;
  292. this.value = value;
  293. this.data = data || null;
  294. this.type = "item";
  295. this.index = this.combox.values.length;
  296. if(!delayLoad){
  297. this.load();
  298. }
  299. },
  300. getItemPosition: function(){
  301. var i=0;
  302. var item = this.getPreviousItem();
  303. while (item){
  304. i++;
  305. item = item.getPreviousItem();
  306. }
  307. return i;
  308. },
  309. checkData: function(callback){
  310. if (!this.data){
  311. this.combox.getSelectList(this.value, function(list){
  312. if (list.length==1){
  313. this.data = list[0].value;
  314. this.value = list[0].text;
  315. }
  316. if (callback) callback();
  317. }.bind(this), this );
  318. }else{
  319. if (callback) callback();
  320. }
  321. },
  322. load: function(){
  323. this.node = new Element("div", {"styles": this.css.valueItemNode});
  324. if (this.combox.input){
  325. this.node.inject(this.combox.input.node, "before");
  326. }else{
  327. this.node.inject(this.combox.node);
  328. }
  329. if (this.getNextItem()){
  330. this.node.set("text", this.value+this.combox.options.splitShow);
  331. }else{
  332. this.node.set("text", this.value);
  333. }
  334. var prev = this.getPreviousItem();
  335. if (prev){
  336. prev.node.set("text", prev.value+this.combox.options.splitShow);
  337. }
  338. this.node.store("item", this);
  339. this.node.addEvents({
  340. "click": function(e){this.edit();e.stopPropagation();}.bind(this),
  341. "mouseover": function(e){this.node.setStyles(this.css.valueItemNode_over)}.bind(this),
  342. "mouseout": function(e){this.node.setStyles(this.css.valueItemNode)}.bind(this),
  343. "mousedown": function(e){e.stopPropagation();}.bind(this),
  344. //"mouseup": function(e){document.all.testCombox.innerHTML +="<br>"+this.value+"mouseup"; this.edit(); e.stopPropagation();}.bind(this),
  345. "focus": function(e){e.stopPropagation();}
  346. });
  347. this.checkData(function(){
  348. if (this.options.noDataColor) if (!this.data) this.node.setStyle("color", "#bd0000");
  349. this.combox.fireEvent("commitInput", [this]);
  350. this.combox.fireEvent("change", [this]);
  351. this.fireEvent("load");
  352. }.bind(this));
  353. },
  354. edit: function(where){
  355. //this.combox.commitInput();
  356. if (!this.input){
  357. this.input = new o2.widget.Combox.Input(this.combox, this, this.value);
  358. this.input.node.inject(this.node, "after");
  359. this.input.hide();
  360. }
  361. this.input.node.show();
  362. this.input.setInputNodeStyles();
  363. this.node.hide();
  364. this.input.setInputPosition(where);
  365. this.combox.editItem = this;
  366. if(this.combox.input)this.combox.input.hide();
  367. this.input.searchItems();
  368. },
  369. isChanged: function(oldValues){
  370. var values = (this.combox.values||[]).map(function(v){ return v.data || v.value});
  371. if( o2.typeOf(values) !== o2.typeOf(oldValues) )return true;
  372. if( values.length !== oldValues.length )return true;
  373. for( var i=0; i<values.length; i++ ){
  374. if( values[i] !== oldValues[i] )return true;
  375. }
  376. return false;
  377. },
  378. commitInput: function(data){
  379. var oldValues = (this.combox.values||[]).map(function(v){ return v.data || v.value});
  380. var valueStr = this.input.node.get("value");
  381. if (valueStr){
  382. var values = valueStr.split(this.combox.splitRegExp);
  383. if (values.length>1){
  384. this.input.node.set("value", "");
  385. this.combox.editItem = null;
  386. var combox = this.combox;
  387. this.combox.deleteItem(this);
  388. //combox.intoEdit(this);
  389. combox.input.node.set("value", valueStr);
  390. combox.commitInput();
  391. }else{
  392. if (this.value==valueStr){
  393. this.data = data || this.data;
  394. }else{
  395. this.value = valueStr;
  396. this.data = data || null;
  397. }
  398. if (!this.data){
  399. if (this.options.noDataColor) this.node.setStyle("color", "#bd0000");
  400. }else{
  401. this.node.setStyle("color", "");
  402. }
  403. if (this.value){
  404. if (this.getNextItem()){
  405. this.node.set("text", this.value+this.combox.options.splitShow);
  406. }else{
  407. this.node.set("text", this.value);
  408. }
  409. this.node.show();
  410. this.input.hide();
  411. this.combox.editItem = null;
  412. this.combox.fireEvent("commitInput", [this]);
  413. if( !this.isChanged || this.isChanged(oldValues) ) this.combox.fireEvent("change", [this, oldValues]);
  414. }else{
  415. this.combox.editItem = null;
  416. var combox = this.combox;
  417. this.input.hideOptionList();
  418. this.combox.deleteItem(this);
  419. if( !this.isChanged || this.isChanged(oldValues) )combox.fireEvent("change", [this, oldValues]);
  420. }
  421. }
  422. }else{
  423. this.combox.editItem = null;
  424. var combox = this.combox;
  425. this.input.hideOptionList();
  426. this.combox.deleteItem(this);
  427. if( !this.isChanged || this.isChanged(oldValues) )combox.fireEvent("change", [this]);
  428. }
  429. window.setTimeout(function(){
  430. // if (this.combox.input){
  431. // this.combox.input.show();
  432. // this.combox.input.node.blur();
  433. // this.combox.input.node.focus();
  434. // }else{
  435. this.combox.intoEdit(this);
  436. // }
  437. }.bind(this), 10);
  438. },
  439. getNextItem: function(){
  440. var node = (this.input) ? this.input.node.getNext() : this.node.getNext();
  441. while (node && !node.retrieve("item")){ node = node.getNext(); }
  442. if (node) return node.retrieve("item");
  443. return null;
  444. },
  445. getPreviousItem: function(){
  446. var node = this.node.getPrevious();
  447. while (node && !node.retrieve("item")){ node = node.getPrevious(); }
  448. if (node) return node.retrieve("item");
  449. return null;
  450. }
  451. });
  452. o2.widget.Combox.Input = new Class({
  453. initialize: function(combox, bind, value){
  454. this.combox = combox;
  455. this.bind = bind;
  456. this.css = this.combox.css;
  457. this.node = new Element("input", {"styles": this.css.inputNode, "type":"text", "value": value});
  458. if (this.combox.options.onlySelect) this.node.set("readonly", true);
  459. this.setInputNodeStyles();
  460. this.setInputWidth();
  461. this.setEvent();
  462. this.hideOption = false;
  463. if(this.combox.options.positionX === "right")this.node.setStyle("float","right");
  464. },
  465. hide: function(){
  466. this.node.hide();
  467. this.hideOptionList();
  468. },
  469. setEvent: function(){
  470. this.node.addEvents({
  471. "mousedown": function(e){e.stopPropagation();},
  472. "input": function(e){
  473. this.setInputWidth();
  474. this.searchItems();
  475. var v = this.node.get("value");
  476. var s = v.substr(v.length-1, 1);
  477. if (s=="," || s==";"){
  478. this.node.set("value", v.substr(0, v.length-1));
  479. if (this.optionListNode && this.optionListNode.getChildren().length===1){
  480. this.confirmValue();
  481. }else{
  482. this.bind.commitInput();
  483. }
  484. }
  485. }.bind(this),
  486. "keydown": function(e){
  487. //if (e.code===186 || e.code===188 || e.code===13 || e.event.key==="," || e.event.key===";" || e.event.code==="Semicolon" || e.event.code==="Comma"){
  488. if (e.code===186 || e.code===188 || e.code===13){
  489. if (e.code===13){
  490. this.confirmValue();
  491. }else{
  492. if (this.optionListNode && this.optionListNode.getChildren().length===1){
  493. this.confirmValue();
  494. }else{
  495. this.bind.commitInput();
  496. }
  497. }
  498. e.preventDefault();
  499. e.stopPropagation();
  500. }
  501. if (e.code===37){ //left
  502. if (this.node.selectionStart==0 && this.node.selectionEnd==0){
  503. var item = this.bind.getPreviousItem();
  504. if (item){
  505. this.bind.commitInput();
  506. item.edit();
  507. }
  508. e.preventDefault();
  509. e.stopPropagation();
  510. }
  511. }
  512. if (e.code===39){ //right
  513. var idx = this.node.get("value").length;
  514. if (this.node.selectionStart==idx && this.node.selectionEnd==idx){
  515. var item = this.bind.getNextItem();
  516. if (item){
  517. this.bind.commitInput();
  518. item.edit("start");
  519. }
  520. e.preventDefault();
  521. e.stopPropagation();
  522. }
  523. }
  524. if (e.code===8){ //backspace
  525. if (this.node.selectionStart==0 && this.node.selectionEnd==0){
  526. var item = this.bind.getPreviousItem();
  527. if (item){
  528. this.bind.commitInput();
  529. item.edit();
  530. item.input.setInputPosition();
  531. }
  532. e.preventDefault();
  533. e.stopPropagation();
  534. }
  535. }
  536. if (e.code===46){ //del
  537. var idx = this.node.get("value").length;
  538. if (this.node.selectionStart==idx && this.node.selectionEnd==idx){
  539. var item = this.bind.getNextItem();
  540. if (item){
  541. this.bind.commitInput();
  542. item.edit();
  543. item.input.setInputPosition("start");
  544. }
  545. e.preventDefault();
  546. e.stopPropagation();
  547. }
  548. }
  549. if (e.code===38){ //up
  550. this.selectPrevOption();
  551. }
  552. if (e.code===40){ //down
  553. this.selectNextOption();
  554. }
  555. }.bind(this),
  556. "blur": function(e){
  557. if (this.combox){
  558. if ((this.combox.editItem == this.bind) || (!this.combox.editItem)){
  559. this.bind.commitInput();
  560. }
  561. this.hideOptionList();
  562. }
  563. e.stopPropagation();
  564. //}
  565. }.bind(this)
  566. });
  567. },
  568. setSelectedOption: function(node){
  569. var styles = node.getStyles("background-color", "background", "color");
  570. node.store("originalStyle", styles);
  571. node.setStyles(this.css.optionNode_selected);
  572. this.selectedOption = node;
  573. },
  574. selectPrevOption: function(){
  575. if (this.optionListNode){
  576. if (this.selectedOption) this.selectedOption.setStyles(this.selectedOption.retrieve("originalStyle"));
  577. node = (this.selectedOption)? (this.selectedOption.getPrevious() || this.optionListNode.getLast()) : this.optionListNode.getLast();
  578. if (node)this.setSelectedOption(node);
  579. this.optionListNode.scrollToNode(node, "top");
  580. }
  581. },
  582. selectNextOption: function(){
  583. if (this.optionListNode){
  584. if (this.selectedOption) this.selectedOption.setStyles(this.selectedOption.retrieve("originalStyle"));
  585. node = (this.selectedOption)? (this.selectedOption.getNext() || this.optionListNode.getFirst()) : this.optionListNode.getFirst();
  586. if (node)this.setSelectedOption(node);
  587. this.optionListNode.scrollToNode(node, "bottom");
  588. }
  589. },
  590. selectOption: function(node){
  591. if (this.optionListNode){
  592. if (this.selectedOption) this.selectedOption.setStyles(this.selectedOption.retrieve("originalStyle"));
  593. if (node)this.setSelectedOption(node);
  594. }
  595. },
  596. confirmValue: function(){
  597. if (this.optionListNode){
  598. if (this.selectedOption){
  599. var data = this.selectedOption.retrieve("data");
  600. var text = this.selectedOption.get("text");
  601. this.node.set("value", text);
  602. this.setInputWidth();
  603. this.bind.commitInput(data);
  604. this.selectedOption = null;
  605. }else{
  606. this.bind.commitInput();
  607. }
  608. }else{
  609. this.bind.commitInput();
  610. }
  611. },
  612. setInputWidth: function(){
  613. if (this.node){
  614. if( this.combox.options.positionX === "right" ){
  615. this.node.setStyle("width", "auto");
  616. this.node.focus();
  617. }else{
  618. var value = this.node.get("value");
  619. if (!this.tmpDivNode) this.tmpDivNode = new Element("div").set("style", this.node.get("style")).setStyles({"display": "none", "float": "left", "width": "auto"}).inject(document.body);
  620. this.tmpDivNode.empty();
  621. value = value.replace(/\s/g, "&nbsp");
  622. this.tmpDivNode.set("html", value);
  623. var size = this.tmpDivNode.getComputedSize();
  624. var x = size.width-size.computedLeft-size.computedRight+5;
  625. var nodeSize = this.combox.node.getComputedSize();
  626. if (x<1) x=1;
  627. if (x>nodeSize.width) x = nodeSize.width;
  628. this.node.setStyle("width", ""+x+"px");
  629. this.node.focus();
  630. }
  631. }
  632. },
  633. setInputPosition: function(where){
  634. this.node.focus();
  635. if (where==="start"){
  636. this.node.setSelectionRange(0,0);
  637. }else{
  638. var idx = this.node.get("value").length;
  639. this.node.setSelectionRange(idx,idx);
  640. }
  641. },
  642. setInputNodeStyles: function(){
  643. if (this.node){
  644. var styles = this.combox.node.getStyles("font-family", "min-height", "font-size", "font-weight", "font-variant", "font-style", "line-height", "color", "text-align");
  645. if (!this.inputHeight){
  646. if(layout.mobile){
  647. }else{
  648. var size = this.combox.node.getComputedSize();
  649. this.inputHeight = ""+size.height+"px";
  650. styles.height = ""+size.height+"px";
  651. }
  652. }
  653. this.node.setStyles(styles);
  654. }
  655. },
  656. destroy: function(){
  657. this.node.destroy();
  658. o2.release(this);
  659. },
  660. searchItems: function(){
  661. this.hideOption = true;
  662. var value = this.node.get("value");
  663. if (value || this.combox.options.focusList){
  664. this.combox.getSelectList(value, function(list){
  665. if (this.hideOption){
  666. if (list.length){
  667. this.showOptionList(list);
  668. }else{
  669. this.hideOptionList();
  670. }
  671. }
  672. }.bind(this));
  673. //var list = this.combox.getSelectList(value);
  674. }else{
  675. this.hideOptionList();
  676. }
  677. },
  678. createOptionListNode: function(){
  679. // this.relativeOptionListLocation = new Element("div", {"styles": this.css.relativeOptionListLocation});
  680. // this.relativeOptionListLocation.inject(this.node, "after");
  681. this.optionListNode = new Element("div", {"styles": this.css.optionListNode});
  682. this.optionListNode.inject(this.node, "after");
  683. this.optionListNode.addEvents({
  684. "mousedown": function(e){
  685. this.noBlur = true;
  686. e.preventDefault();
  687. e.stopPropagation();
  688. }.bind(this),
  689. "mouseup": function(e){
  690. this.node.focus();
  691. this.noBlur = false;
  692. }.bind(this)
  693. });
  694. },
  695. showOptionList: function(list){
  696. if (!this.optionListNode) this.createOptionListNode();
  697. this.optionListNode.setStyle("dispaly", "block");
  698. this.optionListNode.empty();
  699. var _self = this;
  700. var styles = this.combox.node.getStyles("font-family", "font-size", "font-weight", "font-variant", "font-style", "line-height", "color", "text-align");
  701. list.each(function(option){
  702. var optionNode = new Element("div", {"styles": this.css.optionNode, "text": option.text}).inject(this.optionListNode);
  703. optionNode.store("data", option.value);
  704. optionNode.setStyles(styles);
  705. optionNode.addEvents({
  706. "mousedown": function(){
  707. // if (_self.bind.edit){
  708. // _self.bind.edit();
  709. // }else{
  710. // _self.bind.values[_self.bind.values.length-1].edit();
  711. // }
  712. _self.confirmValue();
  713. },
  714. "mouseover": function(){_self.selectOption(this);}
  715. });
  716. }.bind(this));
  717. var node = this.optionListNode.getFirst();
  718. if (node){
  719. var styles = node.getStyles("background-color", "background", "color");
  720. node.store("originalStyle", styles);
  721. node.setStyles(this.css.optionNode_selected);
  722. this.selectedOption = node;
  723. }
  724. this.optionListNode.position({
  725. "relativeTo": this.node,
  726. "position": this.combox.options.positionX === "right" ? "rightBottom" : "leftBottom",
  727. "edge": this.combox.options.positionX === "right" ? "rightTop" : "leftTop",
  728. "offset": {"y": 3}
  729. });
  730. var pNode = this.optionListNode.getOffsetParent();
  731. var p = this.optionListNode.getPosition(pNode);
  732. var s = this.optionListNode.getSize();
  733. var ps = pNode.getSize();
  734. //var ss = pNode.getScroll();
  735. if (p.y+s.y>ps.y){
  736. this.optionListNode.position({
  737. "relativeTo": this.node,
  738. "position": this.combox.options.positionX === "right" ? "rightTop" : "leftTop",
  739. "edge": this.combox.options.positionX === "right" ? "rightBottom" : "leftBottom",
  740. "offset": {"y": 3}
  741. });
  742. }
  743. var p = this.optionListNode.getPosition(pNode);
  744. if (p.y<10){
  745. var top = this.optionListNode.getStyle("top").toInt();
  746. top = top-p.y+10;
  747. this.optionListNode.setStyle("top", ""+top+"px");
  748. }
  749. if (layout.desktop.offices){
  750. Object.each(layout.desktop.offices, function(office){
  751. if (this.optionListNode.isOverlap(office.officeNode)){
  752. office.hide();
  753. }
  754. }.bind(this));
  755. }
  756. },
  757. hideOptionList: function(){
  758. if (this.optionListNode){
  759. this.optionListNode.destroy();
  760. this.optionListNode = null;
  761. }
  762. this.hideOption = false;
  763. if (layout.desktop.offices){
  764. Object.each(layout.desktop.offices, function(office){
  765. if (layout.desktop.currentApp && layout.desktop.currentApp.appId===office.form.app.appId){
  766. var display = office.officeNode.retrieve("officeDisplay");
  767. if (display) office.officeNode.setStyle("display", display);
  768. }
  769. });
  770. }
  771. }
  772. });