-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.modal.js
274 lines (234 loc) · 7.08 KB
/
gemini.modal.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
/* global Templates */
/**
* @fileoverview
A Gemini plugin to to easily pop content up in a modal.
### Notes
- Requires an include to ``accordian.scss`` in your Gemini build
- Without the fixed option set, body height needs to be 100%
### Features
- You can call ``$el.modal()`` to put the content of $el into a modal. To avoid
rendering the same content twice, you can put it in a ``<script>`` tag.
*
* @namespace gemini.modal
* @copyright Carpages.ca 2014
* @author Matt Rose <[email protected]>
*
* @requires gemini
*
* @prop {string} content {@link gemini.modal#content}
* @prop {function} onOpen {@link gemini.modal#onOpen}
* @prop {function} onClose {@link gemini.modal#onClose}
* @prop {integer} fadeIn {@link gemini.modal#fadeIn}
* @prop {integer} fadeOut {@link gemini.modal#fadeOut}
* @prop {boolean} closeable {@link gemini.modal#closeable}
* @prop {boolean} fixed {@link gemini.modal#fixed}
* @prop {boolean} stopPropagation {@link gemini.modal#stopPropagation}
* @prop {object} templates {@link gemini.modal#templates}
*
* @example
var modal = G.Modal({
content: '<h1>Hello World!</h1>'
});
modal.open();
*/
( function( factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define([ 'gemini', 'gemini.modal.templates' ], factory );
} else if ( typeof exports === 'object' ) {
// Node/CommonJS
module.exports = factory( require( 'gemini-loader' ), require( './templates.js' ));
} else {
// Browser globals
factory( G, Templates );
}
})( function( $, T ) {
var _ = $._;
// Make an object to be used by both $.modal and $.fn.modal
$.Modal = function( options ) {
var plugin = {
settings: $.extend(
{},
{
/**
* The HTML content to put in the modal
*
* @name gemini.modal#content
* @type string
* @default ''
*/
content: '',
/**
* Callback function to run when the modal opens
*
* @name gemini.modal#onOpen
* @type function
* @default false
*/
onOpen: false,
/**
* Callback function to run when the modal closes
*
* @name gemini.modal#onClose
* @type function
* @default false
*/
onClose: false,
/**
* The speed that the modal fades in at in milliseconds
*
* @name gemini.modal#fadeIn
* @type integer
* @default 250
*/
fadeIn: 250,
/**
* The speed that the modal fades out at in milliseconds
*
* @name gemini.modal#fadeOut
* @type integer
* @default 250
*/
fadeOut: 250,
/**
* Weather or not the user can manually close the modal
*
* @name gemini.modal#closeable
* @type boolean
* @default true
*/
closeable: true,
/**
* Whether to position the modal wrapper as fixed or not. This setting
* will cut off content if the screen is too small.
*
* @name gemini.modal#fixed
* @type boolean
* @default false
*/
fixed: false,
/**
* A selector describing the content of the modal. Anything clicked
* outside of these items will close the modal.
*
* @name gemini.modal#stopPropagation
* @type string
* @default '#js-modal__content'
*/
stopPropagation: '.js-modal__content',
/**
* Precompiled Handlebar templates to replace default. Expecting 'modal'
* @name jquery.gallery#templates
* @type object
* @default {}
*/
templates: {}
},
options
),
init: function() {
var plugin = this;
// Extend the templates
plugin.T = $.extend( T, plugin.settings.templates );
// Cache wrapper, modal, and exit
plugin.$modal = $( plugin.T.modal())._hide();
plugin.$content = plugin.$modal.find( '.js-modal__content' );
plugin.$exit = plugin.$modal.find( '.js-modal__close' );
// Add content
plugin.$content.html( plugin.settings.content );
// Append modal to body
$( 'body' ).append( plugin.$modal );
if ( plugin.settings.closeable ) {
plugin._closeListeners();
}
},
/**
* Open the modal
*
* @method
* @name gemini.modal#open
**/
open: function() {
var plugin = this;
// Calculate top if not fixed
if ( plugin.settings.fixed ) {
plugin.$modal.addClass( 'modal--fixed' );
} else {
var top = ( $( window ).height() - plugin.$content.height()) / 2;
top = Math.max( top, 0 );
plugin.$content.css( 'top', $( window ).scrollTop() + top );
}
plugin.$modal.addClass( 'is-active' )._fadeIn( plugin.settings.fadeIn );
if ( plugin.settings.closeable ) {
plugin.$exit.fadeIn( plugin.settings.fadeIn );
}
if ( plugin.settings.onOpen ) plugin.settings.onOpen.call( plugin );
},
/**
* Close the modal
*
* @method
* @name gemini.modal#close
**/
close: function() {
var plugin = this;
plugin.$modal.removeClass( 'is-active' )._fadeOut( plugin.settings.fadeOut );
if ( plugin.settings.closeable ) {
plugin.$exit.fadeOut( plugin.settings.fadeOut );
}
if ( plugin.settings.onClose ) plugin.settings.onClose.call( plugin );
},
/**
* Update the content inside of the modal
*
* @method
* @name gemini.modal#update
* @param {string} content The HTML content to put inside of the modal
**/
update: function( content ) {
plugin.$content.html( content );
},
_closeListeners: function() {
var plugin = this;
// Close event on wrapper click and exit click
var $stop = plugin.$modal.find(
_.filter([ '.js-modal__clickable', plugin.settings.stopPropagation ], Boolean ).join( ', ' )
);
var stop = false;
plugin.$modal.click( function() {
if ( stop ) {
stop = false;
} else {
plugin.close();
}
});
$stop.click( function( e ) {
stop = true;
});
plugin.$exit.click( function() {
plugin.close();
});
}
};
plugin.init();
return plugin;
};
$.boiler( 'modal', {
defaults: {},
init: function() {
var plugin = this;
plugin.modal = new $.Modal({
content: plugin.$el.html()
});
},
open: function() {
this.modal.open();
},
close: function() {
this.modal.close();
}
});
// Return the jquery object
// This way you don't need to require both jquery and the plugin
return $;
});