-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathionic-multiple-views.js
83 lines (74 loc) · 2.27 KB
/
ionic-multiple-views.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
angular.module('ionicMultipleViews', [])
.directive('multipleViewsSupport', function ($state) {
function findView(name) {
var views = angular.element(document.body).find('ion-nav-view');
for (var i = 0; i < views.length; i++) {
var view = angular.element(views[i]);
if (view.attr('name') === name) {
return view;
}
}
return false;
}
return {
restrict: 'E',
scope: {
width: '@'
},
link: function (scope, element, attrs) {
var views = $state.current.views;
if (views) {
var names = Object.keys($state.current.views);
if (names.length > 1) {
var pane = element.parent();
var myView = pane.parent();
var currentViewIndex = names.indexOf(myView.attr('name'));
if (currentViewIndex > 0) {
var left = 0;
for (var i = 0; i < currentViewIndex; i++) {
var view = findView(names[i]);
var directiveScope = angular.element(view[0].querySelector('multiple-views-support')).scope().$$childTail;
left += parseInt(directiveScope.width.replace('%', ''), 10);
}
pane.css('left', left + '%');
}
pane.css('width', scope.width);
}
}
}
};
})
.factory('MultipleViewsManager', function ($rootScope, $state) {
var viewCallbacks = {};
var pendingCalls = [];
return {
updateView: function (viewName, params) {
if (!this.isActive()) {
throw 'Cannot use updateView in a single view layout. Please make sure that you are in a multiple views layout using ViewManager.isActive()';
}
var callback = viewCallbacks[viewName];
if (callback) {
callback(params);
} else {
pendingCalls.push({
viewName: viewName,
params: params
});
}
},
isActive: function () {
return $state.current.views && Object.keys($state.current.views).length > 1;
},
updated: function (name, callback) {
viewCallbacks[name] = callback;
for (var i = 0; i < pendingCalls.length; i++) {
var call = pendingCalls[i];
if (call.viewName === name) {
callback(call.params);
pendingCalls.splice(i, 1);
return;
}
}
}
};
});