forked from nashvail/Quttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quttons.js
322 lines (276 loc) · 9.43 KB
/
Quttons.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
(function(factory) {
if (typeof module === 'object' && module.exports) {
// CommonJS
module.exports = factory(require('jquery'));
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else {
// Browser globals
window.Qutton = factory(window.jQuery);
}
})(function($) {
/********************************************
* Quttons.js *
* Quttons are buttons made of Quantum Paper *
* Author : Nash Vail *
*********************************************/
'use strict';
// Qutton Object
function Qutton(jQueryDOMElement) {
// Cache the important elements as jQuery object
this.$container = jQueryDOMElement;
// Dialog is alias of the box that pops up on clicking the Qutton
this.$dialog = this.$container.children();
// Cache the close button if it exists
this.$closeButton = this.$container.find('.close');
// When button is expanded into a dialog isOpen holds true
this.isOpen = false;
// Configuration of the popped up dialog
this.dialogConfig = {
width : this.$dialog.outerWidth(),
height : this.$dialog.outerHeight(),
backgroundColor : toHex(this.$dialog.css('background-color')),
borderRadius : this.$dialog.css('border-radius'),
zIndex : this.$dialog.css('z-index')
};
// Configuration of Qutton
this.quttonConfig = {
width : 60,
height : 60,
backgroundColor : '#EB1220',
icon : '', // Url of the icon that the button is supposed to hold
easing : 'easeInOutQuint'
};
}
// Initializes the click listeners on the qutton itself, document and close button
Qutton.prototype.init = function(quttonConfig) {
$.extend(this.quttonConfig, quttonConfig);
this.$dialog.hide();
// Set up the icon and other properties of the div
this.setIcon();
this.$container.css({
'width' : this.quttonConfig.width + 'px',
'height' : this.quttonConfig.height + 'px',
'background-color' : this.quttonConfig.backgroundColor,
'border-radius' : this.quttonConfig.height + 'px'
});
// Initialize the event handlers
this.events.click.call(this);
this.events.click_document.call(this);
this.events.click_close_button.call(this);
};
Qutton.prototype.closeDialog = function() {
var dialog = this;
if (dialog.isOpen){
dialog.setIcon();
dialog.animateOut();
} else if (dialog.isOpening) {
setTimeout(function() {
dialog.closeDialog();
}, 100);
}
};
Qutton.prototype.openDialog = function() {
this.removeIcon();
this.animateIn();
};
Qutton.prototype.setIcon = function() {
this.$container.css('background-image', 'url(' + this.quttonConfig.icon + ')');
this.$container.css('cursor', 'pointer');
};
Qutton.prototype.removeIcon = function() {
this.$container.css('background-image', 'none');
this.$container.css('cursor', 'auto');
};
// Animates the button into dialog
Qutton.prototype.animateIn = function() {
var that = this;
if (that.isOpening === true) {
return;
}
that.isOpening = true;
// Translate amount to make the dialog look like exploding from desired location
var translate = {
X : -1 * (this.dialogConfig.width/2 - this.quttonConfig.width/2),
Y : -0.5 * (this.dialogConfig.height/2 - this.quttonConfig.width/2)
};
var inSequence = [
{
e : this.$container,
p : {
width : this.dialogConfig.width +'px',
height : this.dialogConfig.height + 'px',
borderRadius : this.dialogConfig.borderRadius,
backgroundColor : this.dialogConfig.backgroundColor,
translateX : translate.X + this.keepInBounds().X + 'px',
translateY : translate.Y + this.keepInBounds().Y + 'px'
},
o : {
duration : 500,
easing : this.quttonConfig.easing,
begin : function() {
// add a placeholder in place to maintain the flow of document
if (!that.$container.next('.quttonClonePlaceHolder').length)
that.$container.after(that.$container.clone().addClass('quttonClonePlaceHolder'));
that.$container.css({
'position' : 'absolute',
'z-index' : '10000'
});
},
complete : function() {
that.isOpen = true;
that.isOpening = false;
}
}
},
{
e : this.$dialog,
p : 'fadeIn',
o : {
duration : 300,
complete : function() {
that.isOpen = true;
}
}
}
];
$.Velocity.RunSequence(inSequence);
};
// Animtes dialog into button
Qutton.prototype.animateOut = function() {
var that = this;
if (that.closing === true) {
return;
}
that.closing = true;
var outSequence = [
{
e : this.$dialog,
p : 'fadeOut',
o : {
duration : 150
}
},
{
e : this.$container,
p :{
width : this.quttonConfig.width + 'px',
height : this.quttonConfig.height + 'px',
backgroundColor : this.quttonConfig.backgroundColor,
// For a perfect circle we will give border radius the same value as height and width
borderRadius : this.quttonConfig.width,
// Neutralize movement of button after it translated to maintain position
translateX : '0px',
translateY : '0px'
},
o : {
easing : this.quttonConfig.easing,
duration : 200,
complete : function() {
// Remove the placeholder
that.$container.next('.quttonClonePlaceHolder').remove();
that.$container.css({
'position' : 'static',
'z-index' : that.dialogConfig.zIndex
});
that.isOpen = false;
that.closing = false;
}
}
}
];
$.Velocity.RunSequence(outSequence);
};
// Check if the explosion of Qutton is within the document bounds.
// Returns an object containing values to translate in X or Y direction in order to
// keep the dialog in bounds of the document on explosion.
Qutton.prototype.keepInBounds= function() {
var $window = $(window);
var windowWidth = $window.width();
var windowHeight = $window.height();
var position = this.$container.position();
// Coordinates of top center of Qutton before it converts to a a dialog
var buttonCenterTop = {
top : position.top,
left : position.left + (this.quttonConfig.width/2)
};
// Coordinates of the dialog once it opens
var dialogCoords = {
top : buttonCenterTop.top - ( 0.5 * (this.dialogConfig.height/2 - this.quttonConfig.height/2)),
left : buttonCenterTop.left - (this.dialogConfig.width/2),
};
// How much the dialog extends beyond the document
var extend = {
left : dialogCoords.left,
right : windowWidth - (dialogCoords.left + this.dialogConfig.width),
top : dialogCoords.top,
bottom : windowHeight - (dialogCoords.top + this.dialogConfig.height)
};
// Amount to translate in X and Y if possible to bring dialog in bounds of document
var translateInBounds = {
X : this.calculateTranslateAmount(extend.left, extend.right),
Y : this.calculateTranslateAmount(extend.top, extend.bottom)
};
return translateInBounds;
};
// Calculates and returns the amount to translate the dialog to keep in bounds of the window
Qutton.prototype.calculateTranslateAmount = function(extendSideOne, extendSideTwo) {
if ((extendSideOne < 0 && extendSideTwo < 0) ||
(extendSideOne > 0 && extendSideTwo > 0 )) {
return 0;
}
// We want to translate in opposite direction of extension
return (extendSideOne < 0 ? -extendSideOne : extendSideTwo);
};
// Event listeners
Qutton.prototype.events = {
// Handles the click on Qutton
click : function() {
var that = this;
this.$container.on('click', function(){
if (!that.isOpen){
that.openDialog();
}
});
},
// Handle clicks on the document, aimed at closing the dialog
click_document : function() {
var that = this;
$(document).on('click', function(event) {
if (!$(event.target).closest(that.$container.selector).length){
if (that.isOpen){
that.closeDialog();
}
}
});
},
// Initializes clicks on close button if it exists
click_close_button : function() {
var that = this;
if (this.$closeButton.length){
this.$closeButton.on('click', function(event){
if (that.isOpen){
that.closeDialog();
}
});
}
}
};
// Converts and returns RGB color code to Hex Code(String)
function toHex(rgb){
rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? '#' +
('0' + parseInt(rgb[1],10).toString(16)).slice(-2) +
('0' + parseInt(rgb[2],10).toString(16)).slice(-2) +
('0' + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}
// Factory method for producing new Material Dialog objects
function factory(jQueryDOMElement) {
if (jQueryDOMElement === null) throw new Error('Passed in element doesn\'t exist in DOM');
return new Qutton(jQueryDOMElement);
}
return {
getInstance: factory
};
});