-
Notifications
You must be signed in to change notification settings - Fork 12
/
Paint.min.js
7891 lines (6604 loc) · 247 KB
/
Paint.min.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
function Controls (container, controls) {
this.controls = controls || [];
this.byName = {};
this.container = container;
this.buildControls();
}
// Removes everything from the container and
// Creates the given controls, includes adding them to .byName
Controls.prototype.buildControls = function buildControls () {
// Empty the container
while (this.container.firstChild) {
this.container.removeChild(container.firstChild);
}
// Create each controler from its definition
// Add to the container and .byName
for (var cKey = 0; cKey < this.controls.length; cKey++) {
var control = this.createControl(this.controls[cKey])
this.container.appendChild(control.containerAppend);
this.byName[this.controls[cKey].name] = control;
if (typeof control.executeAfterAppend == "function") {
control.executeAfterAppend();
}
}
};
// CreateControl calls the constructor for the given control
Controls.prototype.createControl = function createControl (control) {
if (typeof this.constructors[control.type] == "function") {
return this.constructors[control.type](control);
}
// Control is not defined
console.error("Unknown control: " + control.type, control);
return {
containerAppend: document.createTextNode("Unknown control: " + control.type)
};
};
// Object that holds all the contruction functions for the controllers
// A construction object should return whatever should be stored in .byName["nameOfTheControl"]
// The returned object should at least have:
//{
// input: domElement, //DomElement of which .value can be get and set,
// containerAppend: domElement, // Element that should be appended to the container
//}
Controls.prototype.constructors = {};
Controls.prototype.constructors.button = function createButton (control) {
var input = document.createElement("div");
input.className = (control.classAppend || "") + "control-button";
if (control.value)
input.value = control.value;
if (control.text)
input.appendChild(document.createTextNode(control.text));
if (control.image) {
var img = input.appendChild(document.createElement("img"));
img.src = control.image;
img.alt = control.alt;
}
if (control.title)
input.title = control.title;
if (control.data)
for (var datakey in control.data)
input.setAttribute("data-" + datakey, control.data[datakey]);
input.addEventListener("click", function (event) {
control.action(input.value);
event.preventDefault();
});
return {
input: input,
containerAppend: input
}
};
Controls.prototype.constructors.integer = function createIntegerInput (control) {
var container = document.createElement("div");
container.className = (control.classAppend || "") + "control-integer";
// Create the actual input field
var input = document.createElement("input");
input.type = control.range ? "range" : "number";
input.value = control.value;
input.className = (control.classAppend || "") + "control-integer-input";
if (control.text)
input.placeholder = control.text;
if (control.title)
input.title = control.title;
if (control.max)
input.max = control.max;
if (control.min)
input.min = control.min;
if (control.data)
for (var datakey in control.data)
input.setAttribute("data-" + datakey, control.data[datakey]);
var integerOutput = container.appendChild(document.createElement("div"));
integerOutput.className = "control-integer-output";
integerOutput.textContent = '0';
input.addEventListener("input", function () {
//integerOutput.textContent = input.value;
control.action(input.value, true);
});
// Create the minus button
var minusButton = container.appendChild(this.button({
text: "-",
action: function () {
var max = parseInt(input.max || Infinity);
var nextValue = parseInt(input.value) - 1;
input.value = Math.min(max, nextValue);
control.action(input.value, true);
}
}).containerAppend);
// Append the input
container.appendChild(input);
// Create a plus button
container.appendChild(this.button({
text: "+",
action: function () {
var max = parseInt(input.max || Infinity);
var nextValue = parseInt(input.value) + 1;
input.value = Math.min(max, nextValue);
control.action(input.value, true);
}
}).containerAppend);
return {
input: input,
integerOutput: integerOutput,
containerAppend: container
}
};
Controls.prototype.constructors.text = function createTextInput (control) {
// Create the actual input field
var input = document.createElement("input");
input.type = "text";
input.value = control.value;
input.className = (control.classAppend || "") + "control-text-input";
if (control.text)
input.placeholder = control.text;
if (control.title)
input.title = control.title;
if (control.max)
input.max = control.max;
if (control.min)
input.min = control.min;
if (control.data)
for (var datakey in control.data)
input.setAttribute("data-" + datakey, control.data[datakey]);
input.addEventListener("input", function () {
control.action(input.value);
});
return {
input: input,
containerAppend: input
}
};
Controls.prototype.constructors.color = function createColorInput (control) {
// Create the actual input field
var input = document.createElement("input");
input.type = "text";
input.value = control.value;
input.className = (control.classAppend || "") + "control-color-input";
if (control.data)
for (var datakey in control.data)
input.setAttribute("data-" + datakey, control.data[datakey]);
var returnData = {
input: input,
containerAppend: input,
executeAfterAppend: function () {
var spectrum = $(input).spectrum({
showAlpha: true,
showInput: true,
showInitial: true,
preferredFormat: "rgb",
showPalette: true,
maxSelectionSize: 32,
clickoutFiresChange: true,
localStorageKey: "anondraw.palette",
move: function (color) {
control.action(color);
}
});
}
};
return returnData;
};
Controls.prototype.constructors.gradient = function createGradientInput (control) {
var gradientDom = document.createElement("div");
gradientDom.className = (control.classAppend || "") + "control-gradient";
var gradientCreator = new GradientCreator(gradientDom);
gradientCreator.addEventListener("change", control.action)
return {
input: gradientDom,
containerAppend: gradientDom,
gradientCreator: gradientCreator
};
};/*
LICENSE: MIT
Author: Filip Smets
*/
/*
Creates the creator
Everything inside container could be removed
All properties of container could be overwritten, including style
*/
function GradientCreator (container) {
this._container = container;
this.setupDom();
this.rerender();
}
GradientCreator.prototype.INITIAL_STOPS = [{
pos: 0,
color: "rgba(255, 0, 0, 1)"
}, {
pos: 0.15,
color: "rgba(255, 255, 0, 1)"
}, {
pos: 0.3,
color: "rgba(0, 255, 0, 1)"
}, {
pos: 0.5,
color: "rgba(0, 255, 255, 1)"
}, {
pos: 0.65,
color: "rgba(0, 0, 255, 1)"
}, {
pos: 0.80,
color: "rgba(255, 0, 255, 1)"
}, {
pos: 1,
color: "rgba(255, 0, 0, 1)"
}];
/*
#################
# DOM FUNCTIONS #
#################
*/
/*
Removes everything inside the container, then creates all elements
Container will be taken from this._container
*/
GradientCreator.prototype.setupDom = function setupDom () {
// Remove all elements in the container
while (this._container.firstChild)
this._container.removeChild(this._container.firstChild)
this.createPreviewDom();
this.createColorSelector();
};
/*
Create a div for the preview and add inital colors
*/
GradientCreator.prototype.createPreviewDom = function createPreviewDom () {
var preview = this._container.appendChild(document.createElement("div"));
preview.classList.add("gradient-preview");
this._previewDom = preview;
document.addEventListener("mousemove", function (event) {
if (!this._draggingStop) return;
var relativeWidth = this.getRelativeWidth(event, this._previewDom);
// Clamp between 0 and 1
relativeWidth = Math.min(1, Math.max(0, relativeWidth));
this._draggingStop.style.left = relativeWidth * 100 + "%";
this._hasDragged = true;
this.hideColorPicker();
this.rerender();
}.bind(this));
document.addEventListener("touchmove", function (event) {
if (!this._draggingStop) return;
var relativeWidth = this.getRelativeWidth(event, this._previewDom);
// Clamp between 0 and 1
relativeWidth = Math.min(1, Math.max(0, relativeWidth));
this._draggingStop.style.left = relativeWidth * 100 + "%";
this._hasDragged = true;
this.hideColorPicker();
this.rerender();
}.bind(this));
document.addEventListener("mouseup", function (event) {
delete this._draggingStop;
}.bind(this));
document.addEventListener("touchend", function (event) {
delete this._draggingStop;
}.bind(this));
document.addEventListener("click", function (event) {
if (event.target.classList.contains("gradient-stop")) return;
this.hideColorPicker();
}.bind(this));
preview.addEventListener("dblclick", function (event) {
var relativeWidth = this.getRelativeWidth(event, this._previewDom);
this.createStop({
pos: relativeWidth,
color: tinycolor()
});
}.bind(this));
this.INITIAL_STOPS.forEach(this.createStop.bind(this));
};
/*
This function creates a stop dom element from {pos: 0-1, color: tinycolor/csscolor/...}
It also fires the change event and rerenders
*/
GradientCreator.prototype.createStop = function createStop (stop) {
var stopDom = this._previewDom.appendChild(document.createElement("div"))
stopDom.className = "gradient-stop";
stopDom.style.background = stop.color;
stopDom.style.left = stop.pos * 100 + "%";
stopDom.color = tinycolor(stop.color);
stopDom.addEventListener("mousedown", function (event) {
this._draggingStop = stopDom;
this._hasDragged = false;
}.bind(this));
stopDom.addEventListener("touchstart", function (event) {
this._draggingStop = stopDom;
this._hasDragged = false;
}.bind(this));
stopDom.addEventListener("click", function (event) {
if (this._hasDragged) return;
this.changeColorOf(stopDom);
}.bind(this))
stopDom.addEventListener("dblclick", function (event) {
stopDom.parentNode.removeChild(stopDom);
if (event.stopPropagation) event.stopPropagation();
event.cancelBubble = true;
this.rerender();
}.bind(this))
this.rerender();
};
GradientCreator.prototype.hideColorPicker = function hideColorPicker () {
delete this._changingColorOf;
// Search for the color picker
for (var k = 0; k < this._previewDom.children.length; k++) {
// Found
if (this._previewDom.children[k].classList.contains("sp-container")) {
this._previewDom.children[k].classList.add("hide");
}
}
};
GradientCreator.prototype.changeColorOf = function changeColorOf (stop) {
// Search for the color picker
for (var k = 0; k < this._previewDom.children.length; k++) {
// Found
if (this._previewDom.children[k].classList.contains("sp-container")) {
// Place it at the stop
this._previewDom.children[k].style.left = stop.style.left;
// If we are already changing the color of this one just remove the color picker
if (this._changingColorOf == stop) {
this._previewDom.children[k].classList.add("hide");
// Otherwise show it
} else {
this._previewDom.children[k].classList.remove("hide");
$(this.spectrumInput).spectrum("set", stop.color);
}
}
}
this._changingColorOf = stop;
};
/*
Creates the color selector
*/
GradientCreator.prototype.createColorSelector = function createColorSelector () {
var input = this._previewDom.appendChild(document.createElement("input"));
input.type = "color";
this.spectrumInput = input;
$(this.spectrumInput).spectrum({
showAlpha: true,
showInput: true,
showButtons: false,
flat: true,
showInitial: true,
preferredFormat: "rgb",
showPalette: true,
maxSelectionSize: 32,
clickoutFiresChange: true,
move: function (color) {
if (this._changingColorOf) {
this._changingColorOf.color = color;
this._changingColorOf.style.background = color;
this.rerender();
}
}.bind(this)
});
this.hideColorPicker();
};
/*
###################
# General methods #
###################
*/
/*
Get an array of the current stops
Example: [{pos: 0, color: tinycolor()}, {pos: 1, color: tinycolor()}]
*/
GradientCreator.prototype.getStops = function getStops () {
var stops = [];
for (var key = 0; key < this._previewDom.children.length; key++) {
if (!this._previewDom.children[key].classList.contains("gradient-stop")) continue;
stops.push({
pos: parseFloat(this._previewDom.children[key].style.left.slice(0, -1)) / 100,
color: this._previewDom.children[key].color
});
}
stops.sort(function (a, b) {
return a.pos - b.pos;
});
return stops;
};
/*
Given an event object it will return the relative width between 0 and 1.
Works with touch events.
*/
GradientCreator.prototype.getRelativeWidth = function getRelativeWidth (event, target) {
// If there is no clientX/Y (meaning no mouse event) and there are no changed touches
// meaning no touch event, then we can't get the coords relative to the target element
// for this event
if (typeof event.clientX !== "number" && (!event.changedTouches || !event.changedTouches[0] || typeof event.changedTouches[0].clientX !== "number"))
return 0;
// Return the coordinates relative to the target element
var clientX = (typeof event.clientX === 'number') ? event.clientX : event.changedTouches[0].clientX,
target = target || event.target || document.elementFromPoint(clientX, clientY);
var boundingClientRect = target.getBoundingClientRect();
var relativeX = clientX - boundingClientRect.left;
return relativeX / boundingClientRect.width;
};
/*
Replace the css of the preview div to match the stops
Should be called when the stops have changed
This function also fires the change event
*/
GradientCreator.prototype.rerender = function rerender () {
var parsedStops = this.getStops().map(function (stop, index, stops) {
return stop.color + " " + stop.pos * 100 + "%";
});
this._previewDom.style.background = "linear-gradient(90deg, " + parsedStops.join(",") + ")";
this.dispatchEvent({
type: "change",
stops: this.getStops()
});
};
/**
* Event dispatcher
* License mit
* https://github.com/mrdoob/eventdispatcher.js
* @author mrdoob / http://mrdoob.com/
*/
var EventDispatcher = function () {}
EventDispatcher.prototype = {
constructor: EventDispatcher,
apply: function ( object ) {
object.addEventListener = EventDispatcher.prototype.addEventListener;
object.hasEventListener = EventDispatcher.prototype.hasEventListener;
object.removeEventListener = EventDispatcher.prototype.removeEventListener;
object.dispatchEvent = EventDispatcher.prototype.dispatchEvent;
},
addEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) this._listeners = {};
var listeners = this._listeners;
if ( listeners[ type ] === undefined ) {
listeners[ type ] = [];
}
if ( listeners[ type ].indexOf( listener ) === - 1 ) {
listeners[ type ].push( listener );
}
},
hasEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) return false;
var listeners = this._listeners;
if ( listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1 ) {
return true;
}
return false;
},
removeEventListener: function ( type, listener ) {
if ( this._listeners === undefined ) return;
var listeners = this._listeners;
var listenerArray = listeners[ type ];
if ( listenerArray !== undefined ) {
var index = listenerArray.indexOf( listener );
if ( index !== - 1 ) {
listenerArray.splice( index, 1 );
}
}
},
dispatchEvent: function ( event ) {
if ( this._listeners === undefined ) return;
var listeners = this._listeners;
var listenerArray = listeners[ event.type ];
if ( listenerArray !== undefined ) {
event.target = this;
var array = [];
var length = listenerArray.length;
for ( var i = 0; i < length; i ++ ) {
array[ i ] = listenerArray[ i ];
}
for ( var i = 0; i < length; i ++ ) {
array[ i ].call( this, event );
}
}
}
};
EventDispatcher.prototype.apply(GradientCreator.prototype);/**
* Intro.js v2.0
* https://github.com/usablica/intro.js
* MIT licensed
*
* Copyright (C) 2013 usabli.ca - A weekend project by Afshin Mehrabani (@afshinmeh)
*/
(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
factory(exports);
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports'], factory);
} else {
// Browser globals
factory(root);
}
} (this, function (exports) {
//Default config/variables
var VERSION = '2.0';
/**
* IntroJs main class
*
* @class IntroJs
*/
function IntroJs(obj) {
this._targetElement = obj;
this._introItems = [];
this._options = {
/* Next button label in tooltip box */
nextLabel: 'Next →',
/* Previous button label in tooltip box */
prevLabel: '← Back',
/* Skip button label in tooltip box */
skipLabel: 'Skip',
/* Done button label in tooltip box */
doneLabel: 'Done',
/* Default tooltip box position */
tooltipPosition: 'bottom',
/* Next CSS class for tooltip boxes */
tooltipClass: '',
/* CSS class that is added to the helperLayer */
highlightClass: '',
/* Close introduction when pressing Escape button? */
exitOnEsc: true,
/* Close introduction when clicking on overlay layer? */
exitOnOverlayClick: true,
/* Show step numbers in introduction? */
showStepNumbers: true,
/* Let user use keyboard to navigate the tour? */
keyboardNavigation: true,
/* Show tour control buttons? */
showButtons: true,
/* Show tour bullets? */
showBullets: true,
/* Show tour progress? */
showProgress: false,
/* Scroll to highlighted element? */
scrollToElement: true,
/* Set the overlay opacity */
overlayOpacity: 0.8,
/* Precedence of positions, when auto is enabled */
positionPrecedence: ["bottom", "top", "right", "left"],
/* Disable an interaction with element? */
disableInteraction: false,
/* Default hint position */
hintPosition: 'top-middle',
/* Hint button label */
hintButtonLabel: 'Got it'
};
}
/**
* Initiate a new introduction/guide from an element in the page
*
* @api private
* @method _introForElement
* @param {Object} targetElm
* @returns {Boolean} Success or not?
*/
function _introForElement(targetElm) {
var introItems = [],
self = this;
if (this._options.steps) {
//use steps passed programmatically
for (var i = 0, stepsLength = this._options.steps.length; i < stepsLength; i++) {
var currentItem = _cloneObject(this._options.steps[i]);
//set the step
currentItem.step = introItems.length + 1;
//use querySelector function only when developer used CSS selector
if (typeof(currentItem.element) === 'string') {
//grab the element with given selector from the page
currentItem.element = document.querySelector(currentItem.element);
}
//intro without element
if (typeof(currentItem.element) === 'undefined' || currentItem.element == null) {
var floatingElementQuery = document.querySelector(".introjsFloatingElement");
if (floatingElementQuery == null) {
floatingElementQuery = document.createElement('div');
floatingElementQuery.className = 'introjsFloatingElement';
document.body.appendChild(floatingElementQuery);
}
currentItem.element = floatingElementQuery;
currentItem.position = 'floating';
}
if (currentItem.element != null) {
introItems.push(currentItem);
}
}
} else {
//use steps from data-* annotations
var allIntroSteps = targetElm.querySelectorAll('*[data-intro]');
//if there's no element to intro
if (allIntroSteps.length < 1) {
return false;
}
//first add intro items with data-step
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
var currentElement = allIntroSteps[i];
var step = parseInt(currentElement.getAttribute('data-step'), 10);
if (step > 0) {
introItems[step - 1] = {
element: currentElement,
intro: currentElement.getAttribute('data-intro'),
step: parseInt(currentElement.getAttribute('data-step'), 10),
tooltipClass: currentElement.getAttribute('data-tooltipClass'),
highlightClass: currentElement.getAttribute('data-highlightClass'),
position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
};
}
}
//next add intro items without data-step
//todo: we need a cleanup here, two loops are redundant
var nextStep = 0;
for (var i = 0, elmsLength = allIntroSteps.length; i < elmsLength; i++) {
var currentElement = allIntroSteps[i];
if (currentElement.getAttribute('data-step') == null) {
while (true) {
if (typeof introItems[nextStep] == 'undefined') {
break;
} else {
nextStep++;
}
}
introItems[nextStep] = {
element: currentElement,
intro: currentElement.getAttribute('data-intro'),
step: nextStep + 1,
tooltipClass: currentElement.getAttribute('data-tooltipClass'),
highlightClass: currentElement.getAttribute('data-highlightClass'),
position: currentElement.getAttribute('data-position') || this._options.tooltipPosition
};
}
}
}
//removing undefined/null elements
var tempIntroItems = [];
for (var z = 0; z < introItems.length; z++) {
introItems[z] && tempIntroItems.push(introItems[z]); // copy non-empty values to the end of the array
}
introItems = tempIntroItems;
//Ok, sort all items with given steps
introItems.sort(function (a, b) {
return a.step - b.step;
});
//set it to the introJs object
self._introItems = introItems;
//add overlay layer to the page
if(_addOverlayLayer.call(self, targetElm)) {
//then, start the show
_nextStep.call(self);
var skipButton = targetElm.querySelector('.introjs-skipbutton'),
nextStepButton = targetElm.querySelector('.introjs-nextbutton');
self._onKeyDown = function(e) {
if (e.keyCode === 27 && self._options.exitOnEsc == true) {
//escape key pressed, exit the intro
//check if exit callback is defined
if (self._introExitCallback != undefined) {
self._introExitCallback.call(self);
}
_exitIntro.call(self, targetElm);
} else if(e.keyCode === 37) {
//left arrow
_previousStep.call(self);
} else if (e.keyCode === 39) {
//right arrow
_nextStep.call(self);
} else if (e.keyCode === 13) {
//srcElement === ie
var target = e.target || e.srcElement;
if (target && target.className.indexOf('introjs-prevbutton') > 0) {
//user hit enter while focusing on previous button
_previousStep.call(self);
} else if (target && target.className.indexOf('introjs-skipbutton') > 0) {
//user hit enter while focusing on skip button
if (self._introItems.length - 1 == self._currentStep && typeof (self._introCompleteCallback) === 'function') {
self._introCompleteCallback.call(self);
}
//check if any callback is defined
if (self._introExitCallback != undefined) {
self._introExitCallback.call(self);
}
_exitIntro.call(self, targetElm);
} else {
//default behavior for responding to enter
_nextStep.call(self);
}
//prevent default behaviour on hitting Enter, to prevent steps being skipped in some browsers
if(e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
};
self._onResize = function(e) {
_setHelperLayerPosition.call(self, document.querySelector('.introjs-helperLayer'));
_setHelperLayerPosition.call(self, document.querySelector('.introjs-tooltipReferenceLayer'));
};
if (window.addEventListener) {
if (this._options.keyboardNavigation) {
window.addEventListener('keydown', self._onKeyDown, true);
}
//for window resize
window.addEventListener('resize', self._onResize, true);
} else if (document.attachEvent) { //IE
if (this._options.keyboardNavigation) {
document.attachEvent('onkeydown', self._onKeyDown);
}
//for window resize
document.attachEvent('onresize', self._onResize);
}
}
return false;
}
/*
* makes a copy of the object
* @api private
* @method _cloneObject
*/
function _cloneObject(object) {
if (object == null || typeof (object) != 'object' || typeof (object.nodeType) != 'undefined') {
return object;
}
var temp = {};
for (var key in object) {
if (typeof (jQuery) != 'undefined' && object[key] instanceof jQuery) {
temp[key] = object[key];
} else {
temp[key] = _cloneObject(object[key]);
}
}
return temp;
}
/**
* Go to specific step of introduction
*
* @api private
* @method _goToStep
*/
function _goToStep(step) {
//because steps starts with zero
this._currentStep = step - 2;
if (typeof (this._introItems) !== 'undefined') {
_nextStep.call(this);
}
}
/**
* Go to next step on intro
*
* @api private
* @method _nextStep
*/
function _nextStep() {
this._direction = 'forward';
if (typeof (this._currentStep) === 'undefined') {
this._currentStep = 0;
} else {
++this._currentStep;
}
if ((this._introItems.length) <= this._currentStep) {
//end of the intro
//check if any callback is defined
if (typeof (this._introCompleteCallback) === 'function') {
this._introCompleteCallback.call(this);
}
_exitIntro.call(this, this._targetElement);
return;
}
var nextStep = this._introItems[this._currentStep];
if (typeof (this._introBeforeChangeCallback) !== 'undefined') {
this._introBeforeChangeCallback.call(this, nextStep.element);
}
_showElement.call(this, nextStep);
}
/**
* Go to previous step on intro
*
* @api private
* @method _nextStep
*/
function _previousStep() {
this._direction = 'backward';
if (this._currentStep === 0) {
return false;
}
var nextStep = this._introItems[--this._currentStep];
if (typeof (this._introBeforeChangeCallback) !== 'undefined') {
this._introBeforeChangeCallback.call(this, nextStep.element);
}
_showElement.call(this, nextStep);
}
/**
* Exit from intro
*
* @api private
* @method _exitIntro
* @param {Object} targetElement
*/
function _exitIntro(targetElement) {
//remove overlay layer from the page
var overlayLayer = targetElement.querySelector('.introjs-overlay');
//return if intro already completed or skipped
if (overlayLayer == null) {
return;
}