aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/token-field/token-field.reel/token-field.js
blob: 6eb35a8077c7e6731c59a81105c16bb44b4cc4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
var Montage = require("montage").Montage,
    Component = require("ui/component").Component;

var KEY_DELETE = 46,
KEY_BACKSPACE = 8,
KEY_LEFT = 37,
KEY_UP = 38,
KEY_RIGHT = 39,
KEY_DOWN = 40;

exports.TokenField = Montage.create(Component, {

    delegate: {value: null, serializable: true},

    values: {value: null, serializable: true},

    /**
    * Path to a String within an Object that is representative of the Object
    */
    textPropertyPath: {value: null, serializable: true},

    /**
    * Allow ad-hoc strings (strings that do not have corresponding represented object) to be entered.
    */
    allowAdHocValues: {value: null, serializable: true},

    placeholder: {value: null, serializable: true},


    // private

    __hasFocus: {value: null},
    _hasFocus: {
        get: function() {
            return this.__hasFocus;
        },
        set: function(value) {
            if(value != this.__hasFocus) {
                this.__hasFocus = value;
                this.needsDraw = true;
            }
        }
    },

    _tokensController: {value: null, serializable: true},
    _tokenList: {value: null, serializable: true},
    _autocomplete: {value: null, serializable: true},
    __autocompleteValue: {value: null},
    _autocompleteValue: {
        serializable: true,
        get: function() {
            return this.__autocompleteValue;
        },
        set: function(value) {
            this.__autocompleteValue = value;
        }
    },

    __suggestedValue: {value: null},
    _suggestedValue: {
        serializable: true,
        get: function() {
            return this.__suggestedValue;
        },
        set: function(newValue) {
            if(newValue) {
                var representedObject;
                if(!this.allowAdHocValues && String.isString(newValue)) {
                    // since ad-hoc values are not allowed, check with the delegate
                    // if a representedObject can be found for this string
                    representedObject = this.callDelegateMethod('getRepresentedObject', newValue);
                } else {
                    representedObject = newValue;
                }
                if(representedObject) {
                    this.__suggestedValue = representedObject;
                    // able to find a representedObject
                    if(!this.values) {
                        this.values = [];
                    }
                    this.values.push(this.__suggestedValue);
                    this._autocomplete.value = '';
                }
                // nullify the value as autocomplete.value is empty
                this.__suggestedValue = null;

            }
        }
    },

    prepareForActivationEvents: {
        value: function() {
            this.element.addEventListener('mouseup', this);
        }
    },

    prepareForDraw: {
        value: function() {
            this._autocomplete.delegate = this.delegate;
            if(this.identifier) {
                this._autocomplete.identifier = this.identifier;
                // @todo : this might be a problem. Since delegate methods are prefixed with
                // the identifier
                //this.identifier = 'token-field-' + this.identifier;
            }
            this._autocomplete.element.addEventListener("keyup", this);
        }
    },

    draw: {
        value: function() {
            if(this._hasFocus) {
                this._autocomplete.element.focus();
                this.__hasFocus = false;
            } else {
                if(this.placeholder) {
                    this._autocomplete.element.style.width = 'auto';
                }
            }
        }
    },

    // Event handling
    handleMouseup: {
        value: function(event) {
            this._hasFocus = true;
        }
    },

    handleKeyup: {
        value: function(e) {
            var code = e.keyCode;
            //console.log('keyCode', code);
            if(this.values && this.values.length > 0) {
                var selectedIndexes = this._tokensController.selectedIndexes;
                var selectedIndex = (selectedIndexes && selectedIndexes.length > 0 ? selectedIndexes[0] : null);
                var lastIndex = this.values.length - 1, len = this.values.length;

                switch(code) {
                    // @todo - check Keycode in Windows/Linux/Mobile browsers
                    case KEY_BACKSPACE:
                    case KEY_DELETE:
                    // Only remove the token if the token has already been selected
                    // So the behavior is to select the last token if it is not selected already.
                    // If selected already, then remove it

                    if(!this._autocompleteValue) {
                        // check if the selected token is the last one
                        if(selectedIndexes && selectedIndexes.length > 0) {
                            // removes the selected one
                            this._tokensController.removeObjectsAtSelectedIndexes();
                            this._tokensController.selectedIndexes = [];
                        } else {
                            this._tokensController.selectedIndexes = [this.values.length-1];
                        }
                    }

                    break;

                    case KEY_LEFT:
                        if(!this._autocompleteValue) {
                            if(selectedIndex != null) {
                                selectedIndex = selectedIndex - 1;
                                if(selectedIndex < 0) {
                                    selectedIndex = lastIndex;
                                }
                            } else {
                                selectedIndex = lastIndex;
                            }
                            this._tokensController.selectedIndexes = [selectedIndex];
                        }

                    break;

                    case KEY_RIGHT:
                    if(!this._autocompleteValue) {
                        if(selectedIndex != null) {
                            selectedIndex = selectedIndex + 1;
                            if(selectedIndex > lastIndex) {
                                selectedIndex = 0;
                            }
                        } else {
                            selectedIndex = 0;
                        }
                        this._tokensController.selectedIndexes = [selectedIndex];
                    }

                    break;


                    case KEY_UP:
                        if(selectedIndex != null) {
                            this._tokensController.selectedIndexes = [0];
                        }

                    break;

                    case KEY_DOWN:
                        if(selectedIndex != null) {
                            this._tokensController.selectedIndexes = [lastIndex];
                        }

                    break;


                    default:
                        this._tokensController.selectedIndexes = [];
                    break;

                }
            }

        }

    }




});