-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjqmNavigator.js
260 lines (225 loc) · 10.9 KB
/
jqmNavigator.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
//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2012 Piotr Walczyszyn (http://outof.me | @pwalczyszyn)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//////////////////////////////////////////////////////////////////////////////////////
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(this, function ($) {
$(document).bind("mobileinit", function () {
// We want to handle link clicks from Backbone
$.mobile.linkBindingEnabled = false;
// We will handle forms programmatically
$.mobile.ajaxEnabled = false;
// We don't need this as we will be doing page navigation programmatically
$.mobile.hashListeningEnabled = false;
// We don't need this as we will be doing page navigation programmatically
$.mobile.pushStateEnabled = false;
// Turning off page auto initialization, we want to control it programmatically
$.mobile.autoInitializePage = false;
$.extend($.mobile, {
jqmNavigator:{
VERSION:'1.0.0',
/**
* Map of containers and views
*/
_containers:[],
/**
* If this is not set, jqmNavigator will use body tag as default container
*/
defaultPageContainer:null,
/**
* Pushes view to the stack.
*
* @param view {Backbone.View}
* @param options {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
pushView:function jqmNavigator_pushView(view, options) {
var containerViews = this._getPageContainerViews(options);
// Pushing the view to the stack
containerViews.views.push(view);
// Appending the view to the DOM
containerViews.pageContainer.append(view.el);
// Rendering the view
view.render();
if (!$.mobile.firstPage) {
// Adding data-role with page value
view.$el.attr('data-role', 'page');
// First time initialization
if (!$.mobile.autoInitializePage) $.mobile.initializePage();
} else {
// Changing page
$.mobile.changePage(view.$el, $.extend({
role:'page',
changeHash:false,
pageContainer:containerViews.pageContainer
}, options));
}
},
/**
* Pops view from the stack.
*
* @param options {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
popView:function jqmNavigator_popView(options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length > 1) {
// From view ref
var fromView = containerViews.views.pop();
// To view ref
toView = containerViews.views[containerViews.views.length - 1];
fromView.$el.one('pagehide', function (event) {
// Detaching view from DOM
fromView.$el.detach();
});
// Changing to view below current one
$.mobile.changePage(toView.$el, $.extend({
role:'page',
reverse:true,
changeHash:false,
pageContainer:containerViews.pageContainer
}, options));
} else {
console.log('Can\'t pop first view, you can replace it instead!');
}
},
/**
* Pops views from a stack up to the first one.
*
* @param options {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
popToFirst:function jqmNavigator_popToFirst(options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length > 1) {
// From view ref
var fromView = containerViews.views[containerViews.views.length - 1],
// To view ref
toView = containerViews.views[0],
// Removed views
removedViews = containerViews.views.splice(1, containerViews.views.length - 1);
fromView.$el.one('pagehide', function (event) {
removedViews.forEach(function (item) {
item.$el.detach();
}, this);
});
// Changing to view below current one
$.mobile.changePage(toView.$el, $.extend({
role:'page',
reverse:true,
changeHash:false,
pageContainer:containerViews.pageContainer
}, options));
} else {
console.log('Can\'t pop first view, you can replace it instead!');
}
},
/**
* Replaces current view on the stack.
*
* @param options {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
replaceView:function jqmNavigator_replaceView(view, options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length >= 1) {
// From view ref
var fromView = containerViews.views.pop();
fromView.$el.one('pagehide', function (event) {
// Detaching view from DOM
fromView.$el.detach();
});
// Pushing the view to the stack
containerViews.views.push(view);
// Appending the view to the DOM
containerViews.pageContainer.append(view.el);
// Rendering the view
view.render();
// Changing page
$.mobile.changePage(view.$el, $.extend({
role:'page',
changeHash:false,
pageContainer:containerViews.pageContainer
}, options));
}
},
/**
* Replaces all views on the stack.
*
* @param options {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
replaceAll:function jqmNavigator_replaceAll(view, options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length >= 1) {
// From view ref
var fromView = containerViews.views[containerViews.views.length - 1],
// Removed views
removedViews = containerViews.views.splice(0, containerViews.views.length);
fromView.$el.one('pagehide', function (event) {
removedViews.forEach(function (item) {
item.$el.detach();
}, this);
});
// Pushing the view to the stack
containerViews.views.push(view);
// Appending the view to the DOM
containerViews.pageContainer.append(view.el);
// Rendering the view
view.render();
// Changing page
$.mobile.changePage(view.$el, $.extend({
role:'page',
changeHash:false,
pageContainer:containerViews.pageContainer
}, options));
}
},
_getPageContainerViews:function (options) {
var pageContainer = options && options.pageContainer ? options.pageContainer :
$.mobile.pageContainer || this.defaultPageContainer || $('body'),
result;
this._containers.some(function (item) {
if (item.pageContainer[0] === pageContainer[0]) {
result = item;
return true;
}
}, this);
if (!result) this._containers.push(result = {pageContainer:pageContainer, views:[]});
return result;
},
/**
* Returns an array of views for specified pageContainer. If pageContainer param is omitted it tries to
* return views of default container.
*
* @param pageContainer
* @return {*}
*/
getViews:function jqmNavigator_getViews(pageContainer) {
var views,
pc = pageContainer ? pageContainer[0] : ($.mobile.pageContainer ? $.mobile.pageContainer[0] : null);
this._containers.some(function (item) {
if (item.pageContainer[0] === pc) {
views = item.views;
return true;
}
}, this);
return views;
}
}
});
});
}));