forked from pablof7z/chardin.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chardinjs.js
228 lines (203 loc) · 8.47 KB
/
chardinjs.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
// Generated by CoffeeScript 1.6.2
(function() {
var __slice = [].slice;
(function($, window) {
var chardinJs;
chardinJs = (function() {
function chardinJs(el) {
var _this = this;
this.$el = $(el);
$(window).resize(function() {
return _this.refresh();
});
}
chardinJs.prototype.start = function() {
var el, _i, _len, _ref;
if (this._overlay_visible()) {
return false;
}
this._add_overlay_layer();
_ref = this.$el.find('*[data-intro]');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
this._show_element(el);
}
return this.$el.trigger('chardinJs:start');
};
chardinJs.prototype.toggle = function() {
if (!this._overlay_visible()) {
return this.start();
} else {
return this.stop();
}
};
chardinJs.prototype.refresh = function() {
var el, _i, _len, _ref, _results;
if (this._overlay_visible()) {
_ref = this.$el.find('*[data-intro]');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
_results.push(this._position_helper_layer(el));
}
return _results;
} else {
return this;
}
};
chardinJs.prototype.stop = function() {
this.$el.find(".chardinjs-overlay").fadeOut(function() {
return $(this).remove();
});
this.$el.find('.chardinjs-helper-layer').remove();
this.$el.find('.chardinjs-show-element').removeClass('chardinjs-show-element');
this.$el.find('.chardinjs-relative-position').removeClass('chardinjs-relative-position');
if (window.removeEventListener) {
window.removeEventListener("keydown", this._onKeyDown, true);
} else {
if (document.detachEvent) {
document.detachEvent("onkeydown", this._onKeyDown);
}
}
return this.$el.trigger('chardinJs:stop');
};
chardinJs.prototype._overlay_visible = function() {
return this.$el.find('.chardinjs-overlay').length !== 0;
};
chardinJs.prototype._add_overlay_layer = function() {
var element_position, overlay_layer, styleText,
_this = this;
if (this._overlay_visible()) {
return false;
}
overlay_layer = document.createElement("div");
styleText = "";
overlay_layer.className = "chardinjs-overlay";
if (this.$el.prop('tagName') === "BODY") {
styleText += "top: 0;bottom: 0; left: 0;right: 0;position: fixed;";
overlay_layer.setAttribute("style", styleText);
} else {
element_position = this._get_offset(this.$el.get()[0]);
if (element_position) {
styleText += "width: " + element_position.width + "px; height:" + element_position.height + "px; top:" + element_position.top + "px;left: " + element_position.left + "px;";
overlay_layer.setAttribute("style", styleText);
}
}
this.$el.get()[0].appendChild(overlay_layer);
overlay_layer.onclick = function() {
return _this.stop();
};
return setTimeout(function() {
styleText += "opacity: .8;";
return overlay_layer.setAttribute("style", styleText);
}, 10);
};
chardinJs.prototype._get_position = function(element) {
return element.getAttribute('data-position') || 'bottom';
};
chardinJs.prototype._place_tooltip = function(element) {
var my_height, my_width, target_element_position, target_height, target_width, tooltip_layer, tooltip_layer_position;
tooltip_layer = $(element).data('tooltip_layer');
tooltip_layer_position = this._get_offset(tooltip_layer);
tooltip_layer.style.top = null;
tooltip_layer.style.right = null;
tooltip_layer.style.bottom = null;
tooltip_layer.style.left = null;
switch (this._get_position(element)) {
case "top":
case "bottom":
target_element_position = this._get_offset(element);
target_width = target_element_position.width;
my_width = $(tooltip_layer).width();
tooltip_layer.style.left = "" + ((target_width / 2) - (tooltip_layer_position.width / 2)) + "px";
break;
case "left":
case "right":
target_element_position = this._get_offset(element);
target_height = target_element_position.height;
my_height = $(tooltip_layer).height();
tooltip_layer.style.top = "" + ((target_height / 2) - (tooltip_layer_position.height / 2)) + "px";
}
switch (this._get_position(element)) {
case "left":
return tooltip_layer.style.left = "-" + (tooltip_layer_position.width - 34) + "px";
case "right":
return tooltip_layer.style.right = "-" + (tooltip_layer_position.width - 34) + "px";
case "bottom":
return tooltip_layer.style.bottom = "-" + tooltip_layer_position.height + "px";
case "top":
return tooltip_layer.style.top = "-" + tooltip_layer_position.height + "px";
}
};
chardinJs.prototype._position_helper_layer = function(element) {
var element_position, helper_layer;
helper_layer = $(element).data('helper_layer');
element_position = this._get_offset(element);
return helper_layer.setAttribute("style", "width: " + element_position.width + "px; height:" + element_position.height + "px; top:" + element_position.top + "px; left: " + element_position.left + "px;");
};
chardinJs.prototype._show_element = function(element) {
var current_element_position, element_position, helper_layer, tooltip_layer;
element_position = this._get_offset(element);
helper_layer = document.createElement("div");
tooltip_layer = document.createElement("div");
$(element).data('helper_layer', helper_layer).data('tooltip_layer', tooltip_layer);
if (element.id) {
helper_layer.setAttribute("data-id", element.id);
}
helper_layer.className = "chardinjs-helper-layer chardinjs-" + (this._get_position(element));
this._position_helper_layer(element);
this.$el.get()[0].appendChild(helper_layer);
tooltip_layer.className = "chardinjs-tooltip chardinjs-" + (this._get_position(element));
tooltip_layer.innerHTML = "<div class='chardinjs-tooltiptext'>" + (element.getAttribute('data-intro')) + "</div>";
helper_layer.appendChild(tooltip_layer);
this._place_tooltip(element);
element.className += " chardinjs-show-element";
current_element_position = "";
if (element.currentStyle) {
current_element_position = element.currentStyle["position"];
} else {
if (document.defaultView && document.defaultView.getComputedStyle) {
current_element_position = document.defaultView.getComputedStyle(element, null).getPropertyValue("position");
}
}
current_element_position = current_element_position.toLowerCase();
if (current_element_position !== "absolute" && current_element_position !== "relative") {
return element.className += " chardinjs-relative-position";
}
};
chardinJs.prototype._get_offset = function(element) {
var element_position, _x, _y;
element_position = {
width: element.offsetWidth,
height: element.offsetHeight
};
_x = 0;
_y = 0;
while (element && !isNaN(element.offsetLeft) && !isNaN(element.offsetTop)) {
_x += element.offsetLeft;
_y += element.offsetTop;
element = element.offsetParent;
}
element_position.top = _y;
element_position.left = _x;
return element_position;
};
return chardinJs;
})();
return $.fn.extend({
chardinJs: function() {
var $this, args, data, option;
option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
$this = $(this[0]);
data = $this.data('chardinJs');
if (!data) {
$this.data('chardinJs', (data = new chardinJs(this, option)));
}
if (typeof option === 'string') {
data[option].apply(data, args);
}
return data;
}
});
})(window.jQuery, window);
}).call(this);