Statistics
| Branch: | Revision:

blinker / firefox.plugin / data / scripts / text / keyboard.js @ master

History | View | Annotate | Download (124.321 KB)

1 76dd22bd KevinTaron
/*! jQuery UI Virtual Keyboard v1.26.4 *//*
2
Author: Jeremy Satterfield
3
Maintained: Rob Garrison (Mottie on github)
4
Licensed under the MIT License
5

6
An on-screen virtual keyboard embedded within the browser window which
7
will popup when a specified entry field is focused. The user can then
8
type and preview their input before Accepting or Canceling.
9

10
This plugin adds default class names to match jQuery UI theme styling.
11
Bootstrap & custom themes may also be applied - See
12
https://github.com/Mottie/Keyboard#themes
13

14
Requires:
15
    jQuery v1.4.3+
16
    Caret plugin (included)
17
Optional:
18
    jQuery UI (position utility only) & CSS theme
19
    jQuery mousewheel
20

21
Setup/Usage:
22
    Please refer to https://github.com/Mottie/Keyboard/wiki
23

24
-----------------------------------------
25
Caret code modified from jquery.caret.1.02.js
26
Licensed under the MIT License:
27
http://www.opensource.org/licenses/mit-license.php
28
-----------------------------------------
29
*/
30
/*jshint browser:true, jquery:true, unused:false */
31
/*global require:false, define:false, module:false */
32
;(function (factory) {
33
    if (typeof define === 'function' && define.amd) {
34
        define(['jquery'], factory);
35
    } else if (typeof module === 'object' && typeof module.exports === 'object') {
36
        module.exports = factory(require('jquery'));
37
    } else {
38
        factory(jQuery);
39
    }
40
}(function ($) {
41
    'use strict';
42
    var $keyboard = $.keyboard = function (el, options) {
43
    var o, base = this;
44
45
    base.version = '1.26.4';
46
47
    // Access to jQuery and DOM versions of element
48
    base.$el = $(el);
49
    base.el = el;
50
51
    // Add a reverse reference to the DOM object
52
    base.$el.data('keyboard', base);
53
54
    base.init = function () {
55
        var k, position, tmp,
56
            kbcss = $keyboard.css,
57
            kbevents = $keyboard.events;
58
        base.settings = options || {};
59
        // shallow copy position to prevent performance issues; see #357
60
        if (options && options.position) {
61
            position = $.extend({}, options.position);
62
            options.position = null;
63
        }
64
        base.options = o = $.extend(true, {}, $keyboard.defaultOptions, options);
65
        if (position) {
66
            o.position = position;
67
            options.position = position;
68
        }
69
70
        // keyboard is active (not destroyed);
71
        base.el.active = true;
72
        // unique keyboard namespace
73
        base.namespace = '.keyboard' + Math.random().toString(16).slice(2);
74
        // extension namespaces added here (to unbind listeners on base.$el upon destroy)
75
        base.extensionNamespace = [];
76
        // Shift and Alt key toggles, sets is true if a layout has more than one keyset
77
        // used for mousewheel message
78
        base.shiftActive = base.altActive = base.metaActive = base.sets = base.capsLock = false;
79
        // Class names of the basic key set - meta keysets are handled by the keyname
80
        base.rows = ['', '-shift', '-alt', '-alt-shift'];
81
82
        base.inPlaceholder = base.$el.attr('placeholder') || '';
83
        // html 5 placeholder/watermark
84
        base.watermark = $keyboard.watermark && base.inPlaceholder !== '';
85
        // convert mouse repeater rate (characters per second) into a time in milliseconds.
86
        base.repeatTime = 1000 / (o.repeatRate || 20);
87
        // delay in ms to prevent mousedown & touchstart from both firing events at the same time
88
        o.preventDoubleEventTime = o.preventDoubleEventTime || 100;
89
        // flag indication that a keyboard is open
90
        base.isOpen = false;
91
        // is mousewheel plugin loaded?
92
        base.wheel = $.isFunction($.fn.mousewheel);
93
        // special character in regex that need to be escaped
94
        base.escapeRegex = /[-\/\\^$*+?.()|[\]{}]/g;
95
96
        // keyCode of keys always allowed to be typed
97
        k = $keyboard.keyCodes;
98
        // base.alwaysAllowed = [20,33,34,35,36,37,38,39,40,45,46];
99
        base.alwaysAllowed = [
100
            k.capsLock,
101
            k.pageUp,
102
            k.pageDown,
103
            k.end,
104
            k.home,
105
            k.left,
106
            k.up,
107
            k.right,
108
            k.down,
109
            k.insert,
110
            k.delete
111
        ];
112
        base.$keyboard = [];
113
        // keyboard enabled; set to false on destroy
114
        base.enabled = true;
115
        // make a copy of the original keyboard position
116
        if (!$.isEmptyObject(o.position)) {
117
            o.position.orig_at = o.position.at;
118
        }
119
120
        base.checkCaret = (o.lockInput || $keyboard.checkCaretSupport());
121
122
        base.last = {
123
            start: 0,
124
            end: 0,
125
            key: '',
126
            val: '',
127
            preVal: '',
128
            layout: '',
129
            virtual: true,
130
            keyset: [false, false, false], // [shift, alt, meta]
131
            wheel_$Keys: null,
132
            wheelIndex: 0,
133
            wheelLayers: []
134
        };
135
        // used when building the keyboard - [keyset element, row, index]
136
        base.temp = ['', 0, 0];
137
138
        // Callbacks
139
        $.each([
140
            kbevents.kbInit,
141
            kbevents.kbBeforeVisible,
142
            kbevents.kbVisible,
143
            kbevents.kbHidden,
144
            kbevents.inputCanceled,
145
            kbevents.inputAccepted,
146
            kbevents.kbBeforeClose,
147
            kbevents.inputRestricted
148
        ], function (i, callback) {
149
            if ($.isFunction(o[callback])) {
150
                // bind callback functions within options to triggered events
151
                base.$el.bind(callback + base.namespace + 'callbacks', o[callback]);
152
            }
153
        });
154
155
        // Close with esc key & clicking outside
156
        if (o.alwaysOpen) {
157
            o.stayOpen = true;
158
        }
159
160
        tmp = $(document);
161
        if (base.el.ownerDocument !== document) {
162
            tmp = tmp.add(base.el.ownerDocument);
163
        }
164
165
        var bindings = 'keyup checkkeyboard mousedown touchstart ';
166
        if (o.closeByClickEvent) {
167
            bindings += 'click ';
168
        }
169
        tmp.bind(bindings.split(' ').join(base.namespace + ' '), base.checkClose);
170
171
        // Display keyboard on focus
172
        base.$el
173
            .addClass(kbcss.input + ' ' + o.css.input)
174
            .attr({
175
                'aria-haspopup': 'true',
176
                'role': 'textbox'
177
            });
178
179
        // set lockInput if the element is readonly; or make the element readonly if lockInput is set
180
        if (o.lockInput || base.el.readOnly) {
181
            o.lockInput = true;
182
            base.$el
183
                .addClass(kbcss.locked)
184
                .attr({
185
                    'readonly': 'readonly'
186
                });
187
        }
188
        // add disabled/readonly class - dynamically updated on reveal
189
        if (base.$el.is(':disabled') || (base.$el.attr('readonly') &&
190
                !base.$el.hasClass(kbcss.locked))) {
191
            base.$el.addClass(kbcss.noKeyboard);
192
        }
193
194
        if (o.openOn) {
195
            base.bindFocus();
196
        }
197
198
        // Add placeholder if not supported by the browser
199
        if (!base.watermark && base.$el.val() === '' && base.inPlaceholder !== '' &&
200
            base.$el.attr('placeholder') !== '') {
201
            base.$el
202
                .addClass(kbcss.placeholder) // css watermark style (darker text)
203
                .val(base.inPlaceholder);
204
        }
205
206
        base.$el.trigger(kbevents.kbInit, [base, base.el]);
207
208
        // initialized with keyboard open
209
        if (o.alwaysOpen) {
210
            base.reveal();
211
        }
212
213
    };
214
215
    base.toggle = function () {
216
        var $toggle = base.$keyboard.find('.' + $keyboard.css.keyToggle),
217
            locked = !base.enabled;
218
        // prevent physical keyboard from working
219
        base.$preview.prop('readonly', locked || base.options.lockInput);
220
        // disable all buttons
221
        base.$keyboard
222
            .toggleClass($keyboard.css.keyDisabled, locked)
223
            .find('.' + $keyboard.css.keyButton)
224
            .not($toggle)
225
            .prop('disabled', locked)
226
            .attr('aria-disabled', locked);
227
        $toggle.toggleClass($keyboard.css.keyDisabled, locked);
228
        // stop auto typing
229
        if (locked && base.typing_options) {
230
            base.typing_options.text = '';
231
        }
232
    };
233
234
    base.setCurrent = function () {
235
        var kbcss = $keyboard.css,
236