forked from Pagawa/PgwModal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgwmodal.js
More file actions
196 lines (166 loc) · 6.4 KB
/
Copy pathpgwmodal.js
File metadata and controls
196 lines (166 loc) · 6.4 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
/**
* PgwModal - Version 1.4
*
* Copyright 2014, Jonathan M. Piat
* http://pgwjs.com - http://pagawa.com
*
* Released under the GNU GPLv3 license - http://opensource.org/licenses/gpl-3.0
*/
;(function($){
$.pgwModal = function(obj) {
var pgwModal = {};
var defaults = {
close: true,
maxWidth: 500,
loading: 'Loading in progress...',
error: 'An error has occured. Please try again in a few moments.'
};
if (typeof window.pgwModalObject != 'undefined') {
pgwModal = window.pgwModalObject;
}
// Merge the defaults and the user's config
if ((typeof obj == 'object') && (! obj.pushContent)) {
if (! obj.url && ! obj.target && ! obj.content) {
throw new Error('PgwModal - There is no content to display, please provide a config parameter : "url", "target" or "content"');
}
pgwModal.config = {};
pgwModal.config = $.extend({}, defaults, obj);
window.pgwModalObject = pgwModal;
}
// Create modal container
var create = function() {
var appendBody = '<div id="pgwModalWrapper"></div>'
+ '<div id="pgwModal">'
+ '<div class="pm-container">'
+ '<div class="pm-body">'
+ '<a href="javascript:void(0)" class="pm-close" onclick="$.pgwModal(\'close\')"></a>'
+ '<div class="pm-title"></div>'
+ '<div class="pm-content cntr"></div>'
+ '</div>'
+ '</div>'
+ '</div>';
$('body').append(appendBody);
$(document).trigger('PgwModal::Create');
return true;
};
// Reset modal container
var reset = function() {
$('#pgwModal .pm-title, #pgwModal .pm-content').html('');
return true;
};
// Angular compilation
var angularCompilation = function() {
angular.element('body').injector().invoke(function($compile) {
var scope = angular.element($('#pgwModal .pm-content')).scope();
$compile($('#pgwModal .pm-content'))(scope);
scope.$digest();
});
return true;
};
// Push content into the modal
var pushContent = function(content) {
$('#pgwModal .pm-content').html(content);
if (pgwModal.config.angular) {
angularCompilation();
}
reposition();
$(document).trigger('PgwModal::PushContent');
return true;
};
// Repositions the modal
var reposition = function() {
// elements must be visible before height calculation
$('#pgwModal, #pgwModalWrapper').show();
var windows_height = $(window).height();
var modal_height = $('#pgwModal .pm-body').height();
var margin_top = Math.round((windows_height - modal_height)/3);
if (margin_top <= 0) {
margin_top = 10;
}
$('#pgwModal .pm-body').css('margin-top', margin_top);
return true;
};
// Returns the modal data
var getData = function() {
return pgwModal.config.modalData;
};
// Returns the modal status
var isOpen = function() {
return $('body').hasClass('pgwModal');
};
// Close the modal
var close = function() {
$('#pgwModal, #pgwModalWrapper').hide();
$('body').removeClass('pgwModal');
reset();
try {
delete window.pgwModalObject;
} catch(e) {
window['pgwModalObject'] = undefined;
}
$(document).trigger('PgwModal::Close');
return true;
};
// Open the modal
var open = function() {
if ($('#pgwModal').length == 0) {
create();
} else {
reset();
}
if (! pgwModal.config.close) {
$('#pgwModal .pm-close').hide();
} else {
$('#pgwModal .pm-close').show();
}
if (pgwModal.config.title) {
$('#pgwModal .pm-title').text(pgwModal.config.title);
}
if (pgwModal.config.maxWidth) {
$('#pgwModal .pm-body').css('max-width', pgwModal.config.maxWidth);
}
// Content loaded by Ajax
if (pgwModal.config.url) {
if (pgwModal.config.loading) {
$('#pgwModal .pm-content').html(pgwModal.config.loading);
}
var ajaxOptions = {
'url' : obj.url,
'success' : function(data) {
pushContent(data);
},
'error' : function() {
$('#pgwModal .pm-content').html(pgwModal.config.error);
}
};
if (pgwModal.config.ajaxOptions) {
ajaxOptions = $.extend({}, ajaxOptions, pgwModal.config.ajaxOptions);
}
$.ajax(ajaxOptions);
// Content loaded by a html element
} else if (pgwModal.config.target) {
pushContent($(pgwModal.config.target).html());
// Content loaded by a html element
} else if (pgwModal.config.content) {
pushContent(pgwModal.config.content);
}
$('body').addClass('pgwModal');
$(document).trigger('PgwModal::Open');
return true;
};
// Choose the action
if ((typeof obj == 'string') && (obj == 'close')) {
return close();
} else if ((typeof obj == 'string') && (obj == 'reposition')) {
return reposition();
} else if ((typeof obj == 'string') && (obj == 'getData')) {
return getData();
} else if ((typeof obj == 'string') && (obj == 'isOpen')) {
return isOpen();
} else if ((typeof obj == 'object') && (obj.pushContent)) {
return pushContent(obj.pushContent);
} else if (typeof obj == 'object') {
return open();
}
}
})(window.Zepto || window.jQuery);