This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marionette.tooltip.js
155 lines (129 loc) · 4.42 KB
/
marionette.tooltip.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['marionette', 'underscore'], function (Marionette, _) {
return factory(Marionette, _);
});
}
else if (typeof exports !== 'undefined') {
var Marionette = require('marionette');
var _ = require('underscore');
module.exports = factory(Marionette, _);
}
else {
root.Marionette.Tooltip = factory(root.Marionette, root._);
}
}(this, function (Marionette, _) {
'use strict';
var ContainerView = Marionette.LayoutView.extend({
template: _.template('<div class="js-tooltip"></div>'),
className: 'tooltip',
regions: {
region: '.js-tooltip'
},
events: {},
initialize: function () {
var prevents = this.prevents;
if (_.isString(prevents)) {
this.events[prevents] = 'preventPropagation';
}
else if (_.isArray(prevents)) {
_.each(prevents, function (event) {
this.events[event] = 'preventPropagation';
}, this);
}
},
preventPropagation: function (event) {
event.stopPropagation();
}
});
return Marionette.Behavior.extend({
defaults: {
direction: 'top'
},
initialize: function () {
var Container = ContainerView.extend({
className: this.options.className || 'tooltip',
prevents: this.options.prevents
});
this.tooltip = new Container;
},
onShow: function () {
var View = _.result(this.view, 'getTooltip');
if (View === void 0) {
throw new Marionette.Error({
name: 'TooltipException',
message: 'tooltip is not defined'
});
}
this.tooltip.render();
this.tooltip.region.show(new View);
this.tooltip.$el.addClass('direction-' + this.options.direction);
if (this.view.ui && this.view.ui.tooltipTarget !== void 0) {
this.view.ui.tooltipTarget.after(this.tooltip.el);
}
else {
this.el.insertBefore(this.tooltip.el, null);
}
},
onDestroy: function () {
this.tooltip.destroy();
},
setPosition: function () {
var target;
if (this.view.ui && this.view.ui.tooltipTarget !== void 0) {
target = this.view.ui.tooltipTarget;
}
else {
target = this.$el;
}
// tooltip target
var parentPosition = target.position();
var parentWidth = target.outerWidth();
var parentHeight = target.outerHeight();
// tooltip element
var height = this.tooltip.$el.outerHeight();
var width = this.tooltip.$el.outerWidth();
// tooltip default offset
var top = parentPosition.top - height / 2 + parentHeight / 2;
var left = parentPosition.left - width / 2 + parentWidth / 2;
// ajust tooltip position based on tooltip direction
switch (this.options.direction) {
case 'top': {
top = parentPosition.top - height;
break;
}
case 'bottom': {
top = parentHeight + parentPosition.top;
break;
}
case 'left': {
left = parentPosition.left - width;
break;
}
case 'right': {
left = parentPosition.left + parentWidth;
break;
}
}
this.tooltip.$el.css({
top: top,
left: left
});
},
onShowTooltip: function () {
this.setPosition();
this.tooltip.$el.stop(true).fadeIn(200);
},
onHideTooltip: function () {
this.tooltip.$el.stop(true).fadeOut(200);
},
onToggleTooltip: function () {
if (this.tooltip.$el.css('display') === 'none') {
this.onShowTooltip();
}
else {
this.onHideTooltip();
}
}
});
}));