1 (function(angular
, $, _
) {
2 angular
.module('crmRouteBinder', CRM
.angRequires('crmRouteBinder'));
4 // While processing a change from the $watch()'d data, we set the "pendingUpdates" flag
5 // so that automated URL changes don't cause a reload.
6 var pendingUpdates
= null, activeTimer
= null, registered
= false, ignorable
= {};
8 function registerGlobalListener($injector
) {
9 if (registered
) return;
12 $injector
.get('$rootScope').$on('$routeUpdate', function () {
13 // Only reload if someone else -- like the user or an <a href> -- changed URL.
14 if (null === pendingUpdates
) {
15 $injector
.get('$route').reload();
22 watcher
: '$watchCollection',
23 decode
: angular
.fromJson
,
24 encode
: angular
.toJson
,
29 decode: function(v
) { return v
; },
30 encode: function(v
) { return v
; },
35 decode: function(v
) { return parseInt(v
); },
36 encode: function(v
) { return v
; },
41 decode: function(v
) { return v
=== '1'; },
42 encode: function(v
) { return v
? '1' : '0'; },
47 angular
.module('crmRouteBinder').config(function ($provide
) {
48 $provide
.decorator('$rootScope', function ($delegate
, $injector
, $parse
) {
49 Object
.getPrototypeOf($delegate
).$bindToRoute = function (options
) {
50 registerGlobalListener($injector
);
52 options
.format
= options
.format
|| 'json';
53 var fmt
= _
.clone(formats
[options
.format
]);
55 fmt
.watcher
= '$watch';
57 if (options
.default === undefined) {
58 options
.default = fmt
.default;
62 $route
= $injector
.get('$route'),
63 $timeout
= $injector
.get('$timeout');
65 if (options
.param
in $route
.current
.params
) {
66 value
= fmt
.decode($route
.current
.params
[options
.param
]);
69 value
= _
.cloneDeep(options
.default);
70 ignorable
[options
.param
] = fmt
.encode(options
.default);
72 $parse(options
.expr
).assign(_scope
, value
);
74 // Keep the URL bar up-to-date.
75 _scope
[fmt
.watcher
](options
.expr
, function (newValue
) {
76 var encValue
= fmt
.encode(newValue
);
77 if (!_
.isEqual(newValue
, options
.default) && $route
.current
.params
[options
.param
] === encValue
) {
81 pendingUpdates
= pendingUpdates
|| {};
82 pendingUpdates
[options
.param
] = encValue
;
83 var p
= angular
.extend({}, $route
.current
.params
, pendingUpdates
);
85 angular
.forEach(ignorable
, function(v
, k
) {
91 // Remove params from url if they equal their defaults
92 if (_
.isEqual(newValue
, options
.default)) {
93 p
[options
.param
] = null;
96 $route
.updateParams(p
);
98 if (activeTimer
) $timeout
.cancel(activeTimer
);
99 activeTimer
= $timeout(function () {
100 pendingUpdates
= null;
111 })(angular
, CRM
.$, CRM
._
);