SearchInput.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. o2.widget = o2.widget || {};
  2. o2.require("o2.widget.Common", null, false);
  3. o2.widget.SearchInput = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. "style": "default",
  8. "width": 800,
  9. "height": 40
  10. },
  11. initialize: function(options){
  12. this.setOptions(options);
  13. this.path = o2.session.path+"/widget/$SearchInput/";
  14. this.cssPath = o2.session.path+"/widget/$SearchInput/"+this.options.style+"/css.wcss";
  15. this._loadCss();
  16. this.load();
  17. },
  18. load: function(){
  19. if (this.fireEvent("queryLoad")){
  20. this.node = new Element("div", {"styles": this.css.searchNode});
  21. this.searchBarNode = new Element("div", {"styles": this.css.searchBarNode}).inject(this.node);
  22. this.searchAction = new Element("div", {"styles": this.css.searchAction}).inject(this.searchBarNode);
  23. this.searchInputArea = new Element("div", {"styles": this.css.searchInputArea}).inject(this.searchBarNode);
  24. this.input = new Element("input", {"type":"text", "styles": this.css.searchInputNode}).inject(this.searchInputArea);
  25. this.copyPrototype();
  26. this.setEvent();
  27. this.fireEvent("postLoad");
  28. }
  29. },
  30. copyPrototype: function(){
  31. this.inject = this.node.inject.bind(this.node);
  32. this.setStyle = this.node.setStyle.bind(this.node);
  33. this.setStyles = this.node.setStyles.bind(this.node);
  34. this.set = this.node.set.bind(this.node);
  35. },
  36. setEvent: function(){
  37. this.node.addEvent("mousedown", function(e){e.stopPropagation();});
  38. this.searchAction.addEvent("click", function(){
  39. this.doSearch();
  40. }.bind(this));
  41. o2.UD.getDataJson("searchKeys", function(json){
  42. this.keys = json || [];
  43. this.input.addEvents({
  44. "input": function(){
  45. if (this.keys && this.keys.length){
  46. var value = this.input.get("value");
  47. var keys = this.keys.filter(function(d){ return d.indexOf(value)!==-1; });
  48. keys = keys.slice(0,9);
  49. this.showAutoCompleted(keys);
  50. }
  51. }.bind(this),
  52. "keydown": function(e){
  53. if (e.code===13){
  54. this.hideAutoCompleted();
  55. this.doSearch();
  56. }
  57. if (e.code===38){ //up
  58. if (this.autoCompletedNode){
  59. var node = (this.currentKey) ? this.currentKey.getPrevious() : this.autoCompletedNode.getLast();
  60. if (!node) node = this.autoCompletedNode.getLast();
  61. if (node){
  62. this.setCurrentKey(node);
  63. this.input.set("value", node.get("text"));
  64. }
  65. }
  66. }
  67. if (e.code===40){ //down
  68. if (this.autoCompletedNode){
  69. var node = (this.currentKey) ? this.currentKey.getNext() : this.autoCompletedNode.getFirst();
  70. if (!node) node = this.autoCompletedNode.getFirst();
  71. if (node){
  72. this.setCurrentKey(node);
  73. this.input.set("value", node.get("text"));
  74. }
  75. }
  76. }
  77. }.bind(this)
  78. });
  79. }.bind(this));
  80. },
  81. doSearch: function(){
  82. var value = this.input.get("value");
  83. if (value){
  84. if (this.keys){
  85. var idx = this.keys.indexOf(value);
  86. if (idx===-1){
  87. if (this.keys.length>=50) this.keys.shift();
  88. }else{
  89. this.keys.splice(idx, 1);
  90. }
  91. this.keys.push(value);
  92. o2.UD.putData("searchKeys", this.keys);
  93. }
  94. this.fireEvent("search", [value]);
  95. }
  96. },
  97. showAutoCompleted: function(keys){
  98. if (keys.length){
  99. var _self = this;
  100. if (!this.autoCompletedNode){
  101. this.autoCompletedNode = new Element("div", {"styles": this.css.autoCompletedNode}).inject(this.node);
  102. var s = this.node.getSize();
  103. this.autoCompletedNode.setStyle("width", ""+s.x+"px");
  104. }
  105. this.autoCompletedNode.empty();
  106. this.autoCompletedNode.show();
  107. this.hideAutoCompletedFun = this.hideAutoCompleted.bind(this);
  108. $(document.body).addEvent("mousedown", this.hideAutoCompletedFun);
  109. keys.each(function(key){
  110. var node = new Element("div", {"styles": this.css.searchKeyItem, "text": key}).inject(this.autoCompletedNode);
  111. node.addEvents({
  112. "mouseover": function(){_self.setCurrentKey(this);},
  113. "mouseout": function(){_self.removeCurrentKey(this);},
  114. "click": function(){
  115. _self.hideAutoCompleted();
  116. _self.input.set("value", this.get("text"));
  117. _self.doSearch();
  118. }
  119. });
  120. }.bind(this));
  121. }else{
  122. this.hideAutoCompleted();
  123. }
  124. },
  125. setCurrentKey: function(node){
  126. if (this.currentKey) this.removeCurrentKey(this.currentKey);
  127. this.currentKey = node;
  128. node.setStyles(this.css.searchKeyItem_over)
  129. },
  130. removeCurrentKey: function(node){
  131. if (this.currentKey==node) this.currentKey = null;
  132. node.setStyles(this.css.searchKeyItem)
  133. },
  134. hideAutoCompleted: function(){
  135. if (this.hideAutoCompletedFun) $(document.body).removeEvent("mousedown", this.hideAutoCompletedFun);
  136. if (this.autoCompletedNode){
  137. this.autoCompletedNode.empty();
  138. this.autoCompletedNode.hide();
  139. }
  140. },
  141. getValue: function(){
  142. return this.input.get("value");
  143. },
  144. setValue: function(value){
  145. this.input.set("value", value);
  146. }
  147. });