1 /// crmUi: Sundry UI helpers
2 (function (angular
, $, _
) {
3 angular
.module('crmUtil', []);
5 angular
.module('crmUtil').factory('crmApi', function($q
) {
6 return function(entity
, action
, params
, message
) {
7 // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
8 var deferred
= $q
.defer();
10 if (_
.isObject(entity
)) {
11 // eval content is locally generated.
13 p
= CRM
.api3(eval('('+angular
.toJson(entity
)+')'), message
);
15 // eval content is locally generated.
17 p
= CRM
.api3(entity
, action
, eval('('+angular
.toJson(params
)+')'), message
);
19 // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
23 if (result
.is_error
) {
24 deferred
.reject(result
);
26 deferred
.resolve(result
);
30 deferred
.reject(error
);
33 return deferred
.promise
;
37 angular
.module('crmUtil').factory('crmLegacy', function() {
41 // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
42 angular
.module('crmUtil').factory('crmLog', function(){
44 var write
= console
.log
;
47 for (var i
= 0; i
< level
; i
++) s
= s
+ ' ';
51 log: function(msg
, vars
) {
52 write(indent() + msg
, vars
);
54 wrap: function(label
, f
) {
57 crmLog
.log(label
+ ": start", arguments
);
60 r
= f
.apply(this, arguments
);
62 crmLog
.log(label
+ ": end");
72 angular
.module('crmUtil').factory('crmNavigator', ['$window', function($window
) {
74 redirect: function(path
) {
75 $window
.location
.href
= path
;
80 angular
.module('crmUtil').factory('crmNow', function($q
){
81 // FIXME: surely there's already some helper which can do this in one line?
82 // @return string "YYYY-MM-DD hh:mm:ss"
83 return function crmNow() {
84 var currentdate
= new Date();
85 var yyyy
= currentdate
.getFullYear();
86 var mm
= currentdate
.getMonth() + 1;
87 mm
= mm
< 10 ? '0' + mm
: mm
;
88 var dd
= currentdate
.getDate();
89 dd
= dd
< 10 ? '0' + dd
: dd
;
90 var hh
= currentdate
.getHours();
91 hh
= hh
< 10 ? '0' + hh
: hh
;
92 var min
= currentdate
.getMinutes();
93 min
= min
< 10 ? '0' + min
: min
;
94 var sec
= currentdate
.getSeconds();
95 sec
= sec
< 10 ? '0' + sec
: sec
;
96 return yyyy
+ "-" + mm
+ "-" + dd
+ " " + hh
+ ":" + min
+ ":" + sec
;
100 // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
101 // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
102 angular
.module('crmUtil').factory('crmStatus', function($q
){
103 return function(options
, aPromise
){
104 return CRM
.toAPromise($q
, CRM
.status(options
, CRM
.toJqPromise(aPromise
)));
108 // crmWatcher allows one to setup event listeners and temporarily suspend
112 // angular.controller(... function($scope, crmWatcher){
113 // var watcher = crmWatcher();
114 // function myfunc() {
115 // watcher.suspend('foo', function(){
119 // watcher.setup('foo', function(){
121 // $scope.$watch('foo', myfunc),
122 // $scope.$watch('bar', myfunc),
123 // $scope.$watch('whiz', otherfunc)
127 angular
.module('crmUtil').factory('crmWatcher', function(){
129 var unwatches
= {}, watchFactories
= {}, suspends
= {};
131 // Specify the list of watches
132 this.setup = function(name
, newWatchFactory
) {
133 watchFactories
[name
] = newWatchFactory
;
134 unwatches
[name
] = watchFactories
[name
]();
139 // Temporarily disable watches and run some logic
140 this.suspend = function(name
, f
) {
145 r
= f
.apply(this, []);
147 if (suspends
[name
] === 1) {
148 unwatches
[name
] = watchFactories
[name
]();
149 if (!angular
.isArray(unwatches
[name
])) {
150 unwatches
[name
] = [unwatches
[name
]];
158 this.teardown = function(name
) {
159 if (!unwatches
[name
]) return;
160 _
.each(unwatches
[name
], function(unwatch
){
163 delete unwatches
[name
];
170 })(angular
, CRM
.$, CRM
._
);