textext-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in textext-rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2012 Jeff Pollard
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Textext Rails
2
+
3
+ Brings the [jQuery TextExt plugin](http://textextjs.com/) into the Rails asset pipeline. The TextExt asset files are from the TextExt [1.2.0 release](https://github.com/alexgorbatchev/jquery-textext/tree/1.2.0).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'textext-rails'
11
+ ```
12
+
13
+ Then in your `app/assets/javscipts/application.js` file, include the core JS file plus any other TextExt plugins you need.
14
+
15
+ ```javascript
16
+ //= require textext.core
17
+ //= require textext.plugin.tags
18
+ ```
19
+
20
+ Then, add the core CSS plus any plugins you need to your `app/assets/stylesheets/application.css.scss` file:
21
+
22
+ ```css
23
+ *= require textext.core
24
+ *= require textext.plugin.tags
25
+ ```
26
+
27
+ That's it! Please see the [TextExt manual](http://textextjs.com/manual/index.html) and [Github page](https://github.com/alexgorbatchev/jquery-textext) for further usage information.
28
+
29
+ ## License
30
+
31
+ MIT licensed. Please see the LICENSE file for more information.
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ DOWNLOAD_FOLDER = 'https://nodeload.github.com/alexgorbatchev/jquery-textext/tarball/'
4
+ LATEST_VERSION = '1.2.0'
5
+ DOWNLOAD_URL = DOWNLOAD_FOLDER + LATEST_VERSION
6
+
7
+ desc "Sync the files from the upstream release"
8
+ task :sync do
9
+ sh 'mkdir -p sync'
10
+ sh 'mkdir -p vendor/assets'
11
+ sh "curl -0 #{DOWNLOAD_URL} > sync/latest.tar.gz"
12
+ sh 'cd sync && tar -xzvf latest.tar.gz'
13
+
14
+ release_folder = `ls sync | grep alex`.chomp
15
+
16
+ sh 'rm -rf vendor/assets/stylesheets'
17
+ sh 'rm -rf vendor/assets/javascripts'
18
+ sh "mv sync/#{release_folder}/src/css vendor/assets/stylesheets"
19
+ sh "mv sync/#{release_folder}/src/js vendor/assets/javascripts"
20
+ sh 'rm -rf sync'
21
+ end
Binary file
@@ -0,0 +1,2 @@
1
+ require "textext/rails"
2
+ require "textext/rails/version"
@@ -0,0 +1,6 @@
1
+ module Textext
2
+ module Rails
3
+ require 'textext/rails/engine'
4
+ require 'textext/rails/version'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Textext
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Textext
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "textext/rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "textext-rails"
7
+ s.version = Textext::Rails::VERSION
8
+ s.authors = ["Jeff Pollard"]
9
+ s.email = ["jeff.pollard@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Adds the jQuery TextExt plugin to the Rails 3.1 asset pipeline}
12
+ s.description = %q{Adds the jQuery TextExt plugin to the Rails 3.1 asset pipeline}
13
+
14
+ s.rubyforge_project = "textext-rails"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,1613 @@
1
+ /**
2
+ * jQuery TextExt Plugin
3
+ * http://alexgorbatchev.com/textext
4
+ *
5
+ * @version 1.2.0
6
+ * @copyright Copyright (C) 2011 Alex Gorbatchev. All rights reserved.
7
+ * @license MIT License
8
+ */
9
+ (function($, undefined)
10
+ {
11
+ /**
12
+ * TextExt is the main core class which by itself doesn't provide any functionality
13
+ * that is user facing, however it has the underlying mechanics to bring all the
14
+ * plugins together under one roof and make them work with each other or on their
15
+ * own.
16
+ *
17
+ * @author agorbatchev
18
+ * @date 2011/08/19
19
+ * @id TextExt
20
+ */
21
+ function TextExt() {};
22
+
23
+ /**
24
+ * ItemManager is used to seamlessly convert between string that come from the user input to whatever
25
+ * the format the item data is being passed around in. It's used by all plugins that in one way or
26
+ * another operate with items, such as Tags, Filter, Autocomplete and Suggestions. Default implementation
27
+ * works with `String` type.
28
+ *
29
+ * Each instance of `TextExt` creates a new instance of default implementation of `ItemManager`
30
+ * unless `itemManager` option was set to another implementation.
31
+ *
32
+ * To satisfy requirements of managing items of type other than a `String`, different implementation
33
+ * if `ItemManager` should be supplied.
34
+ *
35
+ * If you wish to bring your own implementation, you need to create a new class and implement all the
36
+ * methods that `ItemManager` has. After, you need to supply your pass via the `itemManager` option during
37
+ * initialization like so:
38
+ *
39
+ * $('#input').textext({
40
+ * itemManager : CustomItemManager
41
+ * })
42
+ *
43
+ * @author agorbatchev
44
+ * @date 2011/08/19
45
+ * @id ItemManager
46
+ */
47
+ function ItemManager() {};
48
+
49
+ /**
50
+ * TextExtPlugin is a base class for all plugins. It provides common methods which are reused
51
+ * by majority of plugins.
52
+ *
53
+ * All plugins must register themselves by calling the `$.fn.textext.addPlugin(name, constructor)`
54
+ * function while providing plugin name and constructor. The plugin name is the same name that user
55
+ * will identify the plugin in the `plugins` option when initializing TextExt component and constructor
56
+ * function will create a new instance of the plugin. *Without registering, the core won't
57
+ * be able to see the plugin.*
58
+ *
59
+ * <span class="new label version">new in 1.2.0</span> You can get instance of each plugin from the core
60
+ * via associated function with the same name as the plugin. For example:
61
+ *
62
+ * $('#input').textext()[0].tags()
63
+ * $('#input').textext()[0].autocomplete()
64
+ * ...
65
+ *
66
+ * @author agorbatchev
67
+ * @date 2011/08/19
68
+ * @id TextExtPlugin
69
+ */
70
+ function TextExtPlugin() {};
71
+
72
+ var stringify = (JSON || {}).stringify,
73
+ slice = Array.prototype.slice,
74
+
75
+ UNDEFINED = 'undefined',
76
+
77
+ /**
78
+ * TextExt provides a way to pass in the options to configure the core as well as
79
+ * each plugin that is being currently used. The jQuery exposed plugin `$().textext()`
80
+ * function takes a hash object with key/value set of options. For example:
81
+ *
82
+ * $('textarea').textext({
83
+ * enabled: true
84
+ * })
85
+ *
86
+ * There are multiple ways of passing in the options:
87
+ *
88
+ * 1. Options could be nested multiple levels deep and accessed using all lowercased, dot
89
+ * separated style, eg `foo.bar.world`. The manual is using this style for clarity and
90
+ * consistency. For example:
91
+ *
92
+ * {
93
+ * item: {
94
+ * manager: ...
95
+ * },
96
+ *
97
+ * html: {
98
+ * wrap: ...
99
+ * },
100
+ *
101
+ * autocomplete: {
102
+ * enabled: ...,
103
+ * dropdown: {
104
+ * position: ...
105
+ * }
106
+ * }
107
+ * }
108
+ *
109
+ * 2. Options could be specified using camel cased names in a flat key/value fashion like so:
110
+ *
111
+ * {
112
+ * itemManager: ...,
113
+ * htmlWrap: ...,
114
+ * autocompleteEnabled: ...,
115
+ * autocompleteDropdownPosition: ...
116
+ * }
117
+ *
118
+ * 3. Finally, options could be specified in mixed style. It's important to understand that
119
+ * for each dot separated name, its alternative in camel case is also checked for, eg for
120
+ * `foo.bar.world` it's alternatives could be `fooBarWorld`, `foo.barWorld` or `fooBar.world`,
121
+ * which translates to `{ foo: { bar: { world: ... } } }`, `{ fooBarWorld: ... }`,
122
+ * `{ foo : { barWorld : ... } }` or `{ fooBar: { world: ... } }` respectively. For example:
123
+ *
124
+ * {
125
+ * itemManager : ...,
126
+ * htmlWrap: ...,
127
+ * autocomplete: {
128
+ * enabled: ...,
129
+ * dropdownPosition: ...
130
+ * }
131
+ * }
132
+ *
133
+ * Mixed case is used through out the code, wherever it seems appropriate. However in the code, all option
134
+ * names are specified in the dot notation because it works both ways where as camel case is not
135
+ * being converted to its alternative dot notation.
136
+ *
137
+ * @author agorbatchev
138
+ * @date 2011/08/17
139
+ * @id TextExt.options
140
+ */
141
+
142
+ /**
143
+ * Default instance of `ItemManager` which takes `String` type as default for tags.
144
+ *
145
+ * @name item.manager
146
+ * @default ItemManager
147
+ * @author agorbatchev
148
+ * @date 2011/08/19
149
+ * @id TextExt.options.item.manager
150
+ */
151
+ OPT_ITEM_MANAGER = 'item.manager',
152
+
153
+ /**
154
+ * List of plugins that should be used with the current instance of TextExt. The list could be
155
+ * specified as array of strings or as comma or space separated string.
156
+ *
157
+ * @name plugins
158
+ * @default []
159
+ * @author agorbatchev
160
+ * @date 2011/08/19
161
+ * @id TextExt.options.plugins
162
+ */
163
+ OPT_PLUGINS = 'plugins',
164
+
165
+ /**
166
+ * TextExt allows for overriding of virtually any method that the core or any of its plugins
167
+ * use. This could be accomplished through the use of the `ext` option.
168
+ *
169
+ * It's possible to specifically target the core or any plugin, as well as overwrite all the
170
+ * desired methods everywhere.
171
+ *
172
+ * 1. Targeting the core:
173
+ *
174
+ * ext: {
175
+ * core: {
176
+ * trigger: function()
177
+ * {
178
+ * console.log('TextExt.trigger', arguments);
179
+ * $.fn.textext.TextExt.prototype.trigger.apply(this, arguments);
180
+ * }
181
+ * }
182
+ * }
183
+ *
184
+ * 2. Targeting individual plugins:
185
+ *
186
+ * ext: {
187
+ * tags: {
188
+ * addTags: function(tags)
189
+ * {
190
+ * console.log('TextExtTags.addTags', tags);
191
+ * $.fn.textext.TextExtTags.prototype.addTags.apply(this, arguments);
192
+ * }
193
+ * }
194
+ * }
195
+ *
196
+ * 3. Targeting `ItemManager` instance:
197
+ *
198
+ * ext: {
199
+ * itemManager: {
200
+ * stringToItem: function(str)
201
+ * {
202
+ * console.log('ItemManager.stringToItem', str);
203
+ * return $.fn.textext.ItemManager.prototype.stringToItem.apply(this, arguments);
204
+ * }
205
+ * }
206
+ * }
207
+ *
208
+ * 4. And finally, in edge cases you can extend everything at once:
209
+ *
210
+ * ext: {
211
+ * '*': {
212
+ * fooBar: function() {}
213
+ * }
214
+ * }
215
+ *
216
+ * @name ext
217
+ * @default {}
218
+ * @author agorbatchev
219
+ * @date 2011/08/19
220
+ * @id TextExt.options.ext
221
+ */
222
+ OPT_EXT = 'ext',
223
+
224
+ /**
225
+ * HTML source that is used to generate elements necessary for the core and all other
226
+ * plugins to function.
227
+ *
228
+ * @name html.wrap
229
+ * @default '<div class="text-core"><div class="text-wrap"/></div>'
230
+ * @author agorbatchev
231
+ * @date 2011/08/19
232
+ * @id TextExt.options.html.wrap
233
+ */
234
+ OPT_HTML_WRAP = 'html.wrap',
235
+
236
+ /**
237
+ * HTML source that is used to generate hidden input value of which will be submitted
238
+ * with the HTML form.
239
+ *
240
+ * @name html.hidden
241
+ * @default '<input type="hidden" />'
242
+ * @author agorbatchev
243
+ * @date 2011/08/20
244
+ * @id TextExt.options.html.hidden
245
+ */
246
+ OPT_HTML_HIDDEN = 'html.hidden',
247
+
248
+ /**
249
+ * Hash table of key codes and key names for which special events will be created
250
+ * by the core. For each entry a `[name]KeyDown`, `[name]KeyUp` and `[name]KeyPress` events
251
+ * will be triggered along side with `anyKeyUp` and `anyKeyDown` events for every
252
+ * key stroke.
253
+ *
254
+ * Here's a list of default keys:
255
+ *
256
+ * {
257
+ * 8 : 'backspace',
258
+ * 9 : 'tab',
259
+ * 13 : 'enter!',
260
+ * 27 : 'escape!',
261
+ * 37 : 'left',
262
+ * 38 : 'up!',
263
+ * 39 : 'right',
264
+ * 40 : 'down!',
265
+ * 46 : 'delete',
266
+ * 108 : 'numpadEnter'
267
+ * }
268
+ *
269
+ * Please note the `!` at the end of some keys. This tells the core that by default
270
+ * this keypress will be trapped and not passed on to the text input.
271
+ *
272
+ * @name keys
273
+ * @default { ... }
274
+ * @author agorbatchev
275
+ * @date 2011/08/19
276
+ * @id TextExt.options.keys
277
+ */
278
+ OPT_KEYS = 'keys',
279
+
280
+ /**
281
+ * The core triggers or reacts to the following events.
282
+ *
283
+ * @author agorbatchev
284
+ * @date 2011/08/17
285
+ * @id TextExt.events
286
+ */
287
+
288
+ /**
289
+ * Core triggers `preInvalidate` event before the dimensions of padding on the text input
290
+ * are set.
291
+ *
292
+ * @name preInvalidate
293
+ * @author agorbatchev
294
+ * @date 2011/08/19
295
+ * @id TextExt.events.preInvalidate
296
+ */
297
+ EVENT_PRE_INVALIDATE = 'preInvalidate',
298
+
299
+ /**
300
+ * Core triggers `postInvalidate` event after the dimensions of padding on the text input
301
+ * are set.
302
+ *
303
+ * @name postInvalidate
304
+ * @author agorbatchev
305
+ * @date 2011/08/19
306
+ * @id TextExt.events.postInvalidate
307
+ */
308
+ EVENT_POST_INVALIDATE = 'postInvalidate',
309
+
310
+ /**
311
+ * Core triggers `getFormData` on every key press to collect data that will be populated
312
+ * into the hidden input that will be submitted with the HTML form and data that will
313
+ * be displayed in the input field that user is currently interacting with.
314
+ *
315
+ * All plugins that wish to affect how the data is presented or sent must react to
316
+ * `getFormData` and populate the data in the following format:
317
+ *
318
+ * {
319
+ * input : {String},
320
+ * form : {Object}
321
+ * }
322
+ *
323
+ * The data key must be a numeric weight which will be used to determine which data
324
+ * ends up being used. Data with the highest numerical weight gets the priority. This
325
+ * allows plugins to set the final data regardless of their initialization order, which
326
+ * otherwise would be impossible.
327
+ *
328
+ * For example, the Tags and Autocomplete plugins have to work side by side and Tags
329
+ * plugin must get priority on setting the data. Therefore the Tags plugin sets data
330
+ * with the weight 200 where as the Autocomplete plugin sets data with the weight 100.
331
+ *
332
+ * Here's an example of a typical `getFormData` handler:
333
+ *
334
+ * TextExtPlugin.prototype.onGetFormData = function(e, data, keyCode)
335
+ * {
336
+ * data[100] = self.formDataObject('input value', 'form value');
337
+ * };
338
+ *
339
+ * Core also reacts to the `getFormData` and updates hidden input with data which will be
340
+ * submitted with the HTML form.
341
+ *
342
+ * @name getFormData
343
+ * @author agorbatchev
344
+ * @date 2011/08/19
345
+ * @id TextExt.events.getFormData
346
+ */
347
+ EVENT_GET_FORM_DATA = 'getFormData',
348
+
349
+ /**
350
+ * Core triggers and reacts to the `setFormData` event to update the actual value in the
351
+ * hidden input that will be submitted with the HTML form. Second argument can be value
352
+ * of any type and by default it will be JSON serialized with `TextExt.serializeData()`
353
+ * function.
354
+ *
355
+ * @name setFormData
356
+ * @author agorbatchev
357
+ * @date 2011/08/22
358
+ * @id TextExt.events.setFormData
359
+ */
360
+ EVENT_SET_FORM_DATA = 'setFormData',
361
+
362
+ /**
363
+ * Core triggers and reacts to the `setInputData` event to update the actual value in the
364
+ * text input that user is interacting with. Second argument must be of a `String` type
365
+ * the value of which will be set into the text input.
366
+ *
367
+ * @name setInputData
368
+ * @author agorbatchev
369
+ * @date 2011/08/22
370
+ * @id TextExt.events.setInputData
371
+ */
372
+ EVENT_SET_INPUT_DATA = 'setInputData',
373
+
374
+ /**
375
+ * Core triggers `postInit` event to let plugins run code after all plugins have been
376
+ * created and initialized. This is a good place to set some kind of global values before
377
+ * somebody gets to use them. This is not the right place to expect all plugins to finish
378
+ * their initialization.
379
+ *
380
+ * @name postInit
381
+ * @author agorbatchev
382
+ * @date 2011/08/19
383
+ * @id TextExt.events.postInit
384
+ */
385
+ EVENT_POST_INIT = 'postInit',
386
+
387
+ /**
388
+ * Core triggers `ready` event after all global configuration and prepearation has been
389
+ * done and the TextExt component is ready for use. Event handlers should expect all
390
+ * values to be set and the plugins to be in the final state.
391
+ *
392
+ * @name ready
393
+ * @author agorbatchev
394
+ * @date 2011/08/19
395
+ * @id TextExt.events.ready
396
+ */
397
+ EVENT_READY = 'ready',
398
+
399
+ /**
400
+ * Core triggers `anyKeyUp` event for every key up event triggered within the component.
401
+ *
402
+ * @name anyKeyUp
403
+ * @author agorbatchev
404
+ * @date 2011/08/19
405
+ * @id TextExt.events.anyKeyUp
406
+ */
407
+
408
+ /**
409
+ * Core triggers `anyKeyDown` event for every key down event triggered within the component.
410
+ *
411
+ * @name anyKeyDown
412
+ * @author agorbatchev
413
+ * @date 2011/08/19
414
+ * @id TextExt.events.anyKeyDown
415
+ */
416
+
417
+ /**
418
+ * Core triggers `[name]KeyUp` event for every key specifid in the `keys` option that is
419
+ * triggered within the component.
420
+ *
421
+ * @name [name]KeyUp
422
+ * @author agorbatchev
423
+ * @date 2011/08/19
424
+ * @id TextExt.events.[name]KeyUp
425
+ */
426
+
427
+ /**
428
+ * Core triggers `[name]KeyDown` event for every key specified in the `keys` option that is
429
+ * triggered within the component.
430
+ *
431
+ * @name [name]KeyDown
432
+ * @author agorbatchev
433
+ * @date 2011/08/19
434
+ * @id TextExt.events.[name]KeyDown
435
+ */
436
+
437
+ /**
438
+ * Core triggers `[name]KeyPress` event for every key specified in the `keys` option that is
439
+ * triggered within the component.
440
+ *
441
+ * @name [name]KeyPress
442
+ * @author agorbatchev
443
+ * @date 2011/08/19
444
+ * @id TextExt.events.[name]KeyPress
445
+ */
446
+
447
+ DEFAULT_OPTS = {
448
+ itemManager : ItemManager,
449
+
450
+ plugins : [],
451
+ ext : {},
452
+
453
+ html : {
454
+ wrap : '<div class="text-core"><div class="text-wrap"/></div>',
455
+ hidden : '<input type="hidden" />'
456
+ },
457
+
458
+ keys : {
459
+ 8 : 'backspace',
460
+ 9 : 'tab',
461
+ 13 : 'enter!',
462
+ 27 : 'escape!',
463
+ 37 : 'left',
464
+ 38 : 'up!',
465
+ 39 : 'right',
466
+ 40 : 'down!',
467
+ 46 : 'delete',
468
+ 108 : 'numpadEnter'
469
+ }
470
+ }
471
+ ;
472
+
473
+ // Freak out if there's no JSON.stringify function found
474
+ if(!stringify)
475
+ throw new Error('JSON.stringify() not found');
476
+
477
+ /**
478
+ * Returns object property by name where name is dot-separated and object is multiple levels deep.
479
+ * @param target Object Source object.
480
+ * @param name String Dot separated property name, ie `foo.bar.world`
481
+ * @id core.getProperty
482
+ */
483
+ function getProperty(source, name)
484
+ {
485
+ if(typeof(name) === 'string')
486
+ name = name.split('.');
487
+
488
+ var fullCamelCaseName = name.join('.').replace(/\.(\w)/g, function(match, letter) { return letter.toUpperCase() }),
489
+ nestedName = name.shift(),
490
+ result
491
+ ;
492
+
493
+ if(typeof(result = source[fullCamelCaseName]) != UNDEFINED)
494
+ result = result;
495
+
496
+ else if(typeof(result = source[nestedName]) != UNDEFINED && name.length > 0)
497
+ result = getProperty(result, name);
498
+
499
+ // name.length here should be zero
500
+ return result;
501
+ };
502
+
503
+ /**
504
+ * Hooks up specified events in the scope of the current object.
505
+ * @author agorbatchev
506
+ * @date 2011/08/09
507
+ */
508
+ function hookupEvents()
509
+ {
510
+ var args = slice.apply(arguments),
511
+ self = this,
512
+ target = args.length === 1 ? self : args.shift(),
513
+ event
514
+ ;
515
+
516
+ args = args[0] || {};
517
+
518
+ function bind(event, handler)
519
+ {
520
+ target.bind(event, function()
521
+ {
522
+ // apply handler to our PLUGIN object, not the target
523
+ return handler.apply(self, arguments);
524
+ });
525
+ }
526
+
527
+ for(event in args)
528
+ bind(event, args[event]);
529
+ };
530
+
531
+ function formDataObject(input, form)
532
+ {
533
+ return { 'input' : input, 'form' : form };
534
+ };
535
+
536
+ //--------------------------------------------------------------------------------
537
+ // ItemManager core component
538
+
539
+ p = ItemManager.prototype;
540
+
541
+ /**
542
+ * Initialization method called by the core during instantiation.
543
+ *
544
+ * @signature ItemManager.init(core)
545
+ *
546
+ * @param core {TextExt} Instance of the TextExt core class.
547
+ *
548
+ * @author agorbatchev
549
+ * @date 2011/08/19
550
+ * @id ItemManager.init
551
+ */
552
+ p.init = function(core)
553
+ {
554
+ };
555
+
556
+ /**
557
+ * Filters out items from the list that don't match the query and returns remaining items. Default
558
+ * implementation checks if the item starts with the query.
559
+ *
560
+ * @signature ItemManager.filter(list, query)
561
+ *
562
+ * @param list {Array} List of items. Default implementation works with strings.
563
+ * @param query {String} Query string.
564
+ *
565
+ * @author agorbatchev
566
+ * @date 2011/08/19
567
+ * @id ItemManager.filter
568
+ */
569
+ p.filter = function(list, query)
570
+ {
571
+ var result = [],
572
+ i, item
573
+ ;
574
+
575
+ for(i = 0; i < list.length; i++)
576
+ {
577
+ item = list[i];
578
+ if(this.itemContains(item, query))
579
+ result.push(item);
580
+ }
581
+
582
+ return result;
583
+ };
584
+
585
+ /**
586
+ * Returns `true` if specified item contains another string, `false` otherwise. In the default implementation
587
+ * `String.indexOf()` is used to check if item string begins with the needle string.
588
+ *
589
+ * @signature ItemManager.itemContains(item, needle)
590
+ *
591
+ * @param item {Object} Item to check. Default implementation works with strings.
592
+ * @param needle {String} Search string to be found within the item.
593
+ *
594
+ * @author agorbatchev
595
+ * @date 2011/08/19
596
+ * @id ItemManager.itemContains
597
+ */
598
+ p.itemContains = function(item, needle)
599
+ {
600
+ return this.itemToString(item).toLowerCase().indexOf(needle.toLowerCase()) == 0;
601
+ };
602
+
603
+ /**
604
+ * Converts specified string to item. Because default implemenation works with string, input string
605
+ * is simply returned back. To use custom objects, different implementation of this method could
606
+ * return something like `{ name : {String} }`.
607
+ *
608
+ * @signature ItemManager.stringToItem(str)
609
+ *
610
+ * @param str {String} Input string.
611
+ *
612
+ * @author agorbatchev
613
+ * @date 2011/08/19
614
+ * @id ItemManager.stringToItem
615
+ */
616
+ p.stringToItem = function(str)
617
+ {
618
+ return str;
619
+ };
620
+
621
+ /**
622
+ * Converts specified item to string. Because default implemenation works with string, input string
623
+ * is simply returned back. To use custom objects, different implementation of this method could
624
+ * for example return `name` field of `{ name : {String} }`.
625
+ *
626
+ * @signature ItemManager.itemToString(item)
627
+ *
628
+ * @param item {Object} Input item to be converted to string.
629
+ *
630
+ * @author agorbatchev
631
+ * @date 2011/08/19
632
+ * @id ItemManager.itemToString
633
+ */
634
+ p.itemToString = function(item)
635
+ {
636
+ return item;
637
+ };
638
+
639
+ /**
640
+ * Returns `true` if both items are equal, `false` otherwise. Because default implemenation works with
641
+ * string, input items are compared as strings. To use custom objects, different implementation of this
642
+ * method could for example compare `name` fields of `{ name : {String} }` type object.
643
+ *
644
+ * @signature ItemManager.compareItems(item1, item2)
645
+ *
646
+ * @param item1 {Object} First item.
647
+ * @param item2 {Object} Second item.
648
+ *
649
+ * @author agorbatchev
650
+ * @date 2011/08/19
651
+ * @id ItemManager.compareItems
652
+ */
653
+ p.compareItems = function(item1, item2)
654
+ {
655
+ return item1 == item2;
656
+ };
657
+
658
+ //--------------------------------------------------------------------------------
659
+ // TextExt core component
660
+
661
+ p = TextExt.prototype;
662
+
663
+ /**
664
+ * Initializes current component instance with work with the supplied text input and options.
665
+ *
666
+ * @signature TextExt.init(input, opts)
667
+ *
668
+ * @param input {HTMLElement} Text input.
669
+ * @param opts {Object} Options.
670
+ *
671
+ * @author agorbatchev
672
+ * @date 2011/08/19
673
+ * @id TextExt.init
674
+ */
675
+ p.init = function(input, opts)
676
+ {
677
+ var self = this,
678
+ hiddenInput,
679
+ itemManager,
680
+ container
681
+ ;
682
+
683
+ self._defaults = $.extend({}, DEFAULT_OPTS);
684
+ self._opts = opts || {};
685
+ self._plugins = {};
686
+ self._itemManager = itemManager = new (self.opts(OPT_ITEM_MANAGER))();
687
+ input = $(input);
688
+ container = $(self.opts(OPT_HTML_WRAP));
689
+ hiddenInput = $(self.opts(OPT_HTML_HIDDEN));
690
+
691
+ input
692
+ .wrap(container)
693
+ .keydown(function(e) { return self.onKeyDown(e) })
694
+ .keyup(function(e) { return self.onKeyUp(e) })
695
+ .data('textext', self)
696
+ ;
697
+
698
+ // keep references to html elements using jQuery.data() to avoid circular references
699
+ $(self).data({
700
+ 'hiddenInput' : hiddenInput,
701
+ 'wrapElement' : input.parents('.text-wrap').first(),
702
+ 'input' : input
703
+ });
704
+
705
+ // set the name of the hidden input to the text input's name
706
+ hiddenInput.attr('name', input.attr('name'));
707
+ // remove name attribute from the text input
708
+ input.attr('name', null);
709
+ // add hidden input to the DOM
710
+ hiddenInput.insertAfter(input);
711
+
712
+ $.extend(true, itemManager, self.opts(OPT_EXT + '.item.manager'));
713
+ $.extend(true, self, self.opts(OPT_EXT + '.*'), self.opts(OPT_EXT + '.core'));
714
+
715
+ self.originalWidth = input.outerWidth();
716
+
717
+ self.invalidateBounds();
718
+
719
+ itemManager.init(self);
720
+
721
+ self.initPatches();
722
+ self.initPlugins(self.opts(OPT_PLUGINS), $.fn.textext.plugins);
723
+
724
+ self.on({
725
+ setFormData : self.onSetFormData,
726
+ getFormData : self.onGetFormData,
727
+ setInputData : self.onSetInputData,
728
+ anyKeyUp : self.onAnyKeyUp
729
+ });
730
+
731
+ self.trigger(EVENT_POST_INIT);
732
+ self.trigger(EVENT_READY);
733
+
734
+ self.getFormData(0);
735
+ };
736
+
737
+ /**
738
+ * Initialized all installed patches against current instance. The patches are initialized based on their
739
+ * initialization priority which is returned by each patch's `initPriority()` method. Priority
740
+ * is a `Number` where patches with higher value gets their `init()` method called before patches
741
+ * with lower priority value.
742
+ *
743
+ * This facilitates initializing of patches in certain order to insure proper dependencies
744
+ * regardless of which order they are loaded.
745
+ *
746
+ * By default all patches have the same priority - zero, which means they will be initialized
747
+ * in rorder they are loaded, that is unless `initPriority()` is overriden.
748
+ *
749
+ * @signature TextExt.initPatches()
750
+ *
751
+ * @author agorbatchev
752
+ * @date 2011/10/11
753
+ * @id TextExt.initPatches
754
+ */
755
+ p.initPatches = function()
756
+ {
757
+ var list = [],
758
+ source = $.fn.textext.patches,
759
+ name
760
+ ;
761
+
762
+ for(name in source)
763
+ list.push(name);
764
+
765
+ this.initPlugins(list, source);
766
+ };
767
+
768
+ /**
769
+ * Creates and initializes all specified plugins. The plugins are initialized based on their
770
+ * initialization priority which is returned by each plugin's `initPriority()` method. Priority
771
+ * is a `Number` where plugins with higher value gets their `init()` method called before plugins
772
+ * with lower priority value.
773
+ *
774
+ * This facilitates initializing of plugins in certain order to insure proper dependencies
775
+ * regardless of which order user enters them in the `plugins` option field.
776
+ *
777
+ * By default all plugins have the same priority - zero, which means they will be initialized
778
+ * in the same order as entered by the user.
779
+ *
780
+ * @signature TextExt.initPlugins(plugins)
781
+ *
782
+ * @param plugins {Array} List of plugin names to initialize.
783
+ *
784
+ * @author agorbatchev
785
+ * @date 2011/08/19
786
+ * @id TextExt.initPlugins
787
+ */
788
+ p.initPlugins = function(plugins, source)
789
+ {
790
+ var self = this,
791
+ ext, name, plugin, initList = [], i
792
+ ;
793
+
794
+ if(typeof(plugins) == 'string')
795
+ plugins = plugins.split(/\s*,\s*|\s+/g);
796
+
797
+ for(i = 0; i < plugins.length; i++)
798
+ {
799
+ name = plugins[i];
800
+ plugin = source[name];
801
+
802
+ if(plugin)
803
+ {
804
+ self._plugins[name] = plugin = new plugin();
805
+ self[name] = function() { return plugin; };
806
+ initList.push(plugin);
807
+ $.extend(true, plugin, self.opts(OPT_EXT + '.*'), self.opts(OPT_EXT + '.' + name));
808
+ }
809
+ }
810
+
811
+ // sort plugins based on their priority values
812
+ initList.sort(function(p1, p2)
813
+ {
814
+ p1 = p1.initPriority();
815
+ p2 = p2.initPriority();
816
+
817
+ return p1 === p2
818
+ ? 0
819
+ : p1 < p2 ? 1 : -1
820
+ ;
821
+ });
822
+
823
+ for(i = 0; i < initList.length; i++)
824
+ initList[i].init(self);
825
+ };
826
+
827
+ /**
828
+ * Returns true if specified plugin is was instantiated for the current instance of core.
829
+ *
830
+ * @signature TextExt.hasPlugin(name)
831
+ *
832
+ * @param name {String} Name of the plugin to check.
833
+ *
834
+ * @author agorbatchev
835
+ * @date 2011/12/28
836
+ * @id TextExt.hasPlugin
837
+ * @version 1.1
838
+ */
839
+ p.hasPlugin = function(name)
840
+ {
841
+ return !!this._plugins[name];
842
+ };
843
+
844
+ /**
845
+ * Allows to add multiple event handlers which will be execued in the scope of the current object.
846
+ *
847
+ * @signature TextExt.on([target], handlers)
848
+ *
849
+ * @param target {Object} **Optional**. Target object which has traditional `bind(event, handler)` method.
850
+ * Handler function will still be executed in the current object's scope.
851
+ * @param handlers {Object} Key/value pairs of event names and handlers, eg `{ event: handler }`.
852
+ *
853
+ * @author agorbatchev
854
+ * @date 2011/08/19
855
+ * @id TextExt.on
856
+ */
857
+ p.on = hookupEvents;
858
+
859
+ /**
860
+ * Binds an event handler to the input box that user interacts with.
861
+ *
862
+ * @signature TextExt.bind(event, handler)
863
+ *
864
+ * @param event {String} Event name.
865
+ * @param handler {Function} Event handler.
866
+ *
867
+ * @author agorbatchev
868
+ * @date 2011/08/19
869
+ * @id TextExt.bind
870
+ */
871
+ p.bind = function(event, handler)
872
+ {
873
+ this.input().bind(event, handler);
874
+ };
875
+
876
+ /**
877
+ * Triggers an event on the input box that user interacts with. All core events are originated here.
878
+ *
879
+ * @signature TextExt.trigger(event, ...args)
880
+ *
881
+ * @param event {String} Name of the event to trigger.
882
+ * @param ...args All remaining arguments will be passed to the event handler.
883
+ *
884
+ * @author agorbatchev
885
+ * @date 2011/08/19
886
+ * @id TextExt.trigger
887
+ */
888
+ p.trigger = function()
889
+ {
890
+ var args = arguments;
891
+ this.input().trigger(args[0], slice.call(args, 1));
892
+ };
893
+
894
+ /**
895
+ * Returns instance of `itemManager` that is used by the component.
896
+ *
897
+ * @signature TextExt.itemManager()
898
+ *
899
+ * @author agorbatchev
900
+ * @date 2011/08/19
901
+ * @id TextExt.itemManager
902
+ */
903
+ p.itemManager = function()
904
+ {
905
+ return this._itemManager;
906
+ };
907
+
908
+ /**
909
+ * Returns jQuery input element with which user is interacting with.
910
+ *
911
+ * @signature TextExt.input()
912
+ *
913
+ * @author agorbatchev
914
+ * @date 2011/08/10
915
+ * @id TextExt.input
916
+ */
917
+ p.input = function()
918
+ {
919
+ return $(this).data('input');
920
+ };
921
+
922
+ /**
923
+ * Returns option value for the specified option by name. If the value isn't found in the user
924
+ * provided options, it will try looking for default value.
925
+ *
926
+ * @signature TextExt.opts(name)
927
+ *
928
+ * @param name {String} Option name as described in the options.
929
+ *
930
+ * @author agorbatchev
931
+ * @date 2011/08/19
932
+ * @id TextExt.opts
933
+ */
934
+ p.opts = function(name)
935
+ {
936
+ var result = getProperty(this._opts, name);
937
+ return typeof(result) == 'undefined' ? getProperty(this._defaults, name) : result;
938
+ };
939
+
940
+ /**
941
+ * Returns HTML element that was created from the `html.wrap` option. This is the top level HTML
942
+ * container for the text input with which user is interacting with.
943
+ *
944
+ * @signature TextExt.wrapElement()
945
+ *
946
+ * @author agorbatchev
947
+ * @date 2011/08/19
948
+ * @id TextExt.wrapElement
949
+ */
950
+ p.wrapElement = function()
951
+ {
952
+ return $(this).data('wrapElement');
953
+ };
954
+
955
+ /**
956
+ * Updates container to match dimensions of the text input. Triggers `preInvalidate` and `postInvalidate`
957
+ * events.
958
+ *
959
+ * @signature TextExt.invalidateBounds()
960
+ *
961
+ * @author agorbatchev
962
+ * @date 2011/08/19
963
+ * @id TextExt.invalidateBounds
964
+ */
965
+ p.invalidateBounds = function()
966
+ {
967
+ var self = this,
968
+ input = self.input(),
969
+ wrap = self.wrapElement(),
970
+ container = wrap.parent(),
971
+ width = self.originalWidth,
972
+ height
973
+ ;
974
+
975
+ self.trigger(EVENT_PRE_INVALIDATE);
976
+
977
+ height = input.outerHeight();
978
+
979
+ input.width(width);
980
+ wrap.width(width).height(height);
981
+ container.height(height);
982
+
983
+ self.trigger(EVENT_POST_INVALIDATE);
984
+ };
985
+
986
+ /**
987
+ * Focuses user input on the text box.
988
+ *
989
+ * @signature TextExt.focusInput()
990
+ *
991
+ * @author agorbatchev
992
+ * @date 2011/08/19
993
+ * @id TextExt.focusInput
994
+ */
995
+ p.focusInput = function()
996
+ {
997
+ this.input()[0].focus();
998
+ };
999
+
1000
+ /**
1001
+ * Serializes data for to be set into the hidden input field and which will be submitted
1002
+ * with the HTML form.
1003
+ *
1004
+ * By default simple JSON serialization is used. It's expected that `JSON.stringify`
1005
+ * method would be available either through built in class in most modern browsers
1006
+ * or through JSON2 library.
1007
+ *
1008
+ * @signature TextExt.serializeData(data)
1009
+ *
1010
+ * @param data {Object} Data to serialize.
1011
+ *
1012
+ * @author agorbatchev
1013
+ * @date 2011/08/09
1014
+ * @id TextExt.serializeData
1015
+ */
1016
+ p.serializeData = stringify;
1017
+
1018
+ /**
1019
+ * Returns the hidden input HTML element which will be submitted with the HTML form.
1020
+ *
1021
+ * @signature TextExt.hiddenInput()
1022
+ *
1023
+ * @author agorbatchev
1024
+ * @date 2011/08/09
1025
+ * @id TextExt.hiddenInput
1026
+ */
1027
+ p.hiddenInput = function(value)
1028
+ {
1029
+ return $(this).data('hiddenInput');
1030
+ };
1031
+
1032
+ /**
1033
+ * Abstracted functionality to trigger an event and get the data with maximum weight set by all
1034
+ * the event handlers. This functionality is used for the `getFormData` event.
1035
+ *
1036
+ * @signature TextExt.getWeightedEventResponse(event, args)
1037
+ *
1038
+ * @param event {String} Event name.
1039
+ * @param args {Object} Argument to be passed with the event.
1040
+ *
1041
+ * @author agorbatchev
1042
+ * @date 2011/08/22
1043
+ * @id TextExt.getWeightedEventResponse
1044
+ */
1045
+ p.getWeightedEventResponse = function(event, args)
1046
+ {
1047
+ var self = this,
1048
+ data = {},
1049
+ maxWeight = 0
1050
+ ;
1051
+
1052
+ self.trigger(event, data, args);
1053
+
1054
+ for(var weight in data)
1055
+ maxWeight = Math.max(maxWeight, weight);
1056
+
1057
+ return data[maxWeight];
1058
+ };
1059
+
1060
+ /**
1061
+ * Triggers the `getFormData` event to get all the plugins to return their data.
1062
+ *
1063
+ * After the data is returned, triggers `setFormData` and `setInputData` to update appopriate values.
1064
+ *
1065
+ * @signature TextExt.getFormData(keyCode)
1066
+ *
1067
+ * @param keyCode {Number} Key code number which has triggered this update. It's impotant to pass
1068
+ * this value to the plugins because they might return different values based on the key that was
1069
+ * pressed. For example, the Tags plugin returns an empty string for the `input` value if the enter
1070
+ * key was pressed, otherwise it returns whatever is currently in the text input.
1071
+ *
1072
+ * @author agorbatchev
1073
+ * @date 2011/08/22
1074
+ * @id TextExt.getFormData
1075
+ */
1076
+ p.getFormData = function(keyCode)
1077
+ {
1078
+ var self = this,
1079
+ data = self.getWeightedEventResponse(EVENT_GET_FORM_DATA, keyCode)
1080
+ ;
1081
+
1082
+ self.trigger(EVENT_SET_FORM_DATA , data['form']);
1083
+ self.trigger(EVENT_SET_INPUT_DATA , data['input']);
1084
+ };
1085
+
1086
+ //--------------------------------------------------------------------------------
1087
+ // Event handlers
1088
+
1089
+ /**
1090
+ * Reacts to the `anyKeyUp` event and triggers the `getFormData` to change data that will be submitted
1091
+ * with the form. Default behaviour is that everything that is typed in will be JSON serialized, so
1092
+ * the end result will be a JSON string.
1093
+ *
1094
+ * @signature TextExt.onAnyKeyUp(e)
1095
+ *
1096
+ * @param e {Object} jQuery event.
1097
+ *
1098
+ * @author agorbatchev
1099
+ * @date 2011/08/19
1100
+ * @id TextExt.onAnyKeyUp
1101
+ */
1102
+ p.onAnyKeyUp = function(e, keyCode)
1103
+ {
1104
+ this.getFormData(keyCode);
1105
+ };
1106
+
1107
+ /**
1108
+ * Reacts to the `setInputData` event and populates the input text field that user is currently
1109
+ * interacting with.
1110
+ *
1111
+ * @signature TextExt.onSetInputData(e, data)
1112
+ *
1113
+ * @param e {Event} jQuery event.
1114
+ * @param data {String} Value to be set.
1115
+ *
1116
+ * @author agorbatchev
1117
+ * @date 2011/08/22
1118
+ * @id TextExt.onSetInputData
1119
+ */
1120
+ p.onSetInputData = function(e, data)
1121
+ {
1122
+ this.input().val(data);
1123
+ };
1124
+
1125
+ /**
1126
+ * Reacts to the `setFormData` event and populates the hidden input with will be submitted with
1127
+ * the HTML form. The value will be serialized with `serializeData()` method.
1128
+ *
1129
+ * @signature TextExt.onSetFormData(e, data)
1130
+ *
1131
+ * @param e {Event} jQuery event.
1132
+ * @param data {Object} Data that will be set.
1133
+ *
1134
+ * @author agorbatchev
1135
+ * @date 2011/08/22
1136
+ * @id TextExt.onSetFormData
1137
+ */
1138
+ p.onSetFormData = function(e, data)
1139
+ {
1140
+ var self = this;
1141
+ self.hiddenInput().val(self.serializeData(data));
1142
+ };
1143
+
1144
+ /**
1145
+ * Reacts to `getFormData` event triggered by the core. At the bare minimum the core will tell
1146
+ * itself to use the current value in the text input as the data to be submitted with the HTML
1147
+ * form.
1148
+ *
1149
+ * @signature TextExt.onGetFormData(e, data)
1150
+ *
1151
+ * @param e {Event} jQuery event.
1152
+ *
1153
+ * @author agorbatchev
1154
+ * @date 2011/08/09
1155
+ * @id TextExt.onGetFormData
1156
+ */
1157
+ p.onGetFormData = function(e, data)
1158
+ {
1159
+ var val = this.input().val();
1160
+ data[0] = formDataObject(val, val);
1161
+ };
1162
+
1163
+ //--------------------------------------------------------------------------------
1164
+ // User mouse/keyboard input
1165
+
1166
+ /**
1167
+ * Triggers `[name]KeyUp` and `[name]KeyPress` for every keystroke as described in the events.
1168
+ *
1169
+ * @signature TextExt.onKeyUp(e)
1170
+ *
1171
+ * @param e {Object} jQuery event.
1172
+ * @author agorbatchev
1173
+ * @date 2011/08/19
1174
+ * @id TextExt.onKeyUp
1175
+ */
1176
+
1177
+ /**
1178
+ * Triggers `[name]KeyDown` for every keystroke as described in the events.
1179
+ *
1180
+ * @signature TextExt.onKeyDown(e)
1181
+ *
1182
+ * @param e {Object} jQuery event.
1183
+ * @author agorbatchev
1184
+ * @date 2011/08/19
1185
+ * @id TextExt.onKeyDown
1186
+ */
1187
+
1188
+ $(['Down', 'Up']).each(function()
1189
+ {
1190
+ var type = this.toString();
1191
+
1192
+ p['onKey' + type] = function(e)
1193
+ {
1194
+ var self = this,
1195
+ keyName = self.opts(OPT_KEYS)[e.keyCode],
1196
+ defaultResult = true
1197
+ ;
1198
+
1199
+ if(keyName)
1200
+ {
1201
+ defaultResult = keyName.substr(-1) != '!';
1202
+ keyName = keyName.replace('!', '');
1203
+
1204
+ self.trigger(keyName + 'Key' + type);
1205
+
1206
+ // manual *KeyPress event fimplementation for the function keys like Enter, Backspace, etc.
1207
+ if(type == 'Up' && self._lastKeyDown == e.keyCode)
1208
+ {
1209
+ self._lastKeyDown = null;
1210
+ self.trigger(keyName + 'KeyPress');
1211
+ }
1212
+
1213
+ if(type == 'Down')
1214
+ self._lastKeyDown = e.keyCode;
1215
+ }
1216
+
1217
+ self.trigger('anyKey' + type, e.keyCode);
1218
+
1219
+ return defaultResult;
1220
+ };
1221
+ });
1222
+
1223
+ //--------------------------------------------------------------------------------
1224
+ // Plugin Base
1225
+
1226
+ p = TextExtPlugin.prototype;
1227
+
1228
+ /**
1229
+ * Allows to add multiple event handlers which will be execued in the scope of the current object.
1230
+ *
1231
+ * @signature TextExt.on([target], handlers)
1232
+ *
1233
+ * @param target {Object} **Optional**. Target object which has traditional `bind(event, handler)` method.
1234
+ * Handler function will still be executed in the current object's scope.
1235
+ * @param handlers {Object} Key/value pairs of event names and handlers, eg `{ event: handler }`.
1236
+ *
1237
+ * @author agorbatchev
1238
+ * @date 2011/08/19
1239
+ * @id TextExtPlugin.on
1240
+ */
1241
+ p.on = hookupEvents;
1242
+
1243
+ /**
1244
+ * Returns the hash object that `getFormData` triggered by the core expects.
1245
+ *
1246
+ * @signature TextExtPlugin.formDataObject(input, form)
1247
+ *
1248
+ * @param input {String} Value that will go into the text input that user is interacting with.
1249
+ * @param form {Object} Value that will be serialized and put into the hidden that will be submitted
1250
+ * with the HTML form.
1251
+ *
1252
+ * @author agorbatchev
1253
+ * @date 2011/08/22
1254
+ * @id TextExtPlugin.formDataObject
1255
+ */
1256
+ p.formDataObject = formDataObject;
1257
+
1258
+ /**
1259
+ * Initialization method called by the core during plugin instantiation. This method must be implemented
1260
+ * by each plugin individually.
1261
+ *
1262
+ * @signature TextExtPlugin.init(core)
1263
+ *
1264
+ * @param core {TextExt} Instance of the TextExt core class.
1265
+ *
1266
+ * @author agorbatchev
1267
+ * @date 2011/08/19
1268
+ * @id TextExtPlugin.init
1269
+ */
1270
+ p.init = function(core) { throw new Error('Not implemented') };
1271
+
1272
+ /**
1273
+ * Initialization method wich should be called by the plugin during the `init()` call.
1274
+ *
1275
+ * @signature TextExtPlugin.baseInit(core, defaults)
1276
+ *
1277
+ * @param core {TextExt} Instance of the TextExt core class.
1278
+ * @param defaults {Object} Default plugin options. These will be checked if desired value wasn't
1279
+ * found in the options supplied by the user.
1280
+ *
1281
+ * @author agorbatchev
1282
+ * @date 2011/08/19
1283
+ * @id TextExtPlugin.baseInit
1284
+ */
1285
+ p.baseInit = function(core, defaults)
1286
+ {
1287
+ var self = this;
1288
+
1289
+ core._defaults = $.extend(true, core._defaults, defaults);
1290
+ self._core = core;
1291
+ self._timers = {};
1292
+ };
1293
+
1294
+ /**
1295
+ * Allows starting of multiple timeout calls. Each time this method is called with the same
1296
+ * timer name, the timer is reset. This functionality is useful in cases where an action needs
1297
+ * to occur only after a certain period of inactivity. For example, making an AJAX call after
1298
+ * user stoped typing for 1 second.
1299
+ *
1300
+ * @signature TextExtPlugin.startTimer(name, delay, callback)
1301
+ *
1302
+ * @param name {String} Timer name.
1303
+ * @param delay {Number} Delay in seconds.
1304
+ * @param callback {Function} Callback function.
1305
+ *
1306
+ * @author agorbatchev
1307
+ * @date 2011/08/25
1308
+ * @id TextExtPlugin.startTimer
1309
+ */
1310
+ p.startTimer = function(name, delay, callback)
1311
+ {
1312
+ var self = this;
1313
+
1314
+ self.stopTimer(name);
1315
+
1316
+ self._timers[name] = setTimeout(
1317
+ function()
1318
+ {
1319
+ delete self._timers[name];
1320
+ callback.apply(self);
1321
+ },
1322
+ delay * 1000
1323
+ );
1324
+ };
1325
+
1326
+ /**
1327
+ * Stops the timer by name without resetting it.
1328
+ *
1329
+ * @signature TextExtPlugin.stopTimer(name)
1330
+ *
1331
+ * @param name {String} Timer name.
1332
+ *
1333
+ * @author agorbatchev
1334
+ * @date 2011/08/25
1335
+ * @id TextExtPlugin.stopTimer
1336
+ */
1337
+ p.stopTimer = function(name)
1338
+ {
1339
+ clearTimeout(this._timers[name]);
1340
+ };
1341
+
1342
+ /**
1343
+ * Returns instance of the `TextExt` to which current instance of the plugin is attached to.
1344
+ *
1345
+ * @signature TextExtPlugin.core()
1346
+ *
1347
+ * @author agorbatchev
1348
+ * @date 2011/08/19
1349
+ * @id TextExtPlugin.core
1350
+ */
1351
+ p.core = function()
1352
+ {
1353
+ return this._core;
1354
+ };
1355
+
1356
+ /**
1357
+ * Shortcut to the core's `opts()` method. Returns option value.
1358
+ *
1359
+ * @signature TextExtPlugin.opts(name)
1360
+ *
1361
+ * @param name {String} Option name as described in the options.
1362
+ *
1363
+ * @author agorbatchev
1364
+ * @date 2011/08/19
1365
+ * @id TextExtPlugin.opts
1366
+ */
1367
+ p.opts = function(name)
1368
+ {
1369
+ return this.core().opts(name);
1370
+ };
1371
+
1372
+ /**
1373
+ * Shortcut to the core's `itemManager()` method. Returns instance of the `ItemManger` that is
1374
+ * currently in use.
1375
+ *
1376
+ * @signature TextExtPlugin.itemManager()
1377
+ *
1378
+ * @author agorbatchev
1379
+ * @date 2011/08/19
1380
+ * @id TextExtPlugin.itemManager
1381
+ */
1382
+ p.itemManager = function()
1383
+ {
1384
+ return this.core().itemManager();
1385
+ };
1386
+
1387
+ /**
1388
+ * Shortcut to the core's `input()` method. Returns instance of the HTML element that represents
1389
+ * current text input.
1390
+ *
1391
+ * @signature TextExtPlugin.input()
1392
+ *
1393
+ * @author agorbatchev
1394
+ * @date 2011/08/19
1395
+ * @id TextExtPlugin.input
1396
+ */
1397
+ p.input = function()
1398
+ {
1399
+ return this.core().input();
1400
+ };
1401
+
1402
+ /**
1403
+ * Shortcut to the commonly used `this.input().val()` call to get or set value of the text input.
1404
+ *
1405
+ * @signature TextExtPlugin.val(value)
1406
+ *
1407
+ * @param value {String} Optional value. If specified, the value will be set, otherwise it will be
1408
+ * returned.
1409
+ *
1410
+ * @author agorbatchev
1411
+ * @date 2011/08/20
1412
+ * @id TextExtPlugin.val
1413
+ */
1414
+ p.val = function(value)
1415
+ {
1416
+ var input = this.input();
1417
+
1418
+ if(typeof(value) === UNDEFINED)
1419
+ return input.val();
1420
+ else
1421
+ input.val(value);
1422
+ };
1423
+
1424
+ /**
1425
+ * Shortcut to the core's `trigger()` method. Triggers specified event with arguments on the
1426
+ * component core.
1427
+ *
1428
+ * @signature TextExtPlugin.trigger(event, ...args)
1429
+ *
1430
+ * @param event {String} Name of the event to trigger.
1431
+ * @param ...args All remaining arguments will be passed to the event handler.
1432
+ *
1433
+ * @author agorbatchev
1434
+ * @date 2011/08/19
1435
+ * @id TextExtPlugin.trigger
1436
+ */
1437
+ p.trigger = function()
1438
+ {
1439
+ var core = this.core();
1440
+ core.trigger.apply(core, arguments);
1441
+ };
1442
+
1443
+ /**
1444
+ * Shortcut to the core's `bind()` method. Binds specified handler to the event.
1445
+ *
1446
+ * @signature TextExtPlugin.bind(event, handler)
1447
+ *
1448
+ * @param event {String} Event name.
1449
+ * @param handler {Function} Event handler.
1450
+ *
1451
+ * @author agorbatchev
1452
+ * @date 2011/08/20
1453
+ * @id TextExtPlugin.bind
1454
+ */
1455
+ p.bind = function(event, handler)
1456
+ {
1457
+ this.core().bind(event, handler);
1458
+ };
1459
+
1460
+ /**
1461
+ * Returns initialization priority for this plugin. If current plugin depends upon some other plugin
1462
+ * to be initialized before or after, priority needs to be adjusted accordingly. Plugins with higher
1463
+ * priority initialize before plugins with lower priority.
1464
+ *
1465
+ * Default initialization priority is `0`.
1466
+ *
1467
+ * @signature TextExtPlugin.initPriority()
1468
+ *
1469
+ * @author agorbatchev
1470
+ * @date 2011/08/22
1471
+ * @id TextExtPlugin.initPriority
1472
+ */
1473
+ p.initPriority = function()
1474
+ {
1475
+ return 0;
1476
+ };
1477
+
1478
+ //--------------------------------------------------------------------------------
1479
+ // jQuery Integration
1480
+
1481
+ /**
1482
+ * TextExt integrates as a jQuery plugin available through the `$(selector).textext(opts)` call. If
1483
+ * `opts` argument is passed, then a new instance of `TextExt` will be created for all the inputs
1484
+ * that match the `selector`. If `opts` wasn't passed and TextExt was already intantiated for
1485
+ * inputs that match the `selector`, array of `TextExt` instances will be returned instead.
1486
+ *
1487
+ * // will create a new instance of `TextExt` for all elements that match `.sample`
1488
+ * $('.sample').textext({ ... });
1489
+ *
1490
+ * // will return array of all `TextExt` instances
1491
+ * var list = $('.sample').textext();
1492
+ *
1493
+ * The following properties are also exposed through the jQuery `$.fn.textext`:
1494
+ *
1495
+ * * `TextExt` -- `TextExt` class.
1496
+ * * `TextExtPlugin` -- `TextExtPlugin` class.
1497
+ * * `ItemManager` -- `ItemManager` class.
1498
+ * * `plugins` -- Key/value table of all registered plugins.
1499
+ * * `addPlugin(name, constructor)` -- All plugins should register themselves using this function.
1500
+ *
1501
+ * @author agorbatchev
1502
+ * @date 2011/08/19
1503
+ * @id TextExt.jquery
1504
+ */
1505
+
1506
+ var cssInjected = false;
1507
+
1508
+ var textext = $.fn.textext = function(opts)
1509
+ {
1510
+ var css;
1511
+
1512
+ if(!cssInjected && (css = $.fn.textext.css) != null)
1513
+ {
1514
+ $('head').append('<style>' + css + '</style>');
1515
+ cssInjected = true;
1516
+ }
1517
+
1518
+ return this.map(function()
1519
+ {
1520
+ var self = $(this);
1521
+
1522
+ if(opts == null)
1523
+ return self.data('textext');
1524
+
1525
+ var instance = new TextExt();
1526
+
1527
+ instance.init(self, opts);
1528
+ self.data('textext', instance);
1529
+
1530
+ return instance.input()[0];
1531
+ });
1532
+ };
1533
+
1534
+ /**
1535
+ * This static function registers a new plugin which makes it available through the `plugins` option
1536
+ * to the end user. The name specified here is the name the end user would put in the `plugins` option
1537
+ * to add this plugin to a new instance of TextExt.
1538
+ *
1539
+ * @signature $.fn.textext.addPlugin(name, constructor)
1540
+ *
1541
+ * @param name {String} Name of the plugin.
1542
+ * @param constructor {Function} Plugin constructor.
1543
+ *
1544
+ * @author agorbatchev
1545
+ * @date 2011/10/11
1546
+ * @id TextExt.addPlugin
1547
+ */
1548
+ textext.addPlugin = function(name, constructor)
1549
+ {
1550
+ textext.plugins[name] = constructor;
1551
+ constructor.prototype = new textext.TextExtPlugin();
1552
+ };
1553
+
1554
+ /**
1555
+ * This static function registers a new patch which is added to each instance of TextExt. If you are
1556
+ * adding a new patch, make sure to call this method.
1557
+ *
1558
+ * @signature $.fn.textext.addPatch(name, constructor)
1559
+ *
1560
+ * @param name {String} Name of the patch.
1561
+ * @param constructor {Function} Patch constructor.
1562
+ *
1563
+ * @author agorbatchev
1564
+ * @date 2011/10/11
1565
+ * @id TextExt.addPatch
1566
+ */
1567
+ textext.addPatch = function(name, constructor)
1568
+ {
1569
+ textext.patches[name] = constructor;
1570
+ constructor.prototype = new textext.TextExtPlugin();
1571
+ };
1572
+
1573
+ textext.TextExt = TextExt;
1574
+ textext.TextExtPlugin = TextExtPlugin;
1575
+ textext.ItemManager = ItemManager;
1576
+ textext.plugins = {};
1577
+ textext.patches = {};
1578
+ })(jQuery);
1579
+
1580
+ (function($)
1581
+ {
1582
+ function TextExtIE9Patches() {};
1583
+
1584
+ $.fn.textext.TextExtIE9Patches = TextExtIE9Patches;
1585
+ $.fn.textext.addPatch('ie9',TextExtIE9Patches);
1586
+
1587
+ var p = TextExtIE9Patches.prototype;
1588
+
1589
+ p.init = function(core)
1590
+ {
1591
+ if(navigator.userAgent.indexOf('MSIE 9') == -1)
1592
+ return;
1593
+
1594
+ var self = this;
1595
+
1596
+ core.on({ postInvalidate : self.onPostInvalidate });
1597
+ };
1598
+
1599
+ p.onPostInvalidate = function()
1600
+ {
1601
+ var self = this,
1602
+ input = self.input(),
1603
+ val = input.val()
1604
+ ;
1605
+
1606
+ // agorbatchev :: IE9 doesn't seem to update the padding if box-sizing is on until the
1607
+ // text box value changes, so forcing this change seems to do the trick of updating
1608
+ // IE's padding visually.
1609
+ input.val(Math.random());
1610
+ input.val(val);
1611
+ };
1612
+ })(jQuery);
1613
+