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
;
38 // var block = $scope.block = crmBlocker();
39 // $scope.save = function() { return block(crmApi('MyEntity','create',...)); };
40 // <button ng-click="save()" ng-disabled="block.check()">Do something</button>
41 angular
.module('crmUtil').factory('crmBlocker', function() {
44 var result = function(promise
) {
46 return promise
.finally(function() {
50 result
.check = function() {
57 angular
.module('crmUtil').factory('crmLegacy', function() {
61 // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
62 angular
.module('crmUtil').factory('crmLog', function(){
64 var write
= console
.log
;
67 for (var i
= 0; i
< level
; i
++) s
= s
+ ' ';
71 log: function(msg
, vars
) {
72 write(indent() + msg
, vars
);
74 wrap: function(label
, f
) {
77 crmLog
.log(label
+ ": start", arguments
);
80 r
= f
.apply(this, arguments
);
82 crmLog
.log(label
+ ": end");
92 angular
.module('crmUtil').factory('crmNavigator', ['$window', function($window
) {
94 redirect: function(path
) {
95 $window
.location
.href
= path
;
100 angular
.module('crmUtil').factory('crmNow', function($q
){
101 // FIXME: surely there's already some helper which can do this in one line?
102 // @return string "YYYY-MM-DD hh:mm:ss"
103 return function crmNow() {
104 var currentdate
= new Date();
105 var yyyy
= currentdate
.getFullYear();
106 var mm
= currentdate
.getMonth() + 1;
107 mm
= mm
< 10 ? '0' + mm
: mm
;
108 var dd
= currentdate
.getDate();
109 dd
= dd
< 10 ? '0' + dd
: dd
;
110 var hh
= currentdate
.getHours();
111 hh
= hh
< 10 ? '0' + hh
: hh
;
112 var min
= currentdate
.getMinutes();
113 min
= min
< 10 ? '0' + min
: min
;
114 var sec
= currentdate
.getSeconds();
115 sec
= sec
< 10 ? '0' + sec
: sec
;
116 return yyyy
+ "-" + mm
+ "-" + dd
+ " " + hh
+ ":" + min
+ ":" + sec
;
120 // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
121 // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
122 angular
.module('crmUtil').factory('crmStatus', function($q
){
123 return function(options
, aPromise
){
125 return CRM
.toAPromise($q
, CRM
.status(options
, CRM
.toJqPromise(aPromise
)));
127 return CRM
.toAPromise($q
, CRM
.status(options
));
132 // crmWatcher allows one to setup event listeners and temporarily suspend
136 // angular.controller(... function($scope, crmWatcher){
137 // var watcher = crmWatcher();
138 // function myfunc() {
139 // watcher.suspend('foo', function(){
143 // watcher.setup('foo', function(){
145 // $scope.$watch('foo', myfunc),
146 // $scope.$watch('bar', myfunc),
147 // $scope.$watch('whiz', otherfunc)
151 angular
.module('crmUtil').factory('crmWatcher', function(){
153 var unwatches
= {}, watchFactories
= {}, suspends
= {};
155 // Specify the list of watches
156 this.setup = function(name
, newWatchFactory
) {
157 watchFactories
[name
] = newWatchFactory
;
158 unwatches
[name
] = watchFactories
[name
]();
163 // Temporarily disable watches and run some logic
164 this.suspend = function(name
, f
) {
169 r
= f
.apply(this, []);
171 if (suspends
[name
] === 1) {
172 unwatches
[name
] = watchFactories
[name
]();
173 if (!angular
.isArray(unwatches
[name
])) {
174 unwatches
[name
] = [unwatches
[name
]];
182 this.teardown = function(name
) {
183 if (!unwatches
[name
]) return;
184 _
.each(unwatches
[name
], function(unwatch
){
187 delete unwatches
[name
];
194 })(angular
, CRM
.$, CRM
._
);