forked from pwalczyszyn/jqmNavigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqmNavigator.js
382 lines (310 loc) · 14.8 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//////////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2012 Piotr Walczyszyn (http://outof.me | @pwalczyszyn)
// Copyright 2013 Jonathan Nunez ([email protected])
//
// 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.4.0',
/**
* Map of containers and views
*/
_containers: [],
/**
* If this is not set, jqmNavigator will use body tag as default container
*/
defaultPageContainer: null,
/**
* Get the current page.
*
* @param [options]
* @returns {*}
*/
currentView: function (options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length > 0) {
return containerViews.views[containerViews.views.length - 1];
} else {
return false;
}
},
/**
*
* Pushes view to the stack.
*
* @param view instance of Backbone View
* @param [options] {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
* @param [endCallback] Callback when the target view is shown
*/
pushView: function (view, options, endCallback) {
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 (endCallback) {
view.$el.one('pageshow', function () {
endCallback();
});
}
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
containerViews.pageContainer.pagecontainer('change', view.$el, $.extend({
role: 'page',
changeHash: false
}, options));
}
},
/**
* Pops view from the stack.
*
* @param [options] {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
* @returns {boolean} if succeeded
*/
popView: function (options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length > 1) {
// From view ref
var fromView = containerViews.views.pop();
// To view ref
var toView = containerViews.views[containerViews.views.length - 1];
fromView.$el.one('pagehide', function () {
// Detaching view from DOM
fromView.remove();
});
// Changing to view below current one
containerViews.pageContainer.pagecontainer('change', toView.$el, $.extend({
role: 'page',
reverse: true,
changeHash: false
}, options));
return true;
} else {
return false;
}
},
/**
* Pops views from a stack up to the number (N) supplied.
*
* @param N {number} The number of views to pop back
* @param [options] {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
popBackNViews: function (N, options) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length > 1) {
if (containerViews.views.length - N >= 1) {
// From view ref
var fromView = containerViews.views.pop();
// To view ref
var toView = containerViews.views[containerViews.views.length - N];
// Removed views
var removedViews = containerViews.views.splice((containerViews.views.length - N) + 1, containerViews.views.length - 1);
fromView.$el.one('pagehide', function () {
//Detach views in the middle
removedViews.forEach(function (item) {
item.remove();
}, this);
//Detach origin
fromView.remove();
});
containerViews.pageContainer.pagecontainer('change', toView.$el, $.extend({
role: 'page',
reverse: true,
changeHash: false
}, options));
} else {
console.log('Cannot pop first view or below, you can replace it instead!');
}
} else {
console.log('Cannot 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 (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
var toView = containerViews.views[0];
// Removed views
var removedViews = containerViews.views.splice(1, containerViews.views.length - 1);
fromView.$el.one('pagehide', function () {
removedViews.forEach(function (item) {
item.remove();
}, this);
});
// Changing to view below current one
containerViews.pageContainer.pagecontainer('change', toView.$el, $.extend({
role: 'page',
reverse: true,
changeHash: false
}, options));
} else {
console.log('Cannot pop first view, you can replace it instead!');
}
},
/**
*
* Replaces current view on the stack.
*
* @param view
* @param [options] {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
* @param [endCallback] Callback when the target view is shown
*/
replaceView: function (view, options, endCallback) {
var containerViews = this._getPageContainerViews(options);
if (containerViews.views.length >= 1) {
// From view ref
var fromView = containerViews.views.pop();
fromView.$el.one('pagehide', function () {
// Detaching view from DOM
fromView.remove();
});
// 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 (endCallback) {
view.$el.one('pageshow', function () {
endCallback();
});
}
// Changing page
containerViews.pageContainer.pagecontainer('change', view.$el, $.extend({
role: 'page',
changeHash: false
}, options));
}
},
/**
* Replaces all views on the stack.
*
* @param view
* @param [options] {*} Transition parameters can be passed like: transition, reverse, showLoadMsg or loadMsgDelay
*/
replaceAll: function (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
var removedViews = containerViews.views.splice(0, containerViews.views.length);
fromView.$el.one('pagehide', function () {
removedViews.forEach(function (item) {
item.remove();
}, 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
containerViews.pageContainer.pagecontainer('change', view.$el, $.extend({
role: 'page',
changeHash: false
}, options));
}
},
/**
* Get the page container view related with a `pageContainer`. It tries with:
* - `pageContainer` object from `options`.
* - $.mobile.pageContainer
* - `defaultPageContainer`
* - body
*
* @param options
* @returns {{pageContainer: Object, views: Array}}
* @private
*/
_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;
}
return false;
}, this);
if (!result) {
result = {
pageContainer: pageContainer,
views: []
};
this._containers.push(result);
}
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 = null,
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;
}
return false;
}, this);
return views;
}
}
});
});
}));