-
Notifications
You must be signed in to change notification settings - Fork 60
/
jquery.jkit.1.2.16.js
6101 lines (4266 loc) · 176 KB
/
jquery.jkit.1.2.16.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 Plugin: jKit
//
// > A very easy to use, cross platform jQuery UI toolkit that's still small in size, has the
// > features you need and doesn't get in your way.
//
// Put jQuery and jKit on all your pages and HTML becomes so much better. And the best thing?
// You really don't have to be a programmer to create a trully amazing website!
//
// jKit has 99% of all the features you ever need. You don't have to check out dozens of plugins,
// learn how to use them, only to find out they don't work in a specific browser.
//
// And even if jKit doesn't have that one feature you need right now, jKit is fully extendable
// with plugins and command replacements, all that and your API always stays the same.
//
// - Version: `1.2.16`
// - Release date: `10. 5. 2013`
// - [API Documentation & Demos](http://jquery-jkit.com/)
// - [Source Documentation](http://jquery-jkit.com/sourcemakeup/?file=js/jquery.jkit.1.2.16.js) (made
// with [sourceMakeup](http://jquery-jkit.com/sourcemakeup))
// - [Download](https://github.com/FrediBach/jQuery-jKit/archive/master.zip)
//
// ## Copyright
//
// - (c) 2012/2013 by *Fredi Bach*
// - [Home](http://fredibach.ch/)
//
// ## License
//
// jKit is open source and MIT licensed. For more informations read the **license.txt** file
//
// ## Basic Usage
//
// Inside your head tag or at the bottom of your page before the closing body tag:
//
// <script src="js/jquery-1.9.1.min.js"></script>
// <script src="js/jquery.easing.1.3.js"></script>
// <script src="js/jquery.jkit.1.2.16.min.js"></script>
//
// <script type="text/javascript">
// $(document).ready(function(){
// $('body').jKit();
// });
// </script>
//
// On your HTML elements:
//
// <p data-jkit="[hide:delay=2000;speed=500]">
// Hidden after two seconds
// </p>
//
//
// ## The Source
// Create our plugins local scope, make sure **$** is mapped to jQuery and guarantee that **undefined** really is **undefined**.
(function($, undefined) {
// Create our main function with the following parameters:
//
// - **element** contains the DOM element where jKit is applied to
// - **options** is either a string with a single command name or a JavaScript object with all options or undefined
// - **moreoptions** is optionally used in case options contains the a command string and contains the options object
$.jKit = function(element, options, moreoptions) {
// Define all plugin defaults. These can be overwritten by the plugins options set on init.
var defaults = {
// First we set some general defaults:
prefix: 'jkit',
dataAttribute: 'data-jkit',
activeClass: 'active',
errorClass: 'error',
successClass: 'success',
ignoreFocus: false,
ignoreViewport: false,
keyNavigation: true,
touchNavigation: true,
plugins: {},
replacements: {},
delimiter: ',',
loadminified: true,
// {!} codeblock: macros
// Now we set some default macros for often used command/parameter combinations:
macros: {
'hide-if-empty': 'binding:selector=this;source=text;mode=css.display',
'smooth-blink': 'loop:speed1=2000;duration1=250;speed2=250;duration2=2000'
},
// {!} codeblock: /macros
// Next we're defining all the default options for each command. You can get a good overview of them
// on the official [cheat sheet](http://jquery-jkit.com/pages/cheatsheet.php).
commands: {}
};
// Set an alias to **this** so that we can use it everywhere inside our plugin:
var plugin = this;
// Define some info variables that can be read with the special info command:
plugin.version = '1.2.16';
plugin.inc = [];
// Create an object for the plugin settings:
plugin.settings = {};
// Create an opject to store all jKit command implementations
plugin.commands = {};
// Array to stor all command execution so that we can find out if something was already executed
plugin.executions = {};
// And while were're at it, cache the DOM element:
var $element = $(element),
element = element;
// In case we are just applying a single command, we need to take the options from the **moreoptions** parameter:
if (typeof options == 'string'){
var singlecommand = options;
if (moreoptions == undefined){
moreoptions = {};
}
options = moreoptions;
}
// For a few things, we need some local plugin variables and objects, let's set them now:
var startX, startY;
var windowhasfocus = true;
var uid = 0;
var commandkeys = {};
// We want to know if the current window is in focus or not, we can do this with the **window** object (just not in IE7 & 8):
if ($.support.htmlSerialize || $.support.opacity){
$(window).focus(function() {
windowhasfocus = true;
}).blur(function() {
windowhasfocus = false;
});
}
// ## Plugin Functions
// The following collection of functions are internally used. There is a way to call them with an external script,
// **but you should know what you're doing!** Here's an exmaple:
//
// $('body').data('jKit').executeCommand('body', 'lightbox');
//
// The above code would call the **plugin.executeCommand()** function.
// ### init
//
// The **init** function is called on plugin init and sets up all the stuff we need.
plugin.init = function($el){
// In case this function is called without a specific DOM node, use the plugins main DOM element:
if ($el == undefined) $el = $element;
// Extend the plugin defaults with the applied options:
plugin.settings = $.extend({}, defaults, options);
var s = plugin.settings;
if (singlecommand != undefined){
// If this is an initialization of a single command, all we have to do is execute that one command:
plugin.executeCommand($el, singlecommand, options);
} else {
// It's now time to find all DOM nodes that want to execute a jKit command. You can either use the **data-jkit** attribute,
// or the **rel** attribute. **However, we strongly recommend to use the data-jkit attribute!** The rel attribute support
// will probably removed at some point.
$el.find("*[rel^=jKit], *["+s.dataAttribute+"]").each( function(){
var that = this;
// Get the rel or data-jkit attribute and extract all individual commands (they have to be inside square brackets):
var rel = $(this).attr('rel');
var data = plugin.getDataCommands($(this));
if (data != ''){
rel = $.trim(data).substring(1);
} else {
rel = $.trim(rel).substring(5);
}
rel = rel.substring(0, rel.length-1);
rel = rel.replace(/\]\s+\[/g, "][");
relsplit = rel.split('][');
// Now look at each command seperately:
$.each( relsplit, function(index, value){
// First convert all the escaped characters into internal jKit strings. Later we convert them back and unescape them.
value = value
.replace(/\\=/g,'|jkit-eq|')
.replace(/\\:/g,'|jkit-dp|')
.replace(/\\;/g,'|jkit-sc|')
.replace(/\\\[/g,'|jkit-sbo|')
.replace(/\\\]/g,'|jkit-sbc|')
.replace(/\\\*/g,'|jkit-st|')
.replace(/\\ /g,'|jkit-sp|');
value = $.trim(value);
// Is this a macro call? Let's check if we find a macro with this name:
if (s.macros[value] != undefined) value = s.macros[value];
// Now it's time to parse the options:
var options = plugin.parseOptions(value);
// It's still possible that this is a macro, just with changed options. Let's check that and apply the macro if needed:
if (s.macros[options.type] != undefined){
var macrooptions = plugin.parseOptions(s.macros[options.type]);
options.type = macrooptions.type;
options = $.extend({}, macrooptions, options);
}
// If this is a macro definition, add the current command string to the macros array:
if (options.type == 'macro' && relsplit[index-1] != undefined){
plugin.settings.macros[options.name] = relsplit[index-1];
// If this is the special repeat command, parse the options and than add it to the delivered event handler:
} else if (options.type == 'repeat' && relsplit[index-1] != undefined){
var prevoptions = plugin.parseOptions(relsplit[index-1]);
$el.on( options.onevent, function(){
if (options.delay == undefined) options.delay = 0;
setTimeout( function(){
plugin.executeCommand($(that), prevoptions.type, prevoptions);
}, options.delay);
});
} else if (options.type == 'info'){
var output = 'jKit version: '+plugin.version+'\n';
output += 'Included commands: '+plugin.inc.join(', ')+'\n';
console.log(output);
console.log($el);
} else {
// Looks like this isn't one of the special use commands, so lets execute one of the regular ones.
// If the targets option is set, we first have to find out to which target nodes we have to apply the command:
var targets = [];
if (options.target != undefined){
var targetsplit = options.target.split('.');
targetsplit = [targetsplit.shift(), targetsplit.join('.')]
if (targetsplit[1] == undefined){
targetsplit[1] = '*';
}
switch(targetsplit[0]){
case 'children':
$(that).children(targetsplit[1]).each( function(){
targets.push(this);
});
break;
case 'each':
$(that).find(targetsplit[1]).each( function(){
targets.push(this);
});
break;
default:
targets.push(that);
}
} else {
targets.push(that);
}
$.each( targets, function(i,v){
// First parse all dynamic options. They are declared like this:
//
// [command:myoption={rand|0-1000}]
//
var thisoptions = plugin.parseDynamicOptions(options);
// Now it's time to find out what the command key is for this specific command call.
// This can be set either by the commandkey option, the dot syntax or if both are not
// set, we take the elements id attribute or as a last option, we just generate an unique id.
if (thisoptions.commandkey == undefined){
var id = $(that).attr("id");
if (id != undefined){
thisoptions.commandkey = id;
} else {
thisoptions.commandkey = s.prefix+'-uid-'+(++uid);
}
}
// Now as we have the commandkey, we store it in the plugins commandkey array together
// with some other useful information for later use:
if (thisoptions.commandkey != undefined){
commandkeys[thisoptions.commandkey] = {
'el': v,
'options': thisoptions,
'execs': 0
};
}
// Next we need to check if we have to immediately execute the command or if we have to
// execute it later on a specific event:
if (thisoptions.onevent !== undefined || thisoptions.andonevent !== undefined){
var events = [];
if (thisoptions.onevent !== undefined) events.push(thisoptions.onevent);
if (thisoptions.andonevent !== undefined) events.push(thisoptions.andonevent);
var e = events.join(' ');
$el.on( e, function(){
if (s.replacements[thisoptions.type] != undefined && typeof(s.replacements[thisoptions.type]) === "function"){
s.replacements[thisoptions.type].call(plugin, v, thisoptions.type, thisoptions);
} else {
plugin.executeCommand(v, thisoptions.type, thisoptions);
}
});
}
if (thisoptions.onevent === undefined){
// If this is a command that follows another command we need to make sure that
// the command before this one in the command chain has already been executed:
if (relsplit[index-1] != undefined){
var prevcmd = '';
if (relsplit[(index-1)] != undefined){
var prevoptions = plugin.parseOptions(relsplit[index-1]);
prevcmd = prevoptions.type + '.' + thisoptions.commandkey + '.executed';
}
if (prevcmd != '' && plugin.executions[prevoptions.type + '.' + thisoptions.commandkey + '.executed'] === undefined){
$el.on( prevcmd, function(){
if (s.replacements[thisoptions.type] != undefined && typeof(s.replacements[thisoptions.type]) === "function"){
s.replacements[thisoptions.type].call(plugin, v, thisoptions.type, thisoptions);
} else {
plugin.executeCommand(v, thisoptions.type, thisoptions);
}
});
} else {
if (s.replacements[thisoptions.type] != undefined && typeof(s.replacements[thisoptions.type]) === "function"){
s.replacements[thisoptions.type].call(plugin, v, thisoptions.type, thisoptions);
} else {
plugin.executeCommand(v, thisoptions.type, thisoptions);
}
}
} else {
// If we don't have an event set, we execute it immediately. Wee need to
// check if a command replacement for this command is available and if yes, call it.
if (s.replacements[thisoptions.type] != undefined && typeof(s.replacements[thisoptions.type]) === "function"){
s.replacements[thisoptions.type].call(plugin, v, thisoptions.type, thisoptions);
} else {
plugin.executeCommand(v, thisoptions.type, thisoptions);
}
}
}
});
}
});
});
}
};
// ### getDataCommands
//
// The **getDataCommands** function returns all jKit data element values that have to be executed. They can be spread over multiple
// attributes with different values for different element sizes (responsive):
//
// <div data-jkit="[tabs]" data-jkit-lt-500-width="[show]" data-jkit-gt-499-width="[hide]">
// ...
// </div>
plugin.getDataCommands = function($el){
var s = plugin.settings;
var el = $el.get(0);
var commands = '';
for (var i=0, attrs=el.attributes, l=attrs.length; i<l; i++){
var attr = attrs.item(i).nodeName;
var attrsplit = attr.split('-');
if ( attrsplit[0] + '-' + attrsplit[1] == s.dataAttribute ){
if (attrsplit.length > 2){
if (attrsplit[4] !== undefined && attrsplit[4] == 'height'){
var size = $el.height();
} else {
var size = $el.width();
}
if ( attrsplit[2] !== undefined && attrsplit[3] !== undefined && (
(attrsplit[2] == 'gt' && size > parseInt(attrsplit[3]))
|| (attrsplit[2] == 'lt' && size < parseInt(attrsplit[3])) )
){
commands += $el.attr(attr);
}
} else {
commands += $el.attr(attr);
}
}
}
return commands;
}
// ### applyMacro
//
// The **applyMacro** function lets us execute predefined macros.
plugin.applyMacro = function($el, macro){
var s = plugin.settings;
if (s.macros[macro] != undefined){
var value = s.macros[macro];
var options = plugin.parseOptions(value);
if (s.replacements[options.type] != undefined && typeof(s.replacements[options.type]) === "function"){
s.replacements[options.type].call(plugin, $el, options.type, options);
} else {
plugin.executeCommand($el, options.type, options);
}
}
};
// ### parseOptions
//
// The **parseOptions** function takes a command string and creates an array out of it with all options.
// It automatically detects the command type and command key. An input string can look like this
// (optionally with additional spaces and newlines):
//
// mycommand.mykey:firstoption=value1;secondoption=value2
//
plugin.parseOptions = function(string){
var relsplit = string.split(':');
var commandsplit = relsplit[0].split('.');
var options = { 'type': $.trim(commandsplit[0]) };
if (commandsplit[1] !== undefined){
options['commandkey'] = commandsplit[1];
}
if (options.execute == undefined){
options.execute = 'always';
}
if (relsplit.length > 1){
var optionssplit = relsplit[1].split(';');
$.each( optionssplit, function(key, value){
var optionssplit2 = value.split('=');
options[$.trim(optionssplit2[0])] = $.trim(optionssplit2[1]);
});
}
return options;
};
// ### fixSpeed
//
// The **fixSpeed** function is used to make sure that a speed option has a correct value, either
// "slow", "fast" or an integer.
plugin.fixSpeed = function(speed){
if (speed != 'fast' && speed != 'slow'){
speed = parseInt(speed);
}
return speed;
};
// ### parseDynamicOptions
//
// The **parseDynamicOptions** looks for dynamic options that look like this:
//
// [command:myoption={rand|0-1000}]
//
// Currently only the random options are supported, but more stuff is planned, like increase or decrease.
plugin.parseDynamicOptions = function(options){
var parsedoptions = {};
for (index in options){
var v = options[index];
if (v !== undefined && v.indexOf("{") > -1 && v.indexOf("|") > 0 && v.indexOf("}") > 1){
var option = '';
var dyn = false;
var dynstr = '';
var parse = false;
for (var i=0; i<=(v.length-1);i++){
if (!dyn && v.charAt(i) == '{'){
dyn = true;
} else if (dyn && v.charAt(i) == '}'){
dyn = false;
parse = true;
}
if (dyn || parse){
dynstr += v.charAt(i);
if (parse){
dynstr = dynstr.slice(1, -1);
var dynsplit = dynstr.split('|');
if (dynsplit[0] == 'rand'){
var valsplit = dynsplit[1].split('-');
option += plugin.getRandom(Number(valsplit[0]), Number(valsplit[1]));
}
parse = false;
dynstr = '';
}
} else {
option += v.charAt(i);
}
}
parsedoptions[index] = option;
} else {
parsedoptions[index] = v;
}
}
return parsedoptions;
}
// ### getRandom
//
// The **getRandom** function simply generates a random number between a minimum number and a maximum number.
plugin.getRandom = function(min, max) {
if(min > max) {
return -1;
}
if(min == max) {
return min;
}
var r;
do {
r = Math.random();
}
while(r == 1.0);
return min + parseInt(r * (max-min+1));
}
// ### findElementTag
//
// The **findElementTag** function makes it possible to find the tag name of a specific element in a
// previously defined structure. This makes it possible to write agnostic HTML for tab or similar structures.
// For example on the tab command, both this structures would be succesfully detected:
//
// div (container)
// div (element)
// h3 (title)
// div (content)
//
// ul (container)
// li (element)
// h2 (title)
// p (content)
//
// Check the tab command to get an example of how to use the function.
plugin.findElementTag = function($container, selector, pos, defaultval){
var output = '';
if ( pos !== undefined && !isNaN(pos) && parseInt(pos) == pos ){
if ($container.find(selector).length > pos){
output = $($container.find(selector).get(pos)).prop('tagName');
}
} else {
var tags = {};
$container.find(selector).each( function(i){
if (i < 25){
var tag = $(this).prop('tagName');
if (tag[0] != ''){
if (tags[tag] !== undefined){
tags[tag]++;
} else {
tags[tag] = 1;
}
}
} else {
return false;
}
});
var max = 0;
var maxkey = '';
for (var key in tags){
if (tags[key] > max){
max = tags[key];
maxkey = key;
}
}
output = maxkey;
}
if (output !== undefined && output != ''){
return output;
} else {
return defaultval;
}
};
// ### addDefaults
//
// The **addDefaults** function adds all the default options to the options array. Additionally
// all speed options are fixed if needed.
plugin.addDefaults = function(command, options){
if (plugin.settings.commands[command] != undefined){
var c = plugin.settings.commands[command];
$.each(c, function(i, v){
if (options[i] == undefined) options[i] = v;
if (i.indexOf('speed') > -1) options[i] = plugin.fixSpeed(options[i]);
});
}
return options;
};
// ### executeCommand
//
// The **executeCommand** function is used to execute a command on a specific DOM node with an array of options.
plugin.executeCommand = function(that, type, options){
// First create a few shortcuts:
var s = plugin.settings;
var $that = $(that);
// Create a temporary array in case this command isn't already implemented or loaded. The array acts as a
// command queue that stores all executions of this command till the command is actually loaded.
if (plugin.commands[type] === undefined){
plugin.commands[type] = [];
}
// As long as this plugin command is an array, we know that it isn't loaded already, so load it!
if ($.isArray(plugin.commands[type])){
// Craete an entry in the command queue with the current element and options:
plugin.commands[type].push({
'el': that,
'options': options
});
// We only have to start the ajax in case this was the first command in the queue:
if (s.loadminified){
var commandurl = 'jquery.jkit.commands/' + type + '.min.js';
} else {
var commandurl = 'jquery.jkit.commands/' + type + '.js';
}
if (plugin.commands[type].length == 1){
$.ajax({
url: 'jquery.jkit.commands/' + type + '.js',
success: function(data){
// The script loaded succesfully! Store the queue in a temporary array and than eval the
// loaded script. This way it will be loaded in the current scope and add itself to the plugin
// object. The jQuery.getScript method would load it globally, that's of no use to us.
if (data.indexOf('plugin.commands.') !== -1){
var queue = plugin.commands[type];
eval(data);
// Now execute all commands in our queue:
$.each(queue, function(i,v){
plugin.executeCommand(v.el, type, v.options);
});
}
},
dataType: "text"
});
}
// We can stop this function now. It will be called again with the same options as soon as the command is loaded.
return $that;
}
// Everything below here will be executes if the command is loaded.
// Trigger the **jkit-commandinit** event on the main element with all useful information attached to it.
// This event is currently not used internally, but can of course be listened to from outside the plugin.
$element.trigger('jkit-commandinit', { 'element': $that, 'type': type, 'options': options });
// Check if there is a limit set on how many times we're allowed to execute this command (based on the command key)
if (options.commandkey !== undefined){
commandkeys[options.commandkey]['execs']++;
if ((options.execute == 'once' && commandkeys[options.commandkey]['execs'] > 1) || (!isNaN(options.execute) && commandkeys[options.commandkey]['execs'] > options.execute)){
return $that;
}
}
// Add all default options where there isn't an option set:
options = plugin.addDefaults(type, options);
$.each( options, function(i,v){
// Convert jKit's special escaping strings to their regular characters:
if (typeof v == 'string'){
options[i] = v = v
.replace(/\|jkit\-eq\|/g,'=')
.replace(/\|jkit\-dp\|/g,':')
.replace(/\|jkit\-sc\|/g,';')
.replace(/\|jkit\-sbo\|/g,'[')
.replace(/\|jkit\-sbc\|/g,']')
.replace(/\|jkit\-st\|/g,'*')
.replace(/\|jkit\-sp\|/g,' ');
}
// Call or get all dynamic options (those with an asterix at the end):
if (typeof v == 'string' && v.slice(-1) == '*'){
options[i] = window[v.slice(0, -1)];
if (typeof options[i] == 'function'){
options[i] = options[i].call(that);
}
}
});
// Execute the commands main function:
plugin.commands[ type ].execute($that, options);
if (type != 'remove'){
$element.trigger(type + '.' + options.commandkey + '.executed', {});
plugin.executions[type + '.' + options.commandkey + '.executed'] = true;
}
return $that;
};
// ### triggerEvent
//
// The **triggerEvent** function is used by various commands to trigger an event on an element with
// the commands options added to it:
plugin.triggerEvent = function(event, $el, options){
if (options.commandkey !== undefined){
var eventsplit = event.split(' ');
$.each( eventsplit, function(i,v){
$element.trigger(options.commandkey+'.'+v, { 'element': $el, 'options': options });
});
}
};
// ### cssFromString
//
// The **cssFromString** function is used by the animation command. It parses a specially formated string
// and creates an object that contains all CSS data. Here's an exmaple of the string format:
//
// width(50%),height(50px)
//
plugin.cssFromString = function(css){
var partsplit = css.split(',');
var cssdata = {};
$.each( partsplit, function(i,v){
var innersplit = v.split('(');
if (innersplit.length == 2){
var property = innersplit[0];
var value = innersplit[1].slice(0,-1);
cssdata[property] = value;
}
});
return cssdata;
};
plugin.addCommandDefaults = function(c, d){
// Add the defaults:
defaults.commands[c] = d;
// And trigger the *loaded event* for this command (command.name.loaded) so everyone knows that this command is ready:
$element.trigger('command.'+c+'.loaded', {});
};
// ### addKeypressEvents
//
// The **addKeypressEvents** function is used by the key command and various other features and adds a specific keycode event
// to an element.
plugin.addKeypressEvents = function($el, code){
// Check first if key navigations aren't globally switched off:
if (plugin.settings.keyNavigation){
// Listen to the documents keydown event:
$(document).keydown(function(e){
// Only add the event if this isn't a textaream, select or text input:
if ( this !== e.target && (/textarea|select/i.test( e.target.nodeName ) || e.target.type === "text") ) return;
// Map keycodes to their identifiers:
var keys = {
8: "backspace",
9: "tab",
13: "return",
16: "shift",
17: "ctrl",
18: "alt",
19: "pause",
20: "capslock",
27: "esc",
32: "space",
33: "pageup",
34: "pagedown",
35: "end",
36: "home",
37: "left",
38: "up",
39: "right",
40: "down",
45: "insert",
46: "del",
96: "0",
97: "1",
98: "2",
99: "3",
100: "4",
101: "5",
102: "6",
103: "7",
104: "8",
105: "9",
106: "*",
107: "+",
109: "-",
110: ".",
111 : "/",
112: "f1",
113: "f2",
114: "f3",
115: "f4",
116: "f5",
117: "f6",
118: "f7",
119: "f8",
120: "f9",
121: "f10",
122: "f11",
123: "f12",
144: "numlock",
145: "scroll",
191: "/",
224: "meta"
};
// Add the alphabet:
for(var i=48; i<=90; i++){
keys[i] = String.fromCharCode(i).toLowerCase();
}
if ($.inArray(e.which, keys)){
// Add special keys:
var special = '';
if (e.altKey) special += 'alt+';
if (e.ctrlKey) special += 'ctrl+';
if (e.metaKey) special += 'meta+';
if (e.shiftKey) special += 'shift+';
var keycode = special+keys[e.which];
// If the code matches, trigger the event:
if (keycode == code){
$el.trigger(special+keys[e.which]);
e.preventDefault();
}
}
});
}
}
// ## jKit Commands