-
Notifications
You must be signed in to change notification settings - Fork 0
/
performer.js
2500 lines (2498 loc) · 104 KB
/
performer.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
/*
Performer JavaScript library (http://performerjs.org)
Created by Chris Taylor (http://www.stillbreathing.co.uk)
Additional work by kourge and Danny Linkov
Version 1.0.3
This work is released under any of the following licenses, please choose the one you wish to use:
- Creative Commons Attribution-ShareAlike 3.0 licence (http://creativecommons.org/licenses/by-sa/3.0/)
- Microsoft Public License (http://www.opensource.org/licenses/ms-pl.html)
- MIT License (http://www.opensource.org/licenses/mit-license.php)
- BSD License (http://www.opensource.org/licenses/bsd-license.php)
*/
var Performer =
{
version: '1.0.3',
Scriptaculous: false,
Prototype: false,
jQuery: false,
MooTools: false,
Counter: 0,
Performer: function() {
P.DetectLibrary();
if (P.Scriptaculous || P.Prototype || P.jQuery || P.MooTools) { P.Start(); }
},
// detect the JavaScript Library in use
DetectLibrary: function() {
if (window.Scriptaculous && typeof window.Scriptaculous != 'undefined') {
P.Scriptaculous = true;
} else if (window.Prototype && typeof window.Prototype != 'undefined' && window.Prototype.Version) {
P.Prototype = true;
} else if (window.MooTools && typeof window.MooTools != 'undefined' && window.MooTools.version) {
P.MooTools = true;
} else if (window.jQuery && typeof window.jQuery != 'undefined') {
P.jQuery = true;
}
},
Start: function() {
P.domLoaded(function() {
// set debugging
P.Debugging = false;
P.Debug('Performer.Performer', 'function');
// set up global variables
P.Reloaders = [];
P.Repeaters = [];
P.Groups = null;
P.Duplicators = [];
P.TextValue = [];
P.NewTextValue = [];
P.Hash = parent.location.hash.replace(new RegExp('^[#]+', 'g'), '');
P.Effects = ['slideup', 'slidedown', 'blinddown', 'blindup', 'fadein', 'fadeout'];
// initialise the app
P.Init();
});
},
Init: function() {
P.CheckDebug();
P.Debug('Performer.Init', 'function');
// do the transformations
P.DoTransformers();
// set up listeners
P.DoListeners();
// set the body class
P.addClassName(P.$$('body')[0], 'performer-enabled');
},
ReInit: function(el) {
P.Debug('Performer.ReInit', 'function');
// do the transformations
P.DoTransformers(el, true);
// set up listeners
P.DoListeners(el, true);
},
DoListeners: function(el, reinit) {
if (reinit === undefined) { reinit = false; }
if (el === undefined) { el = ''; }
P.Listeners(el, 'form.formchecker', 'CheckForm', 'submit', reinit);
P.Listeners(el, '.toggler', 'Toggle', 'click,keypress', reinit);
P.Listeners(el, '.switcher', 'Switch', 'click,keypress', reinit);
P.Listeners(el, '.loader', 'Load', 'click,keypress', reinit);
P.Listeners(el, '.deleter', 'Delete', 'click,keypress', reinit);
P.Listeners(el, '.toggleloader', 'ToggleLoad', 'click,keypress', reinit);
P.Listeners(el, '.sizer', 'Size', 'click,keypress', reinit);
P.Listeners(el, '.resizer', 'Resize', 'keypress', reinit);
P.Listeners(el, '.tabber', 'Tab', 'click,keypress', reinit);
P.Listeners(el, '.accordianer', 'Accordian', 'click,keypress', reinit);
P.Listeners(el, '.selector', 'Select', 'change,keypress', reinit);
P.Listeners(el, '.limiter', 'Limit', 'keyup,keydown', reinit);
P.Listeners(el, '.editor', 'Edit', 'click,keypress', reinit);
P.Listeners(el, '.uneditor', 'UnEdit', 'click,keypress', reinit);
P.Listeners(el, '.setter', 'Set', 'click,keypress', reinit);
P.Listeners(el, '.prompter', 'RemovePrompt', 'focus', reinit);
P.Listeners(el, '.prompter', 'CheckPrompt', 'blur', reinit);
P.Listeners(el, '.popper', 'Pop', 'click,keypress', reinit);
P.Listeners(el, '.passwordchecker', 'CheckPassword', 'keyup', reinit);
P.Listeners(el, '.matcher', 'Match', 'keyup', reinit);
P.Listeners(el, '.grouptoggler', 'GroupToggle', 'click,keypress', reinit);
P.Listeners(el, '.submitter', 'Submit', 'submit', reinit);
P.Listeners(el, '.looperforward', 'Loop', 'click,keypress', reinit);
P.Listeners(el, '.looperback', 'Loop', 'click,keypress', reinit);
P.Listeners(el, '.looperfirst', 'Loop', 'click,keypress', reinit);
P.Listeners(el, '.looperlast', 'Loop', 'click,keypress', reinit);
P.Listeners(el, '.looperitem', 'Loop', 'click,keypress', reinit);
P.Listeners(el, '.tooltipper', 'Tooltip', 'mouseover,focus', reinit);
P.Listeners(el, '.popup', 'Tooltip', 'mouseover,focus', reinit);
P.Listeners(el, '.modalwindower', 'ModalWindow', 'click,keypress', reinit);
P.Listeners(el, '.modalwindowcloser', 'CloseModal', 'click,keypress', reinit);
P.Listeners(el, '.contextmenuer', 'ContextMenu', 'mouseup', reinit);
P.Listeners(el, '.styler', 'Style', 'click,keypress', reinit);
P.Listeners(el, '.duplicator', 'Duplicate', 'click,keypress', reinit);
P.Listeners(el, '.morpher', 'Morph', 'click,keypress', reinit);
P.Listeners(el, 'form.submitlocker', 'SubmitLock', 'submit', reinit);
P.Listeners(el, 'a.toggle-performer-debug', 'ToggleDebug', 'click,keypress', reinit);
// hooker listeners
P.Listeners(el, '.hooker-click', 'Hooker', 'click,keypress', reinit);
P.Listeners(el, '.hooker-keypress', 'Hooker', 'keypress', reinit);
P.Listeners(el, '.hooker-change', 'Hooker', 'change', reinit);
P.Listeners(el, '.hooker-mouseover', 'Hooker', 'mouseover', reinit);
P.Listeners(el, '.hooker-mouseout', 'Hooker', 'mouseout', reinit);
P.Listeners(el, '.hooker-submit', 'Hooker', 'submit', reinit);
P.Listeners(el, '.hooker-focus', 'Hooker', 'focus', reinit);
P.Listeners(el, '.hooker-blur', 'Hooker', 'blur', reinit);
},
DoTransformers: function(el, reinit) {
if (reinit === undefined) { reinit = false; }
if (el === undefined || el === 'performerjsdebugwrapper') { el = ''; }
P.Transformers(el, '.hider', 'Hide', reinit);
P.Transformers(el, '.shower', 'Show', reinit);
P.Transformers(el, '.focusser', 'Focus', reinit);
P.Transformers(el, '.limiter', 'LimitNotifier', reinit);
P.Transformers(el, '.reloader', 'Reload', reinit);
P.Transformers(el, '.preloader', 'Preload', reinit);
P.Transformers(el, '.prompter', 'SetPrompt', reinit);
P.Transformers(el, '.truncator', 'Truncate', reinit);
P.Transformers(el, 'ul.looper,ol.looper', 'InitLoop', reinit);
P.Transformers(el, '.tab', 'InitTabs', reinit);
P.Transformers(el, '.contextmenuer', 'DisableContextMenu', reinit);
P.Transformers(el, '.pager', 'Page', reinit);
P.Transformers(el, '.loadmorpher', 'Morph', reinit);
P.Transformers(el, '.loadtoggler', 'Toggle', reinit);
P.Transformers(el, '.loadgrouptoggler', 'GroupToggle', reinit);
P.Transformers(el, '.loadstyler', 'Style', reinit);
P.Transformers(el, '.loadmodalwindower', 'ModalWindow', reinit);
},
// listen for the required classnames
Listeners: function(el, classNames, f, event, reinit) {
var b = P.bind;
var n = P.nodeName;
var fe = P.forEach;
var d = P.$$;
var dbg = P.Debug;
var cls = classNames.split(',');
fe(cls, function(className) {
if (el !== undefined) { el = el + ' '; }
var els = d('body ' + el + className);
if (els) {
dbg('Performer.Listeners(' + el + className + ') - ' + els.length + ' elements found', 'function');
fe(els, function(element) {
if (n(element)) {
fe(event.split(','), function(event) {
b(element, event, P[f]);
});
}
});
}
});
},
// transform the required classnames
Transformers: function(el, classNames, f, reinit) {
var n = P.nodeName;
var fe = P.forEach;
var d = P.$$;
var dbg = P.Debug;
var cls = classNames.split(',');
fe(cls, function(className) {
if (el !== undefined) { el = el + ' '; }
var els = d('body ' + el + className);
if (els) {
dbg('Performer.Transformers(' + el + className + ') - ' + els.length + ' elements found', 'function');
fe(els, function(element) {
if (n(element)) {
P[f](element);
}
});
}
});
},
// check if the debug class is set
CheckDebug: function() {
P.Debug('Performer.CheckDebug', 'function');
var d = P.$$('.performer-debug');
if (d) {
P.Debugging = true;
P.PrepareDebug();
}
},
// get a parameter from an array of class names
classParam: function(classNames, paramName, defaultValue) {
var cls = null;
var val = defaultValue;
var i = classNames.length;
while(i--) {
cls = classNames[i];
if (cls && cls.substring(0, paramName.length + 1) == paramName + '-') { val = cls.replace(paramName + '-', ''); }
}
return val;
},
// runs a custom function on an event
Hooker: function(e) {
// get the event element
var el = P.eventElement(e);
// check the element has the required attributes
if (el && P.getAttribute(el, 'id') && P.getAttribute(el, 'class')) {
// get the event type
var t = e.type;
// get the classes
var func = P.classParam(P.classNames(el), "func", null);
// check the function exists
if (eval('typeof(' + func + ')') == 'function') {
// execute the function, passing the element and event
eval(func + '(el,e)');
}
}
},
// paginate children of an element
Page: function(el) {
var elid = P.identify(el);
el = P.$(elid);
var cls = P.classNames(el);
var selector = P.classParam(cls, "selector", "");
var children;
if (selector.length > 0) {
children = P.children(el, "#" + elid + "-" + selector);
} else {
children = P.children(el);
}
var pagesize = P.classParam(cls, "pagesize", 10);
var startpage = P.classParam(cls, "startpage", 1);
if (children.length > pagesize) {
var page = 1;
var i = 0;
var acn = P.addClassName;
var h = P.Hide;
P.forEach(children, function(el) {
if (i < page * pagesize && i >= (page - 1) * pagesize) {
// do nothing
} else {
page++;
}
acn(el, 'pageelement');
acn(el, 'page' + page);
h(el);
i++;
});
var s = P.Show;
P.forEach(P.$$("#" + elid + " .page" + startpage), function(el) {
s(el);
});
var menu = P.BuildPageMenu(elid, page, startpage);
P.insertAfter(el, menu);
var links = P.$$("." + elid + "pagerlink");
var b = P.bind;
P.forEach(links, function(el) {
b(el, "click", P.ShowPage);
b(el, "keypress", P.ShowPage);
});
}
},
// build pagination menu
BuildPageMenu: function(elid, page, startpage) {
var menu = '<ul class="performer-pagination">';
var currentpage = "";
for (var x = 1; x <= page; x++) {
if (x == startpage) { currentpage = " currentpage"; }
menu += '<li><a href="#' + elid + '-page' + x + '" class="' + elid + 'pagerlink' + currentpage + '" id="' + elid + '-page' + x + '">' + x + '</a></li>';
currentpage = "";
}
menu += '</ul>';
return menu;
},
// show a pager page
ShowPage: function(e) {
var el = P.eventElement(e);
var elid = P.identify(el);
var parts = elid.split("-page");
var fe = P.forEach;
// hide all page elements
var h = P.Hide;
fe(P.$$("#" + parts[0] + " .pageelement"), function(el) {
h(el);
});
// show this page
var s = P.Show;
fe(P.$$("#" + parts[0] + " .page" + parts[1]), function(el) {
s(el);
});
// take off current page class
var rcn = P.removeClassName;
fe(P.$$("." + parts[0] + "pagerlink"), function(el) {
rcn(el, "currentpage");
});
// add current page class
P.addClassName(P.$(elid), "currentpage");
P.stopEvent(e);
},
// shows a context menu when the mouse is right-clicked
ContextMenu: function(e) {
var el = P.eventElement(e);
// hide any other context menus
var h = P.Hide;
var b = P.bind;
P.forEach(P.$$(".performercontextmenu"), function(el) { h(el); });
// check this is a right click
if (el && ((e.which && e.which == 3) || (e.button && e.button == 3) || (e.rightClick))) {
var targetEl = P.classParam(P.classNames(el), "targetEl", false);
var position = P.cursorPosition(e);
if (position && targetEl && P.$(targetEl)) {
var id = P.identify(el);
targetEl = P.$(targetEl);
P.addClassName(targetEl, 'performercontextmenu');
targetEl.style.position = 'absolute';
targetEl.style.zIndex = '10000';
targetEl.style.top = position[1] + 'px';
targetEl.style.left = position[0] + 'px';
P.Show(targetEl);
e.preventDefault();
// add the listener to remove the context menu
b(P.$$('body')[0], 'click', P.HideContextMenu);
b(P.$$('body')[0], 'keypress', P.HideContextMenu);
P.stopEvent(e);
return false;
}
}
return false;
},
// disables context menu on an element
DisableContextMenu: function(el) {
// disable the default context menu
P.disableContext(el);
},
// hides a context menu
HideContextMenu: function(e) {
var els = P.$$('.performercontextmenu');
var rcn = P.removeClassName;
var h = P.Hide;
P.forEach(els, function(el) {
rcn(el, 'performercontextmenu');
h(el);
});
},
// sets the value of a form field
Set: function(e) {
var el = P.eventElement(e);
if (el && P.nodeName(el)) {
var cls = P.classNames(el);
var value = unescape(P.classParam(cls, "value", ""));
var targetEl = P.classParam(cls, "targetEl", P.getAttribute(el, "rel"));
P.setValue(P.$(targetEl), value);
P.stopEvent(e);
}
},
// initialises a loop by hiding all elements in a UL, OL or DL list except the first one or the first one with class 'looperdefault'
InitLoop: function(elid) {
P.Debug('Performer.InitLoop', 'function');
var el = P.$(elid);
var shown = 0;
var i = 0;
var n = P.nodeName;
var h = P.Hide;
var hcn = P.hasClassName;
var id = P.identify;
var hsh = P.Hash;
P.forEach(P.children(el), function(child) {
if (n(child)) {
h(child);
if (hcn(child, 'looperdefault') || hsh == id(child)) { shown = i; }
i++;
}
});
P.Show(P.children(el)[shown]);
},
// moves a looper element
Loop: function(e) {
r = false;
var el = P.findEventElement(e, 'A');
// check the element has the required attribute and is a valid event trigge;
if (e.type == 'click' || P.keyCode(e) == 13) {
var cls = P.classNames(el);
var loop = P.classParam(cls, "targetEl", P.getAttribute(el, 'rel'));
// check the loop can be found
if (P.$(loop)) {
var i = 0;
var toshow = 0;
var nowshowing = 0;
var n = P.nodeName;
var v = P.visible;
var dbg = P.Debug;
var h = P.Hide;
var children = P.children(P.$(loop));
var len = children.length;
P.forEach(children), function(child) {
if (n(child) && v(child)) {
dbg('- Currently showing item ' + i, 'subfunction');
nowshowing = i;
}
h(child);
i++;
});
if (P.hasClassName(el, 'looperback')) {
P.Debug('Performer.Loop (back)', 'function');
toshow = nowshowing - 1;
if (toshow < 0) { toshow = (len - 1); }
} else if (P.hasClassName(el, 'looperforward')) {
P.Debug('Performer.Loop (forward)', 'function');
toshow = nowshowing + 1;
if (toshow >= len) { toshow = 0; }
} else if (P.hasClassName(el, 'looperfirst')) {
P.Debug('Performer.Loop (first)', 'function');
toshow = 0;
} else if (P.hasClassName(el, 'looperlast')) {
P.Debug('Performer.Loop (last)', 'function');
toshow = len - 1;
} else if (P.hasClassName(el, 'looperitem')) {
toshow = P.classParam(cls, "item", 1);
if (toshow >= len) toshow = len - 1;
P.Debug('Performer.Loop ('+toshow+')', 'function');
}
P.Debug('- Showing item ' + toshow, 'subfunction');
P.Show(P.children(P.$(loop))[toshow], "fadein");
P.stopEvent(e);
}
}
},
// show a tooltip when an element has mouseover or focus
Tooltip: function(e, cssClass) {
var el = P.eventElement(e);
if (!cssClass) { cssClass = "performertooltip"; }
var id = P.identify(el);
if (el && P.nodeName(el) && !P.$(id + '_performertooltip')) {
var cls = P.classNames(el);
var text = false;
var originaltext = "";
if (P.getAttribute(el, "title") && P.getAttribute(el, "title") != "") {
originaltext = P.getAttribute(el, "title");
text = '<p>' + originaltext.replace(/\r/g, "<br />\n") + '</p>';
} else {
var targetEl = P.$(P.classParam(cls, "targetEl", false));
if (targetEl) { text = targetEl.innerHTML; }
}
var position = false;
// if this is a mouseover event, get the mouse position
if (e.type == 'mouseover') {
position = P.cursorPosition(e);
} else {
position = P.elementPosition(el);
position[1] = position[1] + el.offsetHeight;
}
if (position && text) {
var className = P.classParam(cls, "className", cssClass);
var width = P.classParam(cls, "width", 300);
var leftoffset = position[0] - (width / 2);
var topoffset = position[1] + 16;
if (leftoffset < 0) {
leftoffset = 25;
}
if (document.body.scrollWidth && ((leftoffset + width) > document.body.scrollWidth)) {
leftoffset = (document.body.scrollWidth - width - 25);
}
P.setAttribute(el, "title", "");
P.setAttribute(el, "temptitle", originaltext);
if (P.getAttribute(el, "alt")) {
P.setAttribute(el, "tempalt", originaltext);
P.setAttribute(el, "alt", "");
}
var parent = P.up(el, "");
if (parent.getAttribute("title")) {
P.setAttribute(parent, "temptitle", originaltext);
P.setAttribute(parent, "title", "");
}
var tooltip = document.createElement('div');
var tooltipinner = document.createElement('div');
tooltipinner.className = cssClass + 'inner';
tooltipinner.innerHTML = text;
tooltip.id = id + '_performertooltip';
tooltip.className = className;
tooltip.style.position = 'absolute';
tooltip.style.zIndex = '10000';
tooltip.style.width = width + 'px';
tooltip.style.top = topoffset + 'px';
tooltip.style.left = leftoffset + 'px';
tooltip.appendChild(tooltipinner);
P.bind(el, 'mouseout', P.HideTooltip);
P.bind(el, 'blur', P.HideTooltip);
document.getElementsByTagName('body')[0].appendChild(tooltip);
}
}
},
// hide a tooltip
HideTooltip: function(e) {
var el = P.eventElement(e);
var id = P.identify(el);
if (el && P.$(id + '_performertooltip')) {
P.remove(P.$(id + '_performertooltip'));
var originaltext = P.getAttribute(el, "temptitle");
P.setAttribute(el, "title", originaltext);
P.removeAttribute(el, "temptitle");
if (P.getAttribute(el, "tempalt")) {
P.setAttribute(el, "alt", originaltext);
P.removeAttribute(el, "tempalt");
}
var parent = P.up(el, "");
if (parent.getAttribute("temptitle")) {
P.setAttribute(parent, "title", originaltext);
P.removeAttribute(parent, "temptitle");
}
}
},
// show a modal window
ModalWindow: function(e) {
var eve = P.eventOrElement(e);
var el = eve.el;
if (el) {
var id = P.identify(el);
var cls = P.classNames(el);
var delay = P.classParam(cls, 'delay', 0)*1000;
var outerClassName = P.classParam(cls, 'outerClassName', 'performermodalouter');
var innerClassName = P.classParam(cls, 'innerClassName', 'performermodalinner');
var lightBox = P.classParam(cls, 'lightBox', true);
var closer = P.classParam(cls, 'closer', true);
var targetPage = P.classParam(cls, 'targetPage', false);
var horizontalPadding = P.classParam(cls, 'horizontalPadding', 100);
var verticalPadding = P.classParam(cls, 'verticalPadding', 100);
var screenDim = P.classParam(cls, 'screenDim', 6);
if (delay === 0) {
P.DoModalWindow(targetPage, lightBox, screenDim, outerClassName, innerClassName, horizontalPadding, verticalPadding, closer);
} else {
setTimeout(function(){
P.DoModalWindow(targetPage, lightBox, screenDim, outerClassName, innerClassName, horizontalPadding, verticalPadding, closer);
}, delay);
}
if(eve.stop) { P.stopEvent(e); }
}
},
DoModalWindow: function(targetPage, lightBox, screenDim, outerClassName, innerClassName, horizontalPadding, verticalPadding, closer) {
if (P.createModal(targetPage, lightBox, screenDim, outerClassName, innerClassName, horizontalPadding, verticalPadding, closer)) {
P.addClassName(P.$('performer_modal'), 'performerloading');
P.DoLoad(targetPage, 'performer_modal', 'get', 'fillandinit');
}
},
// create a modal window
createModal: function(targetPage, lightBox, screenDim, outerClassName, innerClassName, horizontalPadding, verticalPadding, closer) {
if (targetPage && !P.$('performer_modal')) {
// if showing a lightbox, dim the page
var lb = false;
if (lightBox) {
lb = document.createElement('div');
lb.className = outerClassName;
lb.style.zIndex = 100000;
lb.style.position = 'fixed';
lb.style.left = '0px';
lb.style.right = '0px';
lb.style.bottom = '0px';
lb.style.top = '0px';
lb.style.opacity = '0.' + screenDim;
lb.style.filter = 'alpha(opacity=' + screenDim + '0)';
lb.id = 'performer_modal_outer';
}
var inner = document.createElement('div');
inner.className = innerClassName;
inner.id = 'performer_modal';
inner.style.overflow = 'auto';
inner.style.zIndex = 100001;
inner.style.position = 'fixed';
inner.style.left = horizontalPadding + 'px';
inner.style.right = horizontalPadding + 'px';
inner.style.bottom = verticalPadding + 'px';
inner.style.top = verticalPadding + 'px';
// if showing a lightbox, append the inner to the lightbox
if (lightBox) {
document.getElementsByTagName('body')[0].appendChild(lb);
}
document.getElementsByTagName('body')[0].appendChild(inner);
// create the closer
if (closer) {
var closelink = document.createElement('div');
closelink.id = 'performer_modal_closer';
closelink.style.zIndex = 100001;
closelink.style.position = 'fixed';
closelink.style.right = horizontalPadding + 'px';
closelink.style.top = (verticalPadding - 20) + 'px';
closelink.innerHTML = '<a href="#" class="modalwindowcloser">Close</a>';
document.getElementsByTagName('body')[0].appendChild(closelink);
P.bind(P.$$('a.modalwindowcloser')[0], 'keypress', P.CloseModal);
P.bind(P.$$('a.modalwindowcloser')[0], 'click', P.CloseModal);
}
// add the listener for the escape key to close the modal window
P.bind(document, 'keyup', P.CloseModal);
}
return true;
},
// close a modal window
CloseModal: function(e) {
var el = P.eventElement(e);
if ((e.type == 'keyup' && P.keyCode(e) == 27) || (el && P.hasClassName(el, 'modalwindowcloser'))) {
if (P.$('performer_modal_outer')) { document.getElementsByTagName('body')[0].removeChild(P.$('performer_modal_outer')); }
if (P.$('performer_modal_closer')) { document.getElementsByTagName('body')[0].removeChild(P.$('performer_modal_closer')); }
if (P.$('performer_modal')) { document.getElementsByTagName('body')[0].removeChild(P.$('performer_modal')); }
P.stopEvent(e);
}
},
// hide the remainder of an element contents after a certain number of characters
// inspired by http://www.reindel.com/truncate/
Truncate: function(el) {
// get the class parameters
var cls = P.classNames(el);
var limit = P.classParam(cls, 'limit', 50);
var openText = P.classParam(cls, 'openText', '...more');
var closeText = P.classParam(cls, 'closeText', '...less');
if (el && limit) {
var c = el.innerHTML;
var l = c.length;
if (limit < l) {
var id = P.identify(el);
el.innerHTML = '<span id="' + id + '_truncated">' + c.substring(0, limit) + ' <a href="#" class="switcher targetEl1-' + id + '_truncated targetEl2-' + id + '_full">' + openText + '</a></span><span class="hider" id="' + id + '_full">' + c + ' <a href="#" class="switcher targetEl1-' + id + '_truncated targetEl2-' + id + '_full">' + closeText + '</a></span>';
P.ReInit(id);
}
}
},
// duplicates an element
Duplicate: function(e) {
var el = P.eventElement(e);
// get the class parameters
var cls = P.classNames(el);
var sourceEl = P.classParam(cls, "sourceElement", false);
var sourceElement = P.$(sourceEl);
var targetEl = P.classParam(cls, "targetElement", false);
var targetElement = P.$(targetEl);
var start = (parseInt(Performer.classParam(cls, "start", 1))) - 1;
// if the parameters are set
if (el && P.nodeName(el) && sourceElement && P.nodeName(sourceElement) && targetElement && P.nodeName(targetElement)) {
var newel = targetElement.appendChild(sourceElement.cloneNode(true));
if (typeof (P.Duplicators[targetEl]) === 'undefined') {
P.Duplicators[targetEl] = (start + 1);
}
var items = P.Duplicators[targetEl] + 1;
P.Duplicators[targetEl] = items;
newel.innerHTML = newel.innerHTML.replace(/_1/g, '_' + items);
newel.innerHTML = newel.innerHTML.replace(/[1]/g, items);
newel.id = newel.id.replace(/_1/g, '_' + items);
if (P.getAttribute(newel, 'class') != '') {
P.setAttribute(newel, 'class', P.getAttribute(newel, 'class').replace(/_1/g, '_' + items));
}
var countEl = P.classParam(cls, "countElement", false);
var countElement = P.$(countEl);
if (countElement) { P.setValue(countElement, items); }
P.stopEvent(e);
}
},
// disables all submit buttons when a form is submitted
SubmitLock: function(e) {
var el = P.findEventElement(e, 'FORM');
if (el) {
// get unique fields in the form
var fields = P.$F(el);
var acn = P.addClassName;
P.forEach(fields, function(input) {
var field = P.$(P.identify(input));
if (field.type.toLowerCase() == 'submit') {
field.disabled = true;
acn(field, 'performer-disabled');
}
});
}
},
// submits a form to a page and fills an element with the results
Submit: function(e) {
var el = P.findEventElement(e, 'FORM');
// get the class parameters
var cls = P.classNames(el);
var targetPage = P.classParam(cls, "targetPage", false);
var targetEl = P.classParam(cls, "targetEl", P.classParam(cls, "targetElement", false));
var targetElement = P.$(targetEl);
// if the parameters are set
if (targetPage && el && P.nodeName(el) && targetElement && P.nodeName(targetElement)) {
var params = P.serialize(el);
P.addClassName(targetElement, 'performerloading');
P.Request(targetPage, 'POST', params, function(request) {
var text = P.getRequestText(request);
P.update(targetElement, text);
P.removeClassName(targetElement, 'performerloading');
P.Debug('-> Filled \'#' + targetEl + '\'', 'success');
});
P.stopEvent(e);
}
},
// checks the values of fields in a form
CheckForm: function(e) {
P.Debug('Performer.CheckForm', 'function');
var el = P.findEventElement(e, 'FORM');
var fail = false;
var radiogroups = [];
// get unique fields in the form
var fields = P.$F(el);
var id = P.identify;
var d = P.$;
var hcn = P.hasClassName;
var fn = P.FieldNotify;
var fnr = P.FieldNotifyRemove;
var gv = P.getValue;
P.forEach(fields, function(input) {
var identity = id(input);
var field = d(identity);
var val = gv(field);
// field is required
if (hcn(field, 'field-required')) {
if (field.type != 'radio' &&
((field.type == 'checkbox' && field.checked === false) || field.value == "")) {
fn(identity, 'error', 'This field is required');
fail = true;
} else { fnr(identity); }
// radio buttons
if (field.type == 'radio' && radiogroups.PerformerIndexOf(field.name) == -1) {
radiogroups[radiogroups.length] = field.name;
var radiofail = true;
// get radio buttons for this field
var radiobuttons = el[field.name];
for (var i = 0; i < radiobuttons.length; i++) {
if (radiobuttons[i].checked) {
radiofail = false;
break;
}
}
var lastbutton = radiobuttons[radiobuttons.length - 1];
if (radiofail) {
fn(lastbutton, 'error', 'This field is required');
fail = true;
} else { fnr(lastbutton); }
}
}
// field must be an email address
if (hcn(field, 'field-required-email') || hcn(field, 'field-optional-email')) {
var validemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if ((hcn(field, 'field-required-email') && val == "") || (val != "" && !validemail.test(val))) {
fn(identity, 'error', 'This field must be a valid email address');
fail = true;
} else { fnr(identity); }
}
// field must be a number
if (hcn(field, 'field-required-number') || hcn(field, 'field-optional-number')) {
var num = val.replace(",", "").replace("£", "").replace("£", "");
if ((hcn(field, 'field-required-number') && num == "") || (num != "" && isNaN(parseFloat(num)))) {
fn(identity, 'error', 'This field must be a number');
fail = true;
} else {
// set the correct number value
if (num != "") { field.value = parseFloat(num); }
fnr(identity);
}
}
// field must be a date (dd/mm/yyyy)
if (hcn(field, 'field-required-date') || hcn(field, 'field-optional-date')) {
var validdate = /^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})$/;
if ((hcn(field, 'field-required-date') && val == "") || (val != "" && !validdate.test(val))) {
fn(identity, 'error', 'This field must be a date (dd/mm/yyyy)');
fail = true;
} else { fnr(identity); }
}
P.Debug('-> Field ' + input.name, 'subfunction');
});
if (fail) {
fn(P.identify(el), 'error', 'There are errors with some fields. Please check the form and try again.');
P.stopEvent(e);
}
},
// adds a notification to a field
FieldNotify: function(field, messageclass, message) {
if (!P.$(field + '-notification')) {
P.insertAfter(P.$(field), '<span id="' + field + '-notification" class="performer-' + messageclass + '">' + message + '</span>');
} else {
P.$(field + '-notification').innerHTML = message;
P.$(field + '-notification').className = 'performer-' + messageclass;
}
},
// remove a field notification
FieldNotifyRemove: function(field) {
if (P.$(field + '-notification')) {
P.$(field + '-notification').innerHTML = "";
P.$(field + '-notification').className = "";
}
},
// send a list selection to a remote page
Select: function(e) {
P.Debug('Performer.Select', 'function');
var el = P.eventElement(e);
// check the element has the required attributes
if (el && P.nodeName(el) && P.getAttribute(el, 'id') && P.getAttribute(el, 'name') && P.getAttribute(el, 'class')) {
// get the class parameters
var cls = P.classNames(el);
var targetPage = P.classParam(cls, "targetPage", false);
var targetEl = P.classParam(cls, "targetEl", false);
var targetValue = P.classParam(cls, "targetValue", false);
// check we have a targetPage and targetEl and the target element can be found
if (targetPage && P.$(targetEl)) {
if (targetValue == 'true') {
P.DoLoad(targetPage + '?selection=' + P.getValue(el), targetEl, 'post', 'setvalueandinit');
} else {
P.DoLoad(targetPage + '?selection=' + P.getValue(el), targetEl, 'post', 'fillandinit');
}
}
}
},
// check the strength of a password
CheckPassword: function(e) {
P.Debug('Performer.CheckPassword', 'function');
var el = P.eventElement(e);
// check the element has the required attributes
if (el && P.nodeName(el) && P.getAttribute(el, 'id') && P.getAttribute(el, 'name') && P.getAttribute(el, 'class')) {
// get the class parameters
var cls = P.classNames(el);
var notifyEl = P.$(P.classParam(cls, "notifyEl", false));
// check we have a notification element
if (notifyEl) {
P.Show(notifyEl);
var val = el.value;
// if the password is shorter than 6 characters
if (val.length < 6) {
P.update(notifyEl, 'Your password must be at least 6 characters long');
P.className(notifyEl, 'password-weak');
// if the password has only letters
} else {
P.update(notifyEl, '');
// if the password is just letters or just numbers less than 10 characters
if (val.match(/^([a-zA-Z]{6,10})$/) || val.match(/^([0-9]{6,10})$/)) {
P.update(notifyEl, 'Weak password');
P.className(notifyEl, 'password-weak');
// if the password is just letters or just numbers more than 10 characters
} else if (val.match(/^([a-zA-Z]{10,})$/) || val.match(/^([0-9]{10,})$/)) {
P.update(notifyEl, 'Acceptable password');
P.className(notifyEl, 'password-ok');
// if the password contains letters, numbers and characters it is strong
} else if (val.match(/^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[^0-9a-zA-Z]).*$/)) {
P.update(notifyEl, 'Strong password');
P.className(notifyEl, 'password-strong');
// if the password has just letters and numbers, or just letters and characters, or just numbers and characters
} else if (val.match(/^.*(?=.{6,})(?=.*\d)(?=.*[a-z]).*$/) || val.match(/^.*(?=.{6,})(?=.*[^0-9a-zA-Z])(?=.*[a-z]).*$/) || val.match(/^.*(?=.{6,})(?=.*[^0-9a-zA-Z])(?=.*\d).*$/)) {
P.update(notifyEl, 'Acceptable password');
P.className(notifyEl, 'password-ok');
}
}
}
}
},
// match the values of two input boxes
Match: function(e) {
P.Debug('Performer.Match', 'function');
var el = P.eventElement(e);
// check the element has the required attributes
if (el && P.nodeName(el) && P.getAttribute(el, 'id') && P.getAttribute(el, 'name') && P.getAttribute(el, 'class')) {
// get the class parameters
var cls = P.classNames(el);
var notifyEl = P.$(P.classParam(cls, "notifyEl", false));
var matchEl = P.$(P.classParam(cls, "matchEl", false));
// check we have a match element and a notification element
if (notifyEl && matchEl) {
// show the notifyEl
P.Show(notifyEl);
// get the value
var val1 = el.value;
// get the value to be matched
var val2 = matchEl.value;
// if the values match
if (val1 == val2) {
// hide the notifyEl
P.Hide(notifyEl);
}
}
}
},
// toggle element
Toggle: function(e, c) {
var r = false;
var eve = P.eventOrElement(e);
var el = eve.el;
if (!el) { el = P.findEventElement(e, 'A'); }
// check the element has the required attribute and is a valid event trigger
if (el && P.nodeName(el) && ((e.type == 'click' || P.keyCode(e) == 13 || P.keyCode(e) == 32) || !eve.stop)) {
var cls = P.classNames(el);
var showeffect = P.classParam(cls, 'showeffect', 'slidedown');
var hideeffect = P.classParam(cls, 'hideeffect', 'slideup');
var toggleid = P.classParam(cls, 'targetEl', P.getAttribute(el, 'rel'));
if (!toggleid){ toggleid = P.identify(el); }
var move = P.classParam(cls, 'move', false);
var delay = P.classParam(cls, 'delay', 0)*1000;
var targetEl = P.$(toggleid);
// check the target element can be found
if (targetEl && P.nodeName(targetEl)) {
if (delay === 0) {
r = P.DoToggle(toggleid, targetEl, el, showeffect, hideeffect);
} else {
setTimeout(function() {
r = P.DoToggle(toggleid, targetEl, el, showeffect, hideeffect);
}, delay);
}
if (!move || !r) {
if (P.nodeName(el).toLowerCase() == 'a' && eve.stop) { P.stopEvent(e); }
} else {
window.location.hash = P(el, "href").replace(new RegExp('^[#]+', 'g'), '');
}
return r;
}
}
},
DoToggle: function(toggleid, targetEl, el, showeffect, hideeffect) {
// toggle the visibility
if (!P.visible(targetEl)) {
if (toggleid != 'performerjsdebugbox') { P.Debug('Performer.Toggle -> Show \'#' + toggleid + '\'', 'subfunction'); }
P.addClassName(el, 'toggleropen');
var n = P.nodeName;
var s = P.Show;
var v = P.visible;
s(targetEl, showeffect);
P.forEach(P.ancestors(P.$(targetEl)), function(ancestor) {
if (ancestor.tagName != 'body' && ancestor.tagName != 'html' && n(ancestor) && !v(ancestor)) {
s(ancestor);
}
});
r = true;
} else {
if (toggleid != 'performerjsdebugbox') { P.Debug('Performer.Toggle -> Hide \'#' + toggleid + '\'', 'subfunction'); }
P.removeClassName(el, 'toggleropen');
P.Hide(targetEl, hideeffect);
r = false;
}
if (P.nodeName(el).toLowerCase() == 'input' && el.type && el.type == 'checkbox') { el.checked = r; }
return r;
},
// toggles the visibility of a group of elements
GroupToggle: function(e, c) {
var r = false;
var eve = P.eventOrElement(e);
var el = eve.el;
if (!el) { el = P.findEventElement(e, 'A'); }
// check the element has the required attribute and is a valid event trigger
if (el && P.nodeName(el) && ((e.type == 'click' || P.keyCode(e) == 13 || P.keyCode(e) == 32) || !eve.stop)) {
var cls = P.classNames(el);
var targetClass = P.classParam(cls, 'targetClass', P.getAttribute(el, 'rel'));
var showeffect = P.classParam(cls, 'showeffect', 'slidedown');
var hideeffect = P.classParam(cls, 'hideeffect', 'slideup');
var move = P.classParam(cls, 'move', false);
var delay = P.classParam(cls, 'delay', 0)*1000;
var foundEls = P.$$('.' + targetClass);
P.Debug('Performer.GroupToggle -> Toggle ' + foundEls.length + ' elements with class ' + targetClass, 'subfunction');
if (delay === 0) {
r = P.DoGroupToggle(targetClass, foundEls, showeffect, hideeffect);
} else {
setTimeout(function() {
r = P.DoGroupToggle(targetClass, foundEls, showeffect, hideeffect);
}, delay);
}
if (P.nodeName(el).toLowerCase() == 'input' && el.type && el.type == 'checkbox') { el.checked = r; }
if (!move || !r) {
if (P.nodeName(el).toLowerCase() == 'a' && eve.stop) { P.stopEvent(e); }
} else {
window.location.hash = P(el, "href").replace(new RegExp('^[#]+', 'g'), '');
}
return r;
}
},
DoGroupToggle: function(targetClass, foundEls, showeffect, hideeffect) {
var acn = P.addClassName;
var rcn = P.removeClassName;
var s = P.Show;
var h = P.Hide;
var r = false;
P.forEach(foundEls, function(element) {
// toggle the visibility
if (!P.visible(element)) {
acn(element, 'toggleropen');
s(element, showeffect);
r = true;
} else {
rcn(element, 'toggleropen');
h(element, hideeffect);
r = false;
}
});
return r;
},
// switch visibility of two element
Switch: function(e, c) {
P.Debug('Performer.Switch', 'function');
var r = false;
var el = P.eventElement(e);
if (!el) { el = P.findEventElement(e, 'A'); }
// check the element has the required attribute and is a valid event trigger
if (el && P.nodeName(el) && (e.type == 'click' || e.keyCode == P.keyCode(e) == 13)) {
var cls = P.classNames(el);
var t1 = P.classParam(cls, 'targetEl1', P.getAttribute(el, 'rel'));
var targetEl1 = P.$(t1);
var t2 = P.classParam(cls, 'targetEl2', P.getAttribute(el, 'rev'));
var targetEl2 = P.$(t2);