-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdepage-markerbox.js
More file actions
367 lines (323 loc) · 12.2 KB
/
depage-markerbox.js
File metadata and controls
367 lines (323 loc) · 12.2 KB
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
/**
* @require framework/shared/jquery-1.12.3.min.js
*
* @file depage-marker-box
*
* Object for extending in plugins that need markerbox functionality
*
* copyright (c) 2006-2012 Frank Hellenkamp [jonas@depage.net]
*
* @author Ben Wallis
*/
(function($){
if(!$.depage){
$.depage = {};
}
/**
* markerbox
*
* @param options
*/
$.depage.markerbox = function(options) {
var base = {};
String.prototype.replaceAt = function(index, replacement) {
return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
};
// {{{ init
/**
* Init
*
* Get the plugin options.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.markerbox.defaultOptions, options);
};
// }}}
$.extend(base, {
// reference wrapper divs
$dialogue : null,
$wrapper : null,
$contentWrapper : null,
$directionMarker : null,
// {{{ show()
/**
* Show
*
* @return void
*/
show: function(left, top) {
var direction = base.options.direction.toLowerCase();
var that = this;
that.addWrapper();
that.setContent(base.options.title, base.options.message, base.options.icon);
setTimeout(function() {
// defer setting position until element has added all children
that.setPosition(top, left, base.options.direction);
that.$wrapper.addClass("visible");
}, 10);
// allow chaining
return this;
},
// }}}
// {{{ hide()
/**
* Hide
*
* @param duration - gradually fades out default 300
* @param callback - optional callback function
*
* @return void
*/
hide: function(duration, callback) {
duration = duration || base.options.fadeoutDuration;
if (!this.$wrapper) return this;
this.$wrapper.removeClass("visible");
// allow chaining
return this;
},
// }}}
// {{{ hideAfter()
/**
* HideAfter
*
* hides dialog automatically after a duration
*
* @param duration - duration after
* @param callback - optional callback function
*
* @return void
*/
hideAfter : function(duration, callback) {
setTimeout(function(){
base.hide(base.options.fadeoutDuration, callback);
}, duration);
// allow chaining
return this;
},
// }}}
// {{{ addWrapper()
/**
* removes old and adds the new html wrapper
*
* @return void
*/
addWrapper : function() {
// remove old wrapper (also with multiple dialogues)
$('#' + base.options.id).remove();
this.$dialogue = $('<div />');
this.$wrapper = $('<div class="wrapper" />');
this.$dialogue.append(this.$wrapper);
if (base.options.directionMarker) {
// add direction marker
this.$directionMarker = $('<span class="direction-marker" />');
this.$wrapper.append(this.$directionMarker);
}
this.$contentWrapper = $('<div class="message" />');
this.$wrapper.append(this.$contentWrapper);
$("body").append(this.$dialogue);
this.$wrapper.data("depage.markerbox", base);
this.$dialogue.attr({
'class': "depage-markerbox " + base.options.classes,
'id': base.options.id
});
// allow chaining
return this;
},
// }}}
// {{{ setPosition()
/**
* set the position of the dialogue including the direction marker
*
* @return void
*/
setPosition : function(top, left, d) {
d = d.toLowerCase();
directions = {
tl: 'right',
cl: 'right',
bl: 'right',
tr: 'left',
br: 'left',
cr: 'left',
tc: 'bottom',
cc: '',
bc: 'top',
};
var wrapperHeight = this.$wrapper.height();
var wrapperWidth = this.$wrapper.width();
var paddingLeft = parseInt(this.$wrapper.css("padding-left"), 10);
var paddingRight = parseInt(this.$wrapper.css("padding-right"), 10);
var paddingTop = parseInt(this.$wrapper.css("padding-top"), 10);
var paddingBottom = parseInt(this.$wrapper.css("padding-bottom"), 10);
var offset = base.options.positionOffset;
var dHeight = 0,
dWidth = 0,
pos;
if (typeof(this.$directionMarker) !== "undefined") {
dHeight = this.$directionMarker.height();
dWidth = this.$directionMarker.width();
} else {
dHeight = - paddingTop * 2;
dWidth = - paddingLeft * 2;
}
pos = this.adjustPositionToElement(top, left, d, this.$el);
// adjust position to always be inside of view
// @todo center on very small screens?
var elWidth = !left ? this.$el.width() : 0;
var outOfBoundsLeft = pos.left - wrapperWidth - dWidth - elWidth < 10;
var outOfBoundsRight = pos.left + wrapperWidth + dWidth + elWidth > $(window).width() - 10;
if (outOfBoundsLeft && outOfBoundsRight) {
d = d.replaceAt(1, "c");
} else if (d[1] == "r" && outOfBoundsRight) {
d = d.replaceAt(1, "l");
} else if (d[1] == "l" && outOfBoundsLeft) {
d = d.replaceAt(1, "r");
}
pos = this.adjustPositionToElement(top, left, d, this.$el);
this.$dialogue.attr("style", "position: absolute; top: " + pos.top + "px; left: " + pos.left + "px; z-index: 10000");
var wrapperPos = {};
var markerPos = {};
// vertical
switch (d[0]) {
case 't': // top
wrapperPos.top = - paddingTop - dHeight * 0.5;
if (d[1] != "c") markerPos.top = paddingTop;
break;
case 'c': // center
wrapperPos.top = - (wrapperHeight + paddingTop + paddingBottom) / 2;
break;
case 'b': // bottom
wrapperPos.bottom = - paddingBottom - dHeight * 0.5;
if (d[1] != "c") markerPos.bottom = paddingBottom;
break;
}
// horizontal
switch (d[1]) {
case 'l': // left
wrapperPos.right = dHeight + offset - dHeight * 0.5;
markerPos.right = -dHeight;
break;
case 'r': // right
wrapperPos.left = dHeight + offset - dHeight * 0.5;
markerPos.left = -dHeight;
break;
case 'c': // center
wrapperPos.left = - (wrapperWidth + paddingLeft + paddingRight) / 2;
if (d[0] == "t") {
wrapperPos.top -= (wrapperHeight + paddingTop + offset);
} else if (d[0] == "b") {
wrapperPos.bottom -= (wrapperHeight + paddingBottom + offset);
}
if (d[0] == "t" || d[0] == "b") { // horizontal
markerPos.left = (wrapperWidth + paddingLeft + paddingRight) / 2 - dWidth / 2;
}
break;
}
if (d == "tc") {
markerPos.bottom = -dHeight;
} else if (d == "bc") {
markerPos.top = -dHeight;
}
this.$wrapper.css(wrapperPos);
if (typeof(this.$directionMarker) !== "undefined") {
this.$directionMarker.css(markerPos).attr("class", "direction-marker " + directions[d]);
}
},
// }}}
// {{{ adjustPositionToElement()
/**
* set the position of the dialogue including the direction marker
*
* @return void
*/
adjustPositionToElement : function(top, left, d, $el) {
d = d.toLowerCase();
var o = $el.offset();
// get position from current element
if (!left) {
left = o.left;
if (d[1] == "r") {
left += $el.width();
} else if (d[1] == "c") {
left += $el.width() * 0.5;
}
}
if (!top) {
top = o.top;
if (d[0] == "t" && d[1] != "c") {
top += base.options.positionOffset;
} else if (d[0] == "b" && d[1] != "c") {
top += $el.height() - base.options.positionOffset;
} else if (d[0] == "b") {
top += $el.height();
} else if (d[0] == "c") {
top += $el.height() * 0.5;
}
}
top = Math.ceil(top);
left = Math.ceil(left);
return {
top: Math.ceil(top),
left: Math.ceil(left)
};
},
// }}}
// {{{ setContent()
/**
* setContent
*
* @param title
* @param message
* @param icon (optional)
*
* @return void
*/
setContent : function(title, message, icon) {
if (typeof title == 'function') {
title = title();
}
if (typeof message == 'function') {
message = message();
}
var $title = $('<h1 />').text(title);
if (Object.getPrototypeOf(message).jquery) {
var $message = message.clone();
} else {
var $message = $('<p />').text(message);
}
this.$contentWrapper.empty()
.append($title)
.append($message);
// allow chaining
return this;
}
});
base.init();
return base;
};
/**
* Default Options
*
* id - the id of the dialogue element wrapper to display
* message - message the dialouge will display
* buttons - buttons to supply (with corresponding event triggered)
* classes - css classes to supply to the wrapper and content elements
*
*/
$.depage.markerbox.defaultOptions = {
id : 'depage-markerbox',
classes : 'depage-markerbox',
icon: '',
title: '',
message: '',
direction : 'TL',
directionMarker : null,
positionOffset: 10,
fadeinDuration: 300,
fadeoutDuration: 300
};
// }}}
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */