-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathtextext.core.js
1617 lines (1464 loc) · 43.7 KB
/
textext.core.js
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* jQuery TextExt Plugin
* http://textextjs.com
*
* @version 1.3.1
* @copyright Copyright (C) 2011 Alex Gorbatchev. All rights reserved.
* @license MIT License
*/
(function($, undefined)
{
/**
* TextExt is the main core class which by itself doesn't provide any functionality
* that is user facing, however it has the underlying mechanics to bring all the
* plugins together under one roof and make them work with each other or on their
* own.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt
*/
function TextExt() {};
/**
* ItemManager is used to seamlessly convert between string that come from the user input to whatever
* the format the item data is being passed around in. It's used by all plugins that in one way or
* another operate with items, such as Tags, Filter, Autocomplete and Suggestions. Default implementation
* works with `String` type.
*
* Each instance of `TextExt` creates a new instance of default implementation of `ItemManager`
* unless `itemManager` option was set to another implementation.
*
* To satisfy requirements of managing items of type other than a `String`, different implementation
* if `ItemManager` should be supplied.
*
* If you wish to bring your own implementation, you need to create a new class and implement all the
* methods that `ItemManager` has. After, you need to supply your pass via the `itemManager` option during
* initialization like so:
*
* $('#input').textext({
* itemManager : CustomItemManager
* })
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager
*/
function ItemManager() {};
/**
* TextExtPlugin is a base class for all plugins. It provides common methods which are reused
* by majority of plugins.
*
* All plugins must register themselves by calling the `$.fn.textext.addPlugin(name, constructor)`
* function while providing plugin name and constructor. The plugin name is the same name that user
* will identify the plugin in the `plugins` option when initializing TextExt component and constructor
* function will create a new instance of the plugin. *Without registering, the core won't
* be able to see the plugin.*
*
* <span class="new label version">new in 1.2.0</span> You can get instance of each plugin from the core
* via associated function with the same name as the plugin. For example:
*
* $('#input').textext()[0].tags()
* $('#input').textext()[0].autocomplete()
* ...
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExtPlugin
*/
function TextExtPlugin() {};
var stringify = (JSON || {}).stringify,
slice = Array.prototype.slice,
p,
UNDEFINED = 'undefined',
/**
* TextExt provides a way to pass in the options to configure the core as well as
* each plugin that is being currently used. The jQuery exposed plugin `$().textext()`
* function takes a hash object with key/value set of options. For example:
*
* $('textarea').textext({
* enabled: true
* })
*
* There are multiple ways of passing in the options:
*
* 1. Options could be nested multiple levels deep and accessed using all lowercased, dot
* separated style, eg `foo.bar.world`. The manual is using this style for clarity and
* consistency. For example:
*
* {
* item: {
* manager: ...
* },
*
* html: {
* wrap: ...
* },
*
* autocomplete: {
* enabled: ...,
* dropdown: {
* position: ...
* }
* }
* }
*
* 2. Options could be specified using camel cased names in a flat key/value fashion like so:
*
* {
* itemManager: ...,
* htmlWrap: ...,
* autocompleteEnabled: ...,
* autocompleteDropdownPosition: ...
* }
*
* 3. Finally, options could be specified in mixed style. It's important to understand that
* for each dot separated name, its alternative in camel case is also checked for, eg for
* `foo.bar.world` it's alternatives could be `fooBarWorld`, `foo.barWorld` or `fooBar.world`,
* which translates to `{ foo: { bar: { world: ... } } }`, `{ fooBarWorld: ... }`,
* `{ foo : { barWorld : ... } }` or `{ fooBar: { world: ... } }` respectively. For example:
*
* {
* itemManager : ...,
* htmlWrap: ...,
* autocomplete: {
* enabled: ...,
* dropdownPosition: ...
* }
* }
*
* Mixed case is used through out the code, wherever it seems appropriate. However in the code, all option
* names are specified in the dot notation because it works both ways where as camel case is not
* being converted to its alternative dot notation.
*
* @author agorbatchev
* @date 2011/08/17
* @id TextExt.options
*/
/**
* Default instance of `ItemManager` which takes `String` type as default for tags.
*
* @name item.manager
* @default ItemManager
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.options.item.manager
*/
OPT_ITEM_MANAGER = 'item.manager',
/**
* List of plugins that should be used with the current instance of TextExt. The list could be
* specified as array of strings or as comma or space separated string.
*
* @name plugins
* @default []
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.options.plugins
*/
OPT_PLUGINS = 'plugins',
/**
* TextExt allows for overriding of virtually any method that the core or any of its plugins
* use. This could be accomplished through the use of the `ext` option.
*
* It's possible to specifically target the core or any plugin, as well as overwrite all the
* desired methods everywhere.
*
* 1. Targeting the core:
*
* ext: {
* core: {
* trigger: function()
* {
* console.log('TextExt.trigger', arguments);
* $.fn.textext.TextExt.prototype.trigger.apply(this, arguments);
* }
* }
* }
*
* 2. Targeting individual plugins:
*
* ext: {
* tags: {
* addTags: function(tags)
* {
* console.log('TextExtTags.addTags', tags);
* $.fn.textext.TextExtTags.prototype.addTags.apply(this, arguments);
* }
* }
* }
*
* 3. Targeting `ItemManager` instance:
*
* ext: {
* itemManager: {
* stringToItem: function(str)
* {
* console.log('ItemManager.stringToItem', str);
* return $.fn.textext.ItemManager.prototype.stringToItem.apply(this, arguments);
* }
* }
* }
*
* 4. And finally, in edge cases you can extend everything at once:
*
* ext: {
* '*': {
* fooBar: function() {}
* }
* }
*
* @name ext
* @default {}
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.options.ext
*/
OPT_EXT = 'ext',
/**
* HTML source that is used to generate elements necessary for the core and all other
* plugins to function.
*
* @name html.wrap
* @default '<div class="text-core"><div class="text-wrap"/></div>'
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.options.html.wrap
*/
OPT_HTML_WRAP = 'html.wrap',
/**
* HTML source that is used to generate hidden input value of which will be submitted
* with the HTML form.
*
* @name html.hidden
* @default '<input type="hidden" />'
* @author agorbatchev
* @date 2011/08/20
* @id TextExt.options.html.hidden
*/
OPT_HTML_HIDDEN = 'html.hidden',
/**
* Hash table of key codes and key names for which special events will be created
* by the core. For each entry a `[name]KeyDown`, `[name]KeyUp` and `[name]KeyPress` events
* will be triggered along side with `anyKeyUp` and `anyKeyDown` events for every
* key stroke.
*
* Here's a list of default keys:
*
* {
* 8 : 'backspace',
* 9 : 'tab',
* 13 : 'enter!',
* 27 : 'escape!',
* 37 : 'left',
* 38 : 'up!',
* 39 : 'right',
* 40 : 'down!',
* 46 : 'delete',
* 108 : 'numpadEnter'
* }
*
* Please note the `!` at the end of some keys. This tells the core that by default
* this keypress will be trapped and not passed on to the text input.
*
* @name keys
* @default { ... }
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.options.keys
*/
OPT_KEYS = 'keys',
/**
* The core triggers or reacts to the following events.
*
* @author agorbatchev
* @date 2011/08/17
* @id TextExt.events
*/
/**
* Core triggers `preInvalidate` event before the dimensions of padding on the text input
* are set.
*
* @name preInvalidate
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.preInvalidate
*/
EVENT_PRE_INVALIDATE = 'preInvalidate',
/**
* Core triggers `postInvalidate` event after the dimensions of padding on the text input
* are set.
*
* @name postInvalidate
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.postInvalidate
*/
EVENT_POST_INVALIDATE = 'postInvalidate',
/**
* Core triggers `getFormData` on every key press to collect data that will be populated
* into the hidden input that will be submitted with the HTML form and data that will
* be displayed in the input field that user is currently interacting with.
*
* All plugins that wish to affect how the data is presented or sent must react to
* `getFormData` and populate the data in the following format:
*
* {
* input : {String},
* form : {Object}
* }
*
* The data key must be a numeric weight which will be used to determine which data
* ends up being used. Data with the highest numerical weight gets the priority. This
* allows plugins to set the final data regardless of their initialization order, which
* otherwise would be impossible.
*
* For example, the Tags and Autocomplete plugins have to work side by side and Tags
* plugin must get priority on setting the data. Therefore the Tags plugin sets data
* with the weight 200 where as the Autocomplete plugin sets data with the weight 100.
*
* Here's an example of a typical `getFormData` handler:
*
* TextExtPlugin.prototype.onGetFormData = function(e, data, keyCode)
* {
* data[100] = self.formDataObject('input value', 'form value');
* };
*
* Core also reacts to the `getFormData` and updates hidden input with data which will be
* submitted with the HTML form.
*
* @name getFormData
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.getFormData
*/
EVENT_GET_FORM_DATA = 'getFormData',
/**
* Core triggers and reacts to the `setFormData` event to update the actual value in the
* hidden input that will be submitted with the HTML form. Second argument can be value
* of any type and by default it will be JSON serialized with `TextExt.serializeData()`
* function.
*
* @name setFormData
* @author agorbatchev
* @date 2011/08/22
* @id TextExt.events.setFormData
*/
EVENT_SET_FORM_DATA = 'setFormData',
/**
* Core triggers and reacts to the `setInputData` event to update the actual value in the
* text input that user is interacting with. Second argument must be of a `String` type
* the value of which will be set into the text input.
*
* @name setInputData
* @author agorbatchev
* @date 2011/08/22
* @id TextExt.events.setInputData
*/
EVENT_SET_INPUT_DATA = 'setInputData',
/**
* Core triggers `postInit` event to let plugins run code after all plugins have been
* created and initialized. This is a good place to set some kind of global values before
* somebody gets to use them. This is not the right place to expect all plugins to finish
* their initialization.
*
* @name postInit
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.postInit
*/
EVENT_POST_INIT = 'postInit',
/**
* Core triggers `ready` event after all global configuration and prepearation has been
* done and the TextExt component is ready for use. Event handlers should expect all
* values to be set and the plugins to be in the final state.
*
* @name ready
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.ready
*/
EVENT_READY = 'ready',
/**
* Core triggers `anyKeyUp` event for every key up event triggered within the component.
*
* @name anyKeyUp
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.anyKeyUp
*/
/**
* Core triggers `anyKeyDown` event for every key down event triggered within the component.
*
* @name anyKeyDown
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.anyKeyDown
*/
/**
* Core triggers `[name]KeyUp` event for every key specifid in the `keys` option that is
* triggered within the component.
*
* @name [name]KeyUp
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.[name]KeyUp
*/
/**
* Core triggers `[name]KeyDown` event for every key specified in the `keys` option that is
* triggered within the component.
*
* @name [name]KeyDown
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.[name]KeyDown
*/
/**
* Core triggers `[name]KeyPress` event for every key specified in the `keys` option that is
* triggered within the component.
*
* @name [name]KeyPress
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.events.[name]KeyPress
*/
DEFAULT_OPTS = {
itemManager : ItemManager,
plugins : [],
ext : {},
html : {
wrap : '<div class="text-core"><div class="text-wrap"/></div>',
hidden : '<input type="hidden" />'
},
keys : {
8 : 'backspace',
9 : 'tab',
13 : 'enter!',
27 : 'escape!',
37 : 'left',
38 : 'up!',
39 : 'right',
40 : 'down!',
46 : 'delete',
108 : 'numpadEnter'
}
}
;
// Freak out if there's no JSON.stringify function found
if(!stringify)
throw new Error('JSON.stringify() not found');
/**
* Returns object property by name where name is dot-separated and object is multiple levels deep.
* @param target Object Source object.
* @param name String Dot separated property name, ie `foo.bar.world`
* @id core.getProperty
*/
function getProperty(source, name)
{
if(typeof(name) === 'string')
name = name.split('.');
var fullCamelCaseName = name.join('.').replace(/\.(\w)/g, function(match, letter) { return letter.toUpperCase() }),
nestedName = name.shift(),
result
;
if(typeof(result = source[fullCamelCaseName]) != UNDEFINED)
result = result;
else if(typeof(result = source[nestedName]) != UNDEFINED && name.length > 0)
result = getProperty(result, name);
// name.length here should be zero
return result;
};
/**
* Hooks up specified events in the scope of the current object.
* @author agorbatchev
* @date 2011/08/09
*/
function hookupEvents()
{
var args = slice.apply(arguments),
self = this,
target = args.length === 1 ? self : args.shift(),
event
;
args = args[0] || {};
function bind(event, handler)
{
target.bind(event, function()
{
// apply handler to our PLUGIN object, not the target
return handler.apply(self, arguments);
});
}
for(event in args)
bind(event, args[event]);
};
function formDataObject(input, form)
{
return { 'input' : input, 'form' : form };
};
//--------------------------------------------------------------------------------
// ItemManager core component
p = ItemManager.prototype;
/**
* Initialization method called by the core during instantiation.
*
* @signature ItemManager.init(core)
*
* @param core {TextExt} Instance of the TextExt core class.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.init
*/
p.init = function(core)
{
};
/**
* Filters out items from the list that don't match the query and returns remaining items. Default
* implementation checks if the item starts with the query.
*
* @signature ItemManager.filter(list, query)
*
* @param list {Array} List of items. Default implementation works with strings.
* @param query {String} Query string.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.filter
*/
p.filter = function(list, query)
{
var result = [],
i, item
;
for(i = 0; i < list.length; i++)
{
item = list[i];
if(this.itemContains(item, query))
result.push(item);
}
return result;
};
/**
* Returns `true` if specified item contains another string, `false` otherwise. In the default implementation
* `String.indexOf()` is used to check if item string begins with the needle string.
*
* @signature ItemManager.itemContains(item, needle)
*
* @param item {Object} Item to check. Default implementation works with strings.
* @param needle {String} Search string to be found within the item.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.itemContains
*/
p.itemContains = function(item, needle)
{
return this.itemToString(item).toLowerCase().indexOf(needle.toLowerCase()) == 0;
};
/**
* Converts specified string to item. Because default implemenation works with string, input string
* is simply returned back. To use custom objects, different implementation of this method could
* return something like `{ name : {String} }`.
*
* @signature ItemManager.stringToItem(str)
*
* @param str {String} Input string.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.stringToItem
*/
p.stringToItem = function(str)
{
return str;
};
/**
* Converts specified item to string. Because default implemenation works with string, input string
* is simply returned back. To use custom objects, different implementation of this method could
* for example return `name` field of `{ name : {String} }`.
*
* @signature ItemManager.itemToString(item)
*
* @param item {Object} Input item to be converted to string.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.itemToString
*/
p.itemToString = function(item)
{
return item;
};
/**
* Returns `true` if both items are equal, `false` otherwise. Because default implemenation works with
* string, input items are compared as strings. To use custom objects, different implementation of this
* method could for example compare `name` fields of `{ name : {String} }` type object.
*
* @signature ItemManager.compareItems(item1, item2)
*
* @param item1 {Object} First item.
* @param item2 {Object} Second item.
*
* @author agorbatchev
* @date 2011/08/19
* @id ItemManager.compareItems
*/
p.compareItems = function(item1, item2)
{
return item1 == item2;
};
//--------------------------------------------------------------------------------
// TextExt core component
p = TextExt.prototype;
/**
* Initializes current component instance with work with the supplied text input and options.
*
* @signature TextExt.init(input, opts)
*
* @param input {HTMLElement} Text input.
* @param opts {Object} Options.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.init
*/
p.init = function(input, opts)
{
var self = this,
hiddenInput,
itemManager,
container
;
self._defaults = $.extend({}, DEFAULT_OPTS);
self._opts = opts || {};
self._plugins = {};
self._itemManager = itemManager = new (self.opts(OPT_ITEM_MANAGER))();
input = $(input);
container = $(self.opts(OPT_HTML_WRAP));
hiddenInput = $(self.opts(OPT_HTML_HIDDEN));
input
.wrap(container)
.keydown(function(e) { return self.onKeyDown(e) })
.keyup(function(e) { return self.onKeyUp(e) })
.data('textext', self)
;
// keep references to html elements using jQuery.data() to avoid circular references
$(self).data({
'hiddenInput' : hiddenInput,
'wrapElement' : input.parents('.text-wrap').first(),
'input' : input
});
// set the name of the hidden input to the text input's name
hiddenInput.attr('name', input.attr('name'));
// remove name attribute from the text input
input.attr('name', null);
// add hidden input to the DOM
hiddenInput.insertAfter(input);
$.extend(true, itemManager, self.opts(OPT_EXT + '.item.manager'));
$.extend(true, self, self.opts(OPT_EXT + '.*'), self.opts(OPT_EXT + '.core'));
self.originalWidth = input.outerWidth();
self.invalidateBounds();
itemManager.init(self);
self.initPatches();
self.initPlugins(self.opts(OPT_PLUGINS), $.fn.textext.plugins);
self.on({
setFormData : self.onSetFormData,
getFormData : self.onGetFormData,
setInputData : self.onSetInputData,
anyKeyUp : self.onAnyKeyUp
});
self.trigger(EVENT_POST_INIT);
self.trigger(EVENT_READY);
self.getFormData(0);
};
/**
* Initialized all installed patches against current instance. The patches are initialized based on their
* initialization priority which is returned by each patch's `initPriority()` method. Priority
* is a `Number` where patches with higher value gets their `init()` method called before patches
* with lower priority value.
*
* This facilitates initializing of patches in certain order to insure proper dependencies
* regardless of which order they are loaded.
*
* By default all patches have the same priority - zero, which means they will be initialized
* in rorder they are loaded, that is unless `initPriority()` is overriden.
*
* @signature TextExt.initPatches()
*
* @author agorbatchev
* @date 2011/10/11
* @id TextExt.initPatches
*/
p.initPatches = function()
{
var list = [],
source = $.fn.textext.patches,
name
;
for(name in source)
list.push(name);
this.initPlugins(list, source);
};
/**
* Creates and initializes all specified plugins. The plugins are initialized based on their
* initialization priority which is returned by each plugin's `initPriority()` method. Priority
* is a `Number` where plugins with higher value gets their `init()` method called before plugins
* with lower priority value.
*
* This facilitates initializing of plugins in certain order to insure proper dependencies
* regardless of which order user enters them in the `plugins` option field.
*
* By default all plugins have the same priority - zero, which means they will be initialized
* in the same order as entered by the user.
*
* @signature TextExt.initPlugins(plugins)
*
* @param plugins {Array} List of plugin names to initialize.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.initPlugins
*/
p.initPlugins = function(plugins, source)
{
var self = this,
ext, name, plugin, initList = [], i
;
if(typeof(plugins) == 'string')
plugins = plugins.split(/\s*,\s*|\s+/g);
for(i = 0; i < plugins.length; i++)
{
name = plugins[i];
plugin = source[name];
if(plugin)
{
self._plugins[name] = plugin = new plugin();
self[name] = (function(plugin) {
return function(){ return plugin; }
})(plugin);
initList.push(plugin);
$.extend(true, plugin, self.opts(OPT_EXT + '.*'), self.opts(OPT_EXT + '.' + name));
}
}
// sort plugins based on their priority values
initList.sort(function(p1, p2)
{
p1 = p1.initPriority();
p2 = p2.initPriority();
return p1 === p2
? 0
: p1 < p2 ? 1 : -1
;
});
for(i = 0; i < initList.length; i++)
initList[i].init(self);
};
/**
* Returns true if specified plugin is was instantiated for the current instance of core.
*
* @signature TextExt.hasPlugin(name)
*
* @param name {String} Name of the plugin to check.
*
* @author agorbatchev
* @date 2011/12/28
* @id TextExt.hasPlugin
* @version 1.1
*/
p.hasPlugin = function(name)
{
return !!this._plugins[name];
};
/**
* Allows to add multiple event handlers which will be execued in the scope of the current object.
*
* @signature TextExt.on([target], handlers)
*
* @param target {Object} **Optional**. Target object which has traditional `bind(event, handler)` method.
* Handler function will still be executed in the current object's scope.
* @param handlers {Object} Key/value pairs of event names and handlers, eg `{ event: handler }`.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.on
*/
p.on = hookupEvents;
/**
* Binds an event handler to the input box that user interacts with.
*
* @signature TextExt.bind(event, handler)
*
* @param event {String} Event name.
* @param handler {Function} Event handler.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.bind
*/
p.bind = function(event, handler)
{
this.input().bind(event, handler);
};
/**
* Triggers an event on the input box that user interacts with. All core events are originated here.
*
* @signature TextExt.trigger(event, ...args)
*
* @param event {String} Name of the event to trigger.
* @param ...args All remaining arguments will be passed to the event handler.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.trigger
*/
p.trigger = function()
{
var args = arguments;
this.input().trigger(args[0], slice.call(args, 1));
};
/**
* Returns instance of `itemManager` that is used by the component.
*
* @signature TextExt.itemManager()
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.itemManager
*/
p.itemManager = function()
{
return this._itemManager;
};
/**
* Returns jQuery input element with which user is interacting with.
*
* @signature TextExt.input()
*
* @author agorbatchev
* @date 2011/08/10
* @id TextExt.input
*/
p.input = function()
{
return $(this).data('input');
};
/**
* Returns option value for the specified option by name. If the value isn't found in the user
* provided options, it will try looking for default value.
*
* @signature TextExt.opts(name)
*
* @param name {String} Option name as described in the options.
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.opts
*/
p.opts = function(name)
{
var result = getProperty(this._opts, name);
return typeof(result) == 'undefined' ? getProperty(this._defaults, name) : result;
};
/**
* Returns HTML element that was created from the `html.wrap` option. This is the top level HTML
* container for the text input with which user is interacting with.
*
* @signature TextExt.wrapElement()
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.wrapElement
*/
p.wrapElement = function()
{
return $(this).data('wrapElement');
};
/**
* Updates container to match dimensions of the text input. Triggers `preInvalidate` and `postInvalidate`
* events.
*
* @signature TextExt.invalidateBounds()
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.invalidateBounds
*/
p.invalidateBounds = function()
{
var self = this,
input = self.input(),
wrap = self.wrapElement(),
container = wrap.parent(),
width = self.originalWidth + 'px',
height
;
self.trigger(EVENT_PRE_INVALIDATE);
height = input.outerHeight() + 'px';
// using css() method instead of width() and height() here because they don't seem to do the right thing in jQuery 1.8.x
// https://github.com/alexgorbatchev/jquery-textext/issues/74
input.css({ 'width' : width });
wrap.css({ 'width' : width, 'height' : height });
container.css({ 'height' : height });
self.trigger(EVENT_POST_INVALIDATE);
};
/**
* Focuses user input on the text box.
*
* @signature TextExt.focusInput()
*
* @author agorbatchev
* @date 2011/08/19
* @id TextExt.focusInput
*/
p.focusInput = function()
{