[483] | 1 | define([ |
---|
| 2 | "dojo/_base/declare", |
---|
| 3 | "dojo/_base/lang", |
---|
| 4 | "dojo/_base/window", |
---|
| 5 | "dijit/form/_SearchMixin", |
---|
| 6 | "dojox/mobile/TextBox", |
---|
| 7 | "dojo/dom-class", |
---|
| 8 | "dojo/keys", |
---|
| 9 | "dojo/touch", |
---|
| 10 | "dojo/on", |
---|
| 11 | "./sniff" |
---|
| 12 | ], function(declare, lang, win, SearchMixin, TextBox, domClass, keys, touch, on, has){ |
---|
| 13 | |
---|
| 14 | return declare("dojox.mobile.SearchBox", [TextBox, SearchMixin], { |
---|
| 15 | // summary: |
---|
| 16 | // A non-templated base class for INPUT type="search". |
---|
| 17 | |
---|
| 18 | // baseClass: String |
---|
| 19 | // The name of the CSS class of this widget. |
---|
| 20 | baseClass: "mblTextBox mblSearchBox", |
---|
| 21 | |
---|
| 22 | // type: String |
---|
| 23 | // Corresponds to the type attribute of the HTML `<input>` element. |
---|
| 24 | // The value is "search". |
---|
| 25 | type: "search", |
---|
| 26 | |
---|
| 27 | placeHolder: "", |
---|
| 28 | |
---|
| 29 | // incremental: Boolean |
---|
| 30 | // Set true to search on every key or false to only search after |
---|
| 31 | // pressing ENTER or cancel. |
---|
| 32 | incremental: true, |
---|
| 33 | |
---|
| 34 | _setIncrementalAttr: function(val){ |
---|
| 35 | // summary: |
---|
| 36 | // Custom setter so the INPUT doesn't get the incremental attribute set. |
---|
| 37 | // tags: |
---|
| 38 | // private |
---|
| 39 | this.incremental = val; |
---|
| 40 | }, |
---|
| 41 | |
---|
| 42 | _onInput: function(e){ |
---|
| 43 | // tags: |
---|
| 44 | // private |
---|
| 45 | if(e.charOrCode == keys.ENTER){ |
---|
| 46 | e.charOrCode = 229; |
---|
| 47 | }else if(!this.incremental){ |
---|
| 48 | e.charOrCode = 0; // call _onInput to make sure a pending query is aborted |
---|
| 49 | } |
---|
| 50 | this.inherited(arguments); |
---|
| 51 | }, |
---|
| 52 | |
---|
| 53 | postCreate: function(){ |
---|
| 54 | this.inherited(arguments); |
---|
| 55 | this.textbox.removeAttribute('incremental'); // only want onsearch to fire for ENTER and cancel |
---|
| 56 | if(!this.textbox.hasAttribute('results')){ |
---|
| 57 | this.textbox.setAttribute('results', '0'); // enables webkit search decoration |
---|
| 58 | } |
---|
| 59 | if(has("ios") < 5){ |
---|
| 60 | domClass.add(this.domNode, 'iphone4'); // cannot click cancel button after focus so just remove it |
---|
| 61 | this.connect(this.textbox, "onfocus", // if value changes between start of onfocus to end, then it was a cancel |
---|
| 62 | function(){ |
---|
| 63 | if(this.textbox.value !== ''){ |
---|
| 64 | this.defer( |
---|
| 65 | function(){ |
---|
| 66 | if(this.textbox.value === ''){ |
---|
| 67 | this._onInput({ charOrCode: keys.ENTER }); // emulate onsearch |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | ); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | ); |
---|
| 74 | } |
---|
| 75 | this.connect(this.textbox, "onsearch", |
---|
| 76 | function(){ |
---|
| 77 | this._onInput({ charOrCode: keys.ENTER }); |
---|
| 78 | } |
---|
| 79 | ); |
---|
| 80 | |
---|
| 81 | // Clear action for the close button (iOS specific) |
---|
| 82 | var _this = this; |
---|
| 83 | var touchStartX, touchStartY; |
---|
| 84 | var handleRelease; |
---|
| 85 | if(has("ios")){ |
---|
| 86 | this.on(touch.press, function(evt){ |
---|
| 87 | var rect; |
---|
| 88 | touchStartX = evt.touches ? evt.touches[0].pageX : evt.pageX; |
---|
| 89 | touchStartY = evt.touches ? evt.touches[0].pageY : evt.pageY; |
---|
| 90 | // As the native searchbox on iOS, clear on release, not on start. |
---|
| 91 | handleRelease = on(win.doc, touch.release, |
---|
| 92 | function(evt){ |
---|
| 93 | var rect, dx, dy; |
---|
| 94 | if(_this.get("value") != ""){ |
---|
| 95 | dx = evt.pageX - touchStartX; |
---|
| 96 | dy = evt.pageY - touchStartY; |
---|
| 97 | // Mimic the behavior of native iOS searchbox: |
---|
| 98 | // if location of release event close to the location of start event: |
---|
| 99 | if(Math.abs(dx) <= 4 && Math.abs(dy) <= 4){ |
---|
| 100 | evt.preventDefault(); |
---|
| 101 | _this.set("value", ""); |
---|
| 102 | _this._onInput({ charOrCode: keys.ENTER }); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | if(handleRelease){ // possibly already cancelled/cleared on touch.press |
---|
| 106 | handleRelease.remove(); |
---|
| 107 | handleRelease = null; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | ); |
---|
| 111 | rect = _this.domNode.getBoundingClientRect(); |
---|
| 112 | // if touched in the right-most 20 pixels of the search box |
---|
| 113 | if(rect.right - (evt.touches ? evt.touches[0].pageX : evt.pageX) >= 20){ |
---|
| 114 | // cancel |
---|
| 115 | if(handleRelease){ |
---|
| 116 | handleRelease.remove(); |
---|
| 117 | handleRelease = null; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | }); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | }); |
---|
| 124 | }); |
---|