-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructor.js
103 lines (88 loc) · 2.33 KB
/
instructor.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
(function ( $ ) {
$.widget("msi.instructor", {
options : {
steps : []
},
_create : function () {
this.steps = this.options.steps;
this.currentStep = this.steps[0];
this.currentTip = this.currentStep.tips[0];
},
_getById : function ( id ) {
var $el = $('#' + id);
if($el.length) {
return $el;
} else {
console.error('No element found with id "' + id + '"');
}
},
_getByAttr : function ( attrName, attrVal ) {
if(!attrVal) {
return $('[' + attrName + ']');
}
return $('[' + attrName + '="' + attrVal + '"]');
},
_getStepByName : function ( name ) {
var step;
$.each(this.steps, function ( index, item ) {
if(item.title == name) {
step = item;
}
});
if (!step) {
throw new Error('Step "' + name + '" was not found');
}
return step;
},
_getStepIndex : function ( step ) {
return this.steps.indexOf(step);
},
_getTipIndex : function ( tip ) {
return this.currentStep.tips.indexOf(tip);
},
tooltip : function ( id, message, title ) {
var $el = this._getById(id);
var options = {
target: true,
tipJoint:"left",
targetJoint: "right",
showOn: "creation",
hideTrigger: 'closeButton',
hideOn: [],
offset: [-20, 0]
};
var tooltip = new Opentip($el, message, title, options);
},
goToStep : function ( title ) {
var $stepLabel = this._getByAttr('data-instruct-step', title)
, step = this._getStepByName(title);
this.currentStep = step;
this.currentTip = step.tips[0];
if(step.onStart) {
step.onStart.call(step);
}
},
nextStep : function () {
var currentStepIndex = this._getStepIndex(this.currentStep)
, nextStep = this.steps[currentStepIndex + 1]
, nextStepIndex = this._getStepIndex(nextStep);
if(nextStepIndex > -1) {
var nextStep = this.steps[currentStepIndex + 1];
this.goToStep(nextStep.title);
} else {
console.log('No more steps in tutorial');
}
},
nextTip : function () {
var currentTipIndex = this._getTipIndex(this.currentTip)
, nextTip = this.currentStep.tips[currentTipIndex + 1]
, nextTipIndex = this._getTipIndex(nextTip);
if(currentTipIndex > -1) {
this.tooltip(this.currentTip.element, this.currentTip.message, this.currentTip.title);
} else {
this.nextStep();
}
this.currentTip = nextTip;
}
});
})(jQuery);