035f7d8479049cf8eb7637f8c7ad4372247feba1.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. if (!dojo._hasResource["dijit.form.FilteringSelect"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dijit.form.FilteringSelect"] = true;
  7. dojo.provide("dijit.form.FilteringSelect");
  8. dojo.require("dijit.form.ComboBox");
  9. dojo.declare("dijit.form.FilteringSelect", [dijit.form.MappedTextBox,
  10. dijit.form.ComboBoxMixin], {
  11. /*
  12. * summary Enhanced version of HTML's <select> tag.
  13. *
  14. * Similar features: - There is a drop down list of possible
  15. * values. - You can only enter a value from the drop down list.
  16. * (You can't enter an arbitrary value.) - The value submitted
  17. * with the form is the hidden value (ex: CA), not the displayed
  18. * value a.k.a. label (ex: California)
  19. *
  20. * Enhancements over plain HTML version: - If you type in some
  21. * text then it will filter down the list of possible values in
  22. * the drop down list. - List can be specified either as a
  23. * static list or via a javascript function (that can get the
  24. * list from a server)
  25. */
  26. // searchAttr: String
  27. // Searches pattern match against this field
  28. // labelAttr: String
  29. // Optional. The text that actually appears in the drop down.
  30. // If not specified, the searchAttr text is used instead.
  31. labelAttr : "",
  32. // labelType: String
  33. // "html" or "text"
  34. labelType : "text",
  35. _isvalid : true,
  36. isValid : function() {
  37. return this._isvalid;
  38. },
  39. _callbackSetLabel : function(/* Array */result, /* Object */
  40. dataObject, /* Boolean, optional */priorityChange) {
  41. // summary
  42. // Callback function that dynamically sets the label of the
  43. // ComboBox
  44. // setValue does a synchronous lookup,
  45. // so it calls _callbackSetLabel directly,
  46. // and so does not pass dataObject
  47. // dataObject==null means do not test the lastQuery, just
  48. // continue
  49. if (dataObject
  50. && dataObject.query[this.searchAttr] != this._lastQuery) {
  51. return;
  52. }
  53. if (!result.length) {
  54. // #3268: do nothing on bad input
  55. // this._setValue("", "");
  56. // #3285: change CSS to indicate error
  57. if (!this._hasFocus) {
  58. this.valueNode.value = "";
  59. }
  60. dijit.form.TextBox.superclass.setValue.call(this,
  61. undefined, !this._hasFocus);
  62. this._isvalid = false;
  63. this.validate(this._hasFocus);
  64. } else {
  65. this._setValueFromItem(result[0], priorityChange);
  66. }
  67. },
  68. _openResultList : function(/* Object */results, /* Object */
  69. dataObject) {
  70. // #3285: tap into search callback to see if user's query
  71. // resembles a match
  72. if (dataObject.query[this.searchAttr] != this._lastQuery) {
  73. return;
  74. }
  75. this._isvalid = results.length != 0;
  76. this.validate(true);
  77. dijit.form.ComboBoxMixin.prototype._openResultList.apply(
  78. this, arguments);
  79. },
  80. getValue : function() {
  81. // don't get the textbox value but rather the previously set
  82. // hidden value
  83. return this.valueNode.value;
  84. },
  85. _getValueField : function() {
  86. // used for option tag selects
  87. return "value";
  88. },
  89. _setValue : function(/* String */value, /* String */
  90. displayedValue, /* Boolean, optional */priorityChange) {
  91. this.valueNode.value = value;
  92. dijit.form.FilteringSelect.superclass.setValue.call(this,
  93. value, priorityChange, displayedValue);
  94. this._lastDisplayedValue = displayedValue;
  95. },
  96. setValue : function(/* String */value, /* Boolean, optional */
  97. priorityChange) {
  98. // summary
  99. // Sets the value of the select.
  100. // Also sets the label to the corresponding value by reverse
  101. // lookup.
  102. // #3347: fetchItemByIdentity if no keyAttr specified
  103. var self = this;
  104. var handleFetchByIdentity = function(item, priorityChange) {
  105. if (item) {
  106. if (self.store.isItemLoaded(item)) {
  107. self._callbackSetLabel([item], undefined,
  108. priorityChange);
  109. } else {
  110. self.store.loadItem({
  111. item : item,
  112. onItem : function(result,
  113. dataObject) {
  114. self._callbackSetLabel(result,
  115. dataObject,
  116. priorityChange)
  117. }
  118. });
  119. }
  120. } else {
  121. self._isvalid = false;
  122. // prevent errors from Tooltip not being created yet
  123. self.validate(false);
  124. }
  125. }
  126. this.store.fetchItemByIdentity({
  127. identity : value,
  128. onItem : function(item) {
  129. handleFetchByIdentity(item, priorityChange)
  130. }
  131. });
  132. },
  133. _setValueFromItem : function(/* item */item, /* Boolean, optional */
  134. priorityChange) {
  135. // summary
  136. // Set the displayed valued in the input box, based on a
  137. // selected item.
  138. // Users shouldn't call this function; they should be
  139. // calling setDisplayedValue() instead
  140. this._isvalid = true;
  141. this._setValue(this.store.getIdentity(item), this
  142. .labelFunc(item, this.store),
  143. priorityChange);
  144. },
  145. labelFunc : function(/* item */item, /* dojo.data.store */store) {
  146. // summary: Event handler called when the label changes
  147. // returns the label that the ComboBox should display
  148. return store.getValue(item, this.searchAttr);
  149. },
  150. onkeyup : function(/* Event */evt) {
  151. // summary: internal function
  152. // FilteringSelect needs to wait for the complete label
  153. // before committing to a reverse lookup
  154. // this.setDisplayedValue(this.textbox.value);
  155. },
  156. _doSelect : function(/* Event */tgt) {
  157. // summary:
  158. // ComboBox's menu callback function
  159. // FilteringSelect overrides this to set both the visible
  160. // and hidden value from the information stored in the menu
  161. this.item = tgt.item;
  162. this._setValueFromItem(tgt.item, true);
  163. },
  164. setDisplayedValue : function(/* String */label) {
  165. // summary:
  166. // Set textbox to display label
  167. // Also performs reverse lookup to set the hidden value
  168. // Used in InlineEditBox
  169. if (this.store) {
  170. var query = {};
  171. this._lastQuery = query[this.searchAttr] = label;
  172. // if the label is not valid, the callback will never
  173. // set it,
  174. // so the last valid value will get the warning textbox
  175. // set the textbox value now so that the impending
  176. // warning will make sense to the user
  177. this.textbox.value = label;
  178. this._lastDisplayedValue = label;
  179. this.store.fetch({
  180. query : query,
  181. queryOptions : {
  182. ignoreCase : this.ignoreCase,
  183. deep : true
  184. },
  185. onComplete : dojo.hitch(this,
  186. this._callbackSetLabel)
  187. });
  188. }
  189. },
  190. _getMenuLabelFromItem : function(/* Item */item) {
  191. // internal function to help ComboBoxMenu figure out what to
  192. // display
  193. if (this.labelAttr) {
  194. return {
  195. html : this.labelType == "html",
  196. label : this.store.getValue(item, this.labelAttr)
  197. };
  198. } else {
  199. // because this function is called by ComboBoxMenu,
  200. // this.inherited tries to find the superclass of
  201. // ComboBoxMenu
  202. return dijit.form.ComboBoxMixin.prototype._getMenuLabelFromItem
  203. .apply(this, arguments);
  204. }
  205. },
  206. postMixInProperties : function() {
  207. dijit.form.ComboBoxMixin.prototype.postMixInProperties
  208. .apply(this, arguments);
  209. dijit.form.MappedTextBox.prototype.postMixInProperties
  210. .apply(this, arguments);
  211. }
  212. });
  213. }