-
Notifications
You must be signed in to change notification settings - Fork 16
/
jquery.mobile.dynamic.popup.js
144 lines (105 loc) · 4.84 KB
/
jquery.mobile.dynamic.popup.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
/**
Copyright (c) 2012-2014 Serban Ghita <[email protected]>
jQuery Mobile dynamic popup. MIT Licensed
*/
(function($){
function dynamicPopup(){
var settings,
$popup,
$popupMain,
$popupContent,
$popupCloseBtn,
$activePage = $.mobile.activePage;
this.init = function( options ){
// Extend the general settings.
settings = $.extend({
content: '',
popupId: 'popup' + $activePage.attr('id'),
'data-theme': 'a',
'data-overlay-theme': 'none',
'data-position-to': 'window',
'data-rel': 'back',
'data-dismissible': true,
'data-transition': 'none',
'data-arrow': false
},
options);
// Sending a plain text or HTML message.
if(typeof options === 'string'){
settings.content = options;
}
this.createPopupElements();
this.populatePopupContent();
this.putPopupInDOM();
this.openPopup();
return $popup;
}
// Create the popup objects without the actual contents.
// If the popup container exists return it's objects.
this.createPopupElements = function(){
$popup = $('#' + settings.popupId);
// New popup, it doesn't exist.
if( !$popup.length ){
// Create the generic popup elements.
$popupMain = $('<div></div>').attr({
'id': settings.popupId,
'data-role': 'popup',
'data-theme': settings['data-theme'],
'data-overlay-theme': settings['data-overlay-theme'],
'data-position-to': settings['data-position-to'],
'data-dismissible': settings['data-dismissible'],
'data-transition': settings['data-transition'],
'data-arrow': settings['data-arrow']
})
.addClass('ui-popup-main');
$popupContent = $('<div></div>').attr({
'data-role': 'content'
})
.addClass('ui-content ui-popup-content');
$popupCloseBtn = $('<a></a>').attr({
'href': '#',
'data-role': 'button',
'data-rel': settings['data-rel']
})
.addClass('ui-btn ui-corner-all ui-shadow ui-btn-a ui-icon-delete ui-btn-icon-notext ui-btn-right ui-popup-close-btn');
} else {
// Find the existing HTML wrappers.
$popupMain = $popup;
$popupContent = $popup.find('.ui-popup-content');
$popupCloseBtn = $popup.find('.ui-popup-close-btn');
}
}
// Populate the popup's content.
this.populatePopupContent = function(){
// 1. Static HTML string.
if(typeof settings.content === 'string'){
$popupContent.html( settings.content );
}
// 2. jQuery object.
if(settings.content instanceof jQuery){
// Grab the content inside the wrapper.
$popupContent.html( settings.content.html() );
}
}
this.putPopupInDOM = function(){
// Remove the existing popup from DOM.
$popup.remove();
// Tie together the HTML elements.
$popupMain.append( $popupCloseBtn ).append( $popupContent );
// Append the popup to the current page.
$activePage.append( $popupMain );
}
this.openPopup = function(){
// Init.
$popup = $('#' + settings.popupId);
$popup.popup( settings );
// Open.
$popup.popup('open');
}
}
// Register the plugin.
$.dynamic_popup = function( options ){
var popup = new dynamicPopup();
return popup.init( options );
}
})(jQuery);