1 /// crmUi: Sundry UI helpers
2 (function (angular
, $, _
) {
3 angular
.module('crmUtil', []);
5 // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
6 // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
7 angular
.module('crmUtil').factory('crmStatus', function($q
){
8 return function(options
, aPromise
){
9 return CRM
.toAPromise($q
, CRM
.status(options
, CRM
.toJqPromise(aPromise
)));
13 // crmWatcher allows one to setup event listeners and temporarily suspend
17 // angular.controller(... function($scope, crmWatcher){
18 // var watcher = crmWatcher();
19 // function myfunc() {
20 // watcher.suspend('foo', function(){
24 // watcher.setup('foo', function(){
26 // $scope.$watch('foo', myfunc),
27 // $scope.$watch('bar', myfunc),
28 // $scope.$watch('whiz', otherfunc)
32 angular
.module('crmUtil').factory('crmWatcher', function(){
34 var unwatches
= {}, watchFactories
= {}, suspends
= {};
36 // Specify the list of watches
37 this.setup = function(name
, newWatchFactory
) {
38 watchFactories
[name
] = newWatchFactory
;
39 unwatches
[name
] = watchFactories
[name
]();
44 // Temporarily disable watches and run some logic
45 this.suspend = function(name
, f
) {
50 r
= f
.apply(this, []);
52 if (suspends
[name
] === 1) {
53 unwatches
[name
] = watchFactories
[name
]();
54 if (!angular
.isArray(unwatches
[name
])) {
55 unwatches
[name
] = [unwatches
[name
]];
63 this.teardown = function(name
) {
64 if (!unwatches
[name
]) return;
65 _
.each(unwatches
[name
], function(unwatch
){
68 delete unwatches
[name
];
75 // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
76 angular
.module('crmUtil').factory('crmLog', function(){
78 var write
= console
.log
;
81 for (var i
= 0; i
< level
; i
++) s
= s
+ ' ';
85 log: function(msg
, vars
) {
86 write(indent() + msg
, vars
);
88 wrap: function(label
, f
) {
91 crmLog
.log(label
+ ": start", arguments
);
94 r
= f
.apply(this, arguments
);
96 crmLog
.log(label
+ ": end");
106 })(angular
, CRM
.$, CRM
._
);