aboutsummaryrefslogtreecommitdiff
path: root/js/components/hintable.reel/hintable.js
blob: d09cdbd2153fead0dedf8b04f062a0aeb56df405 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

/* ComputedStyleSubPanel.js */
var Montage   = require("montage").Montage,
    Component = require("montage/ui/component").Component,
    Editable  = require("js/components/editable.reel").Editable;


/*

EDITABLE - Methods
- startEdit
- stopEdit
- value
- 
- _suggest
- _suggestNext
- _suggestPrev
- _clearSuggest
- _accept
- _revert
- _setCaret

*/


exports.Hintable = Montage.create(Editable, {
    inheritsFrom : { value : Editable },
    _matchIndex  : { value : 0 },
    matches      : { value : [] },
    
    _hint : { value : null },
    hint : {
        get : function() {
            return this._hint;
        },
        set : function(hint) {
            hint = hint || '';
            
            ///// Set the hint element's text
            this._getFirstTextNode(this.hintElement).textContent = hint;
            ///// if hintElement was removed from the DOM, the object still
            ///// exists, so it needs to be re-appended
            if(this.hintElement.parentNode === null) {
                this._element.appendChild(this.hintElement);
            }

            this._hint = hint;
        }
    },
    
    _hintElement : { value : null },
    hintElement : {
        get : function() {
            if(!this._hintElement) {
                /// Remove the phantom "<BR>" element that is generated when 
                /// content editable element is empty
                this._children(this._element, function(item) { 
                    return item.nodeName === 'BR';
                }).forEach(function(item) {
                    this._element.removeChild(item);
                }, this);

                this._hintElement = document.createElement('span');
                this._hintElement.classList.add(this.hintClass);
                
                this._element.appendChild(this._hintElement);
            }
            
            return this._hintElement;
        },
        set : function(el) {
            this._hintElement = el;
        }
    },
    
    _getHintDifference : {
        value : function() {
            if(!this.matches[this._matchIndex]) {
                debugger;
            }
            return this.matches[this._matchIndex].substr(this.value.length);
        }
    },
    
    hintNext : {
        value : function(e) {
            if(e) { e.preventDefault(); }
                //console.log('next1');
            
            if(this._matchIndex < this.matches.length - 1) {
                //console.log('next');
                ++this._matchIndex;
                this.hint = this._getHintDifference();
            }
        }
    },
    hintPrev : {
        value : function(e) {
            if(e) { e.preventDefault(); }
                //console.log('prev1');
            if(this._matchIndex !== 0) {
                //console.log('prev');
                --this._matchIndex;
                this.hint = this._getHintDifference();
            }
        }
    },

    accept : {
        value: function(e, preserveCaretPosition) {
            if(e) {
                e.preventDefault();
            }
            var fullText = this._hint;
            this.hint = null;
            this.value += fullText;
            
            if(!preserveCaretPosition) {
                this.setCursor('end');
            }

            this._sendEvent('accept');
        }
    },
    revert : {
        value : function(e, forceRevert) {
            this.hint = null;
            
            if(this.isEditable || forceRevert) {
                /// revert to old value
                this.value = (this._preEditValue);
                this._sendEvent('revert');
                //console.log('reverting');
                
            }
        }
    },

    handleKeydown : {
        value : function handleKeydown(e) {
            var k = e.keyCode,
                isCaretAtEnd, selection, text;
                
            this._super(arguments);
            
			/// Remove the phantom "<BR>" element that is generated when 
			/// content editable element is empty
			this._children(this._element, function(item) { 
			    return item.nodeName === 'BR';
			}).forEach(function(item) {
			    this._element.removeChild(item);
			}, this);
            
            if(k === 39) {
                selection = window.getSelection();
                text = selection.baseNode.textContent;
                isCaretAtEnd = (selection.anchorOffset === text.length);
            }
            
            if(this.hint && isCaretAtEnd) {
                ///// Advance the cursor
                this.hint = this.hint.substr(0, 1);
                this.accept(e);
                this.handleInput();
            }
            
            this._execKeyAction(e);
        }
    },
    ///// Text input has changed values
    handleInput : {
        value : function handleInput(e) {
            this._super(arguments);

            var val = this.value,
                 matches, hint;
            //console.log('val = "' + val + '"');
            //// Handle auto-suggest if configured
            if(this.hints && this.hints.length) {

                if(val.length > 0) { // content is not empty
                    
                    this._matchIndex = 0;                    
                    this.matches = this.hints.filter(function(h) {
                        if(!h) { return false; }
                        return h.indexOf(val) === 0;
                    }).sort();
                    
                    ///// If there are no matches, or the new value doesn't match all the
                    ///// previous matches, then get new list of matches
                    if(!this.matches.length || !this._matchesAll(val)) {
                    }
                    
                    if(this.matches.length) { // match(es) found
                        if(this.matches[this._matchIndex] !== val) {
                            // Suggest the matched hint, subtracting the typed-in string
                            // Only if the hint is not was the user has typed already
                            this.hint = this._getHintDifference();
                        } else {
                            this.hint = null;                            
                        }
                    } else { // no matches found
                        this.hint = null;
                    }
                } else { // no suggestion for empty string
                    this.hint = null;
                }

            }
        }
    },
    handleBackspace : {
        value : function(e) {
            this.matches.length = 0;
        }
    },
    _matchesAll : {
        value : function(value) {
            return this.matches.every(function(match) {
                return match.indexOf(value) === 0;
            }, this);
        }
    },
    _execKeyAction : {
        value : function(e) {
            var key = e.keyCode,
                keys = this.keyActions;
            
            if(this.hint) {
                if( keys.hint.revert.indexOf(key) !== -1 ) { this.revert(e); }
                if( keys.hint.accept.indexOf(key) !== -1 ) { this.accept(e); }
                if( keys.hint.stop.indexOf(key) !== -1 )   { this.stop(e); }
                if( keys.hint.next.indexOf(key) !== -1 )   { this.hintNext(e); }
                if( keys.hint.prev.indexOf(key) !== -1 )   { this.hintPrev(e); }
                if( keys.hint.backsp.indexOf(key) !== -1 )   { this.handleBackspace(e); }
            } else {
                if(keys.noHint.revert.indexOf(key) !== -1) { this.revert(e); }
                if(keys.noHint.stop.indexOf(key) !== -1)   { this.stop(e); }
                //if( keys.hint.next.indexOf(key) !== -1 )   { this.handleDown(e); }
                //if( keys.hint.prev.indexOf(key) !== -1 )   { this.handleUp(e); }
                //if( keys.hint.backsp.indexOf(key) !== -1 )   { this.backspace(e); }
            }
        }
    },
    
    /* --------------- Utils --------------- */
    
    _children : {
        value : function(el, filter) {
            var f = filter || function(item) {
                return item.nodeType === 1;
            };
            return this._toArray(el.childNodes).filter(f);
        }
    },
    _toArray : {
        value : function(arrayLikeObj) {
            return Array.prototype.slice.call(arrayLikeObj);
        }
    },
    _super : {
        value : function(args) {
            this.inheritsFrom[arguments.callee.caller.name].apply(this, args);
        }
    },

    /* --------- CONFIG ---------- */
    hints : {
        value : ['Testing a hint.', 'Testing another hint.', 'Testing the last hint.'],
        distinct: true
    },
    hintClass : {
        value : "hintable-hint"
    },
    keyActions : { 
        value : {
            hint : {
                accept : [9,13,186], // accept hint
                stop   : [27,186],   // stop editing
                next   : [40],       // cycle to next hint
                prev   : [38],       // cycle to prev hint
                revert : [27],       // revert value
                backsp : [8]         // backspace hit
            },
            noHint : {
                stop   : [27,9,13,186],
                next   : [40],
                prev   : [38],
                revert : [27],
                backsp : [8]
            }
        },
        distinct: true
    }
    
});