Commit | Line | Data |
---|---|---|
685acae4 | 1 | /// crmUi: Sundry UI helpers |
2 | (function (angular, $, _) { | |
3 | ||
f8601d61 TO |
4 | var uidCount = 0; |
5 | ||
685acae4 | 6 | angular.module('crmUi', []) |
030dce01 | 7 | |
0cbed02c | 8 | // example <div crm-ui-accordion crm-title="ts('My Title')" crm-collapsed="true">...content...</div> |
e4f46be0 | 9 | // WISHLIST: crmCollapsed should support two-way/continuous binding |
030dce01 TO |
10 | .directive('crmUiAccordion', function() { |
11 | return { | |
12 | scope: { | |
207819ec | 13 | crmUiAccordion: '=' |
030dce01 | 14 | }, |
207819ec | 15 | template: '<div ng-class="cssClasses"><div class="crm-accordion-header">{{crmUiAccordion.title}} <a crm-ui-help="help" ng-if="help"></a></div><div class="crm-accordion-body" ng-transclude></div></div>', |
030dce01 | 16 | transclude: true, |
0cbed02c TO |
17 | link: function (scope, element, attrs) { |
18 | scope.cssClasses = { | |
207819ec CW |
19 | 'crm-accordion-wrapper': true, |
20 | collapsed: scope.crmUiAccordion.collapsed | |
0cbed02c | 21 | }; |
207819ec CW |
22 | scope.help = null; |
23 | scope.$watch('crmUiAccordion', function(crmUiAccordion) { | |
24 | if (crmUiAccordion && crmUiAccordion.help) { | |
25 | scope.help = crmUiAccordion.help.clone({}, { | |
26 | title: crmUiAccordion.title | |
27 | }); | |
28 | } | |
29 | }); | |
0cbed02c | 30 | } |
030dce01 TO |
31 | }; |
32 | }) | |
33 | ||
69a65adc TO |
34 | // Examples: |
35 | // crmUiAlert({text: 'My text', title: 'My title', type: 'error'}); | |
36 | // crmUiAlert({template: '<a ng-click="ok()">Hello</a>', scope: $scope.$new()}); | |
37 | // var h = crmUiAlert({templateUrl: '~/crmFoo/alert.html', scope: $scope.$new()}); | |
38 | // ... h.close(); ... | |
39 | .service('crmUiAlert', function($compile, $rootScope, $templateRequest, $q) { | |
40 | var count = 0; | |
41 | return function crmUiAlert(params) { | |
42 | var id = 'crmUiAlert_' + (++count); | |
43 | var tpl = null; | |
44 | if (params.templateUrl) { | |
45 | tpl = $templateRequest(params.templateUrl); | |
46 | } | |
47 | else if (params.template) { | |
48 | tpl = params.template; | |
49 | } | |
50 | if (tpl) { | |
51 | params.text = '<div id="' + id + '"></div>'; // temporary stub | |
52 | } | |
53 | var result = CRM.alert(params.text, params.title, params.type, params.options); | |
54 | if (tpl) { | |
55 | $q.when(tpl, function(html) { | |
56 | var scope = params.scope || $rootScope.$new(); | |
57 | var linker = $compile(html); | |
58 | $('#' + id).append($(linker(scope))); | |
59 | }); | |
60 | } | |
61 | return result; | |
62 | }; | |
63 | }) | |
64 | ||
a42344f4 CW |
65 | // Simple wrapper around $.crmDatepicker. |
66 | // example with no time input: <input crm-ui-datepicker="{time: false}" ng-model="myobj.datefield"/> | |
70e7470f | 67 | // example with custom date format: <input crm-ui-datepicker="{date: 'm/d/y'}" ng-model="myobj.datefield"/> |
a42344f4 | 68 | .directive('crmUiDatepicker', function () { |
510d515d TO |
69 | return { |
70 | restrict: 'AE', | |
2f31bc16 | 71 | require: 'ngModel', |
510d515d | 72 | scope: { |
a42344f4 | 73 | crmUiDatepicker: '=' |
510d515d | 74 | }, |
2f31bc16 | 75 | link: function (scope, element, attrs, ngModel) { |
ac5009c1 CW |
76 | ngModel.$render = function () { |
77 | element.val(ngModel.$viewValue).change(); | |
78 | }; | |
79 | ||
a42344f4 CW |
80 | element |
81 | .crmDatepicker(scope.crmUiDatepicker) | |
82 | .on('change', function() { | |
ac5009c1 | 83 | var requiredLength = 19; |
a42344f4 CW |
84 | if (scope.crmUiDatepicker && scope.crmUiDatepicker.time === false) { |
85 | requiredLength = 10; | |
86 | } | |
87 | if (scope.crmUiDatepicker && scope.crmUiDatepicker.date === false) { | |
ac5009c1 | 88 | requiredLength = 8; |
a42344f4 CW |
89 | } |
90 | ngModel.$setValidity('incompleteDateTime', !($(this).val().length && $(this).val().length !== requiredLength)); | |
510d515d | 91 | }); |
510d515d TO |
92 | } |
93 | }; | |
94 | }) | |
95 | ||
c4c69e7b TO |
96 | // Display debug information (if available) |
97 | // For richer DX, checkout Batarang/ng-inspector (Chrome/Safari), or AngScope/ng-inspect (Firefox). | |
98 | // example: <div crm-ui-debug="myobject" /> | |
99 | .directive('crmUiDebug', function ($location) { | |
100 | return { | |
101 | restrict: 'AE', | |
102 | scope: { | |
103 | crmUiDebug: '@' | |
104 | }, | |
105 | template: function() { | |
106 | var args = $location.search(); | |
916f65c0 | 107 | return (args && args.angularDebug) ? '<div crm-ui-accordion=\'{title: ts("Debug (%1)", {1: crmUiDebug}), collapsed: true}\'><pre>{{data|json}}</pre></div>' : ''; |
c4c69e7b TO |
108 | }, |
109 | link: function(scope, element, attrs) { | |
110 | var args = $location.search(); | |
111 | if (args && args.angularDebug) { | |
112 | scope.ts = CRM.ts(null); | |
113 | scope.$parent.$watch(attrs.crmUiDebug, function(data) { | |
114 | scope.data = data; | |
115 | }); | |
116 | } | |
117 | } | |
118 | }; | |
119 | }) | |
120 | ||
489c2674 | 121 | // Display a field/row in a field list |
c4256f35 CW |
122 | // example: <div crm-ui-field="{title: ts('My Field')}"> {{mydata}} </div> |
123 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" /> </div> | |
124 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" required /> </div> | |
563ba527 | 125 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field'), help: hs('help_field_name')}"> {{mydata}} </div> |
3cc9c048 | 126 | .directive('crmUiField', function() { |
489c2674 TO |
127 | // Note: When writing new templates, the "label" position is particular. See/patch "var label" below. |
128 | var templateUrls = { | |
ef5d18a1 TO |
129 | default: '~/crmUi/field.html', |
130 | checkbox: '~/crmUi/field-cb.html' | |
489c2674 TO |
131 | }; |
132 | ||
133 | return { | |
f8601d61 TO |
134 | require: '^crmUiIdScope', |
135 | restrict: 'EA', | |
489c2674 | 136 | scope: { |
d8356d0b | 137 | // {title, name, help, helpFile} |
c4256f35 | 138 | crmUiField: '=' |
489c2674 TO |
139 | }, |
140 | templateUrl: function(tElement, tAttrs){ | |
141 | var layout = tAttrs.crmLayout ? tAttrs.crmLayout : 'default'; | |
142 | return templateUrls[layout]; | |
143 | }, | |
144 | transclude: true, | |
f8601d61 | 145 | link: function (scope, element, attrs, crmUiIdCtrl) { |
489c2674 | 146 | $(element).addClass('crm-section'); |
563ba527 | 147 | scope.help = null; |
dfc2a868 TO |
148 | scope.$watch('crmUiField', function(crmUiField) { |
149 | if (crmUiField && crmUiField.help) { | |
150 | scope.help = crmUiField.help.clone({}, { | |
151 | title: crmUiField.title | |
152 | }); | |
153 | } | |
154 | }); | |
f8601d61 TO |
155 | } |
156 | }; | |
157 | }) | |
158 | ||
159 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> | |
160 | .directive('crmUiId', function () { | |
161 | return { | |
162 | require: '^crmUiIdScope', | |
163 | restrict: 'EA', | |
3cc9c048 TO |
164 | link: { |
165 | pre: function (scope, element, attrs, crmUiIdCtrl) { | |
166 | var id = crmUiIdCtrl.get(attrs.crmUiId); | |
167 | element.attr('id', id); | |
168 | } | |
f8601d61 TO |
169 | } |
170 | }; | |
171 | }) | |
489c2674 | 172 | |
dfc2a868 TO |
173 | // for example, see crmUiHelp |
174 | .service('crmUiHelp', function(){ | |
175 | // example: var h = new FieldHelp({id: 'foo'}); h.open(); | |
176 | function FieldHelp(options) { | |
177 | this.options = options; | |
178 | } | |
179 | angular.extend(FieldHelp.prototype, { | |
180 | get: function(n) { | |
181 | return this.options[n]; | |
182 | }, | |
183 | open: function open() { | |
dfc2a868 TO |
184 | CRM.help(this.options.title, {id: this.options.id, file: this.options.file}); |
185 | }, | |
186 | clone: function clone(options, defaults) { | |
dfc2a868 TO |
187 | return new FieldHelp(angular.extend({}, defaults, this.options, options)); |
188 | } | |
189 | }); | |
190 | ||
563ba527 | 191 | // example: var hs = crmUiHelp({file: 'CRM/Foo/Bar'}); |
dfc2a868 | 192 | return function(defaults){ |
563ba527 TO |
193 | // example: hs('myfield') |
194 | // example: hs({id: 'myfield', title: 'Foo Bar', file: 'Whiz/Bang'}) | |
dfc2a868 TO |
195 | return function(options) { |
196 | if (_.isString(options)) { | |
197 | options = {id: options}; | |
198 | } | |
199 | return new FieldHelp(angular.extend({}, defaults, options)); | |
6dc9d4ca TO |
200 | }; |
201 | }; | |
dfc2a868 TO |
202 | }) |
203 | ||
204 | // Display a help icon | |
9a593827 | 205 | // Example: Use a default *.hlp file |
563ba527 TO |
206 | // scope.hs = crmUiHelp({file: 'Path/To/Help/File'}); |
207 | // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field'})"> | |
9a593827 | 208 | // Example: Use an explicit *.hlp file |
563ba527 | 209 | // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field', file:'CRM/Foo/Bar'})"> |
a517b78a CW |
210 | .directive('crmUiHelp', function() { |
211 | return { | |
212 | restrict: 'EA', | |
563ba527 TO |
213 | link: function(scope, element, attrs) { |
214 | setTimeout(function() { | |
215 | var crmUiHelp = scope.$eval(attrs.crmUiHelp); | |
dfc2a868 TO |
216 | var title = crmUiHelp && crmUiHelp.get('title') ? ts('%1 Help', {1: crmUiHelp.get('title')}) : ts('Help'); |
217 | element.attr('title', title); | |
563ba527 | 218 | }, 50); |
dfc2a868 | 219 | |
a517b78a CW |
220 | element |
221 | .addClass('helpicon') | |
a517b78a CW |
222 | .attr('href', '#') |
223 | .on('click', function(e) { | |
224 | e.preventDefault(); | |
563ba527 | 225 | scope.$eval(attrs.crmUiHelp).open(); |
a517b78a CW |
226 | }); |
227 | } | |
228 | }; | |
229 | }) | |
230 | ||
f8601d61 TO |
231 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> |
232 | .directive('crmUiFor', function ($parse, $timeout) { | |
233 | return { | |
234 | require: '^crmUiIdScope', | |
235 | restrict: 'EA', | |
236 | template: '<span ng-class="cssClasses"><span ng-transclude/><span crm-ui-visible="crmIsRequired" class="crm-marker" title="This field is required.">*</span></span>', | |
237 | transclude: true, | |
238 | link: function (scope, element, attrs, crmUiIdCtrl) { | |
239 | scope.crmIsRequired = false; | |
240 | scope.cssClasses = {}; | |
489c2674 | 241 | |
f8601d61 | 242 | if (!attrs.crmUiFor) return; |
489c2674 | 243 | |
f8601d61 TO |
244 | var id = crmUiIdCtrl.get(attrs.crmUiFor); |
245 | element.attr('for', id); | |
246 | var ngModel = null; | |
489c2674 | 247 | |
f8601d61 TO |
248 | var updateCss = function () { |
249 | scope.cssClasses['crm-error'] = !ngModel.$valid && !ngModel.$pristine; | |
250 | }; | |
489c2674 | 251 | |
f8601d61 TO |
252 | // Note: if target element is dynamically generated (eg via ngInclude), then it may not be available |
253 | // immediately for initialization. Use retries/retryDelay to initialize such elements. | |
254 | var init = function (retries, retryDelay) { | |
255 | var input = $('#' + id); | |
4109d03e | 256 | if (input.length === 0) { |
f8601d61 TO |
257 | if (retries) { |
258 | $timeout(function(){ | |
259 | init(retries-1, retryDelay); | |
260 | }, retryDelay); | |
261 | } | |
262 | return; | |
263 | } | |
489c2674 | 264 | |
f8601d61 TO |
265 | var tgtScope = scope;//.$parent; |
266 | if (attrs.crmDepth) { | |
267 | for (var i = attrs.crmDepth; i > 0; i--) { | |
268 | tgtScope = tgtScope.$parent; | |
269 | } | |
270 | } | |
489c2674 | 271 | |
f8601d61 TO |
272 | if (input.attr('ng-required')) { |
273 | scope.crmIsRequired = scope.$parent.$eval(input.attr('ng-required')); | |
274 | scope.$parent.$watch(input.attr('ng-required'), function (isRequired) { | |
275 | scope.crmIsRequired = isRequired; | |
276 | }); | |
277 | } | |
278 | else { | |
f2bad133 | 279 | scope.crmIsRequired = input.prop('required'); |
f8601d61 | 280 | } |
489c2674 | 281 | |
f8601d61 TO |
282 | ngModel = $parse(attrs.crmUiFor)(tgtScope); |
283 | if (ngModel) { | |
284 | ngModel.$viewChangeListeners.push(updateCss); | |
285 | } | |
286 | }; | |
489c2674 | 287 | |
f8601d61 TO |
288 | $timeout(function(){ |
289 | init(3, 100); | |
489c2674 TO |
290 | }); |
291 | } | |
292 | }; | |
293 | }) | |
294 | ||
d376f33e | 295 | // Define a scope in which a name like "subform.foo" maps to a unique ID. |
f8601d61 TO |
296 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> |
297 | .directive('crmUiIdScope', function () { | |
298 | return { | |
299 | restrict: 'EA', | |
300 | scope: {}, | |
301 | controllerAs: 'crmUiIdCtrl', | |
302 | controller: function($scope) { | |
303 | var ids = {}; | |
304 | this.get = function(name) { | |
305 | if (!ids[name]) { | |
306 | ids[name] = "crmUiId_" + (++uidCount); | |
307 | } | |
308 | return ids[name]; | |
f2bad133 | 309 | }; |
f8601d61 TO |
310 | }, |
311 | link: function (scope, element, attrs) {} | |
312 | }; | |
313 | }) | |
314 | ||
d376f33e | 315 | // Display an HTML blurb inside an IFRAME. |
107c5cc7 | 316 | // example: <iframe crm-ui-iframe="getHtmlContent()"></iframe> |
79cf1116 | 317 | // example: <iframe crm-ui-iframe crm-ui-iframe-src="getUrl()"></iframe> |
107c5cc7 TO |
318 | .directive('crmUiIframe', function ($parse) { |
319 | return { | |
320 | scope: { | |
79cf1116 TO |
321 | crmUiIframeSrc: '@', // expression which evaluates to a URL |
322 | crmUiIframe: '@' // expression which evaluates to HTML content | |
107c5cc7 TO |
323 | }, |
324 | link: function (scope, elm, attrs) { | |
325 | var iframe = $(elm)[0]; | |
326 | iframe.setAttribute('width', '100%'); | |
327 | iframe.setAttribute('frameborder', '0'); | |
328 | ||
329 | var refresh = function () { | |
79cf1116 TO |
330 | if (attrs.crmUiIframeSrc) { |
331 | iframe.setAttribute('src', scope.$parent.$eval(attrs.crmUiIframeSrc)); | |
107c5cc7 | 332 | } |
79cf1116 TO |
333 | else { |
334 | var iframeHtml = scope.$parent.$eval(attrs.crmUiIframe); | |
107c5cc7 | 335 | |
79cf1116 TO |
336 | var doc = iframe.document; |
337 | if (iframe.contentDocument) { | |
338 | doc = iframe.contentDocument; | |
339 | } | |
340 | else if (iframe.contentWindow) { | |
341 | doc = iframe.contentWindow.document; | |
342 | } | |
343 | ||
344 | doc.open(); | |
345 | doc.writeln(iframeHtml); | |
346 | doc.close(); | |
347 | } | |
f2bad133 | 348 | }; |
107c5cc7 | 349 | |
02cd9764 CW |
350 | // If the iframe is in a dialog, respond to resize events |
351 | $(elm).parent().on('dialogresize dialogopen', function(e, ui) { | |
352 | $(this).css({padding: '0', margin: '0', overflow: 'hidden'}); | |
353 | iframe.setAttribute('height', '' + $(this).innerHeight() + 'px'); | |
354 | }); | |
355 | ||
107c5cc7 | 356 | scope.$parent.$watch(attrs.crmUiIframe, refresh); |
107c5cc7 TO |
357 | } |
358 | }; | |
359 | }) | |
360 | ||
f8f85764 TO |
361 | // Example: |
362 | // <a ng-click="$broadcast('my-insert-target', 'some new text')>Insert</a> | |
363 | // <textarea crm-ui-insert-rx='my-insert-target'></textarea> | |
f8f85764 TO |
364 | .directive('crmUiInsertRx', function() { |
365 | return { | |
366 | link: function(scope, element, attrs) { | |
367 | scope.$on(attrs.crmUiInsertRx, function(e, tokenName) { | |
c62731c3 TC |
368 | CRM.wysiwyg.insert(element, tokenName); |
369 | $(element).select2('close').select2('val', ''); | |
f91b1c0c | 370 | CRM.wysiwyg.focus(element); |
f8f85764 TO |
371 | }); |
372 | } | |
373 | }; | |
374 | }) | |
375 | ||
d376f33e | 376 | // Define a rich text editor. |
3cc9c048 TO |
377 | // example: <textarea crm-ui-id="myForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html"></textarea> |
378 | .directive('crmUiRichtext', function ($timeout) { | |
38737af8 TO |
379 | return { |
380 | require: '?ngModel', | |
381 | link: function (scope, elm, attr, ngModel) { | |
65d3e04c | 382 | |
c62731c3 | 383 | var editor = CRM.wysiwyg.create(elm); |
38737af8 TO |
384 | if (!ngModel) { |
385 | return; | |
386 | } | |
387 | ||
07996588 | 388 | if (attr.ngBlur) { |
f91b1c0c CW |
389 | $(elm).on('blur', function() { |
390 | $timeout(function() { | |
07996588 | 391 | scope.$eval(attr.ngBlur); |
d03e0d13 | 392 | }); |
07996588 TO |
393 | }); |
394 | } | |
395 | ||
f91b1c0c | 396 | ngModel.$render = function(value) { |
c62731c3 | 397 | CRM.wysiwyg.setVal(elm, ngModel.$viewValue); |
38737af8 TO |
398 | }; |
399 | } | |
400 | }; | |
401 | }) | |
402 | ||
d376f33e | 403 | // Display a lock icon (based on a boolean). |
685acae4 | 404 | // example: <a crm-ui-lock binding="mymodel.boolfield"></a> |
405 | // example: <a crm-ui-lock | |
406 | // binding="mymodel.boolfield" | |
407 | // title-locked="ts('Boolfield is locked')" | |
408 | // title-unlocked="ts('Boolfield is unlocked')"></a> | |
409 | .directive('crmUiLock', function ($parse, $rootScope) { | |
410 | var defaultVal = function (defaultValue) { | |
411 | var f = function (scope) { | |
412 | return defaultValue; | |
f2bad133 | 413 | }; |
685acae4 | 414 | f.assign = function (scope, value) { |
415 | // ignore changes | |
f2bad133 | 416 | }; |
685acae4 | 417 | return f; |
418 | }; | |
419 | ||
420 | // like $parse, but accepts a defaultValue in case expr is undefined | |
421 | var parse = function (expr, defaultValue) { | |
422 | return expr ? $parse(expr) : defaultVal(defaultValue); | |
423 | }; | |
424 | ||
425 | return { | |
426 | template: '', | |
427 | link: function (scope, element, attrs) { | |
f2bad133 TO |
428 | var binding = parse(attrs.binding, true); |
429 | var titleLocked = parse(attrs.titleLocked, ts('Locked')); | |
430 | var titleUnlocked = parse(attrs.titleUnlocked, ts('Unlocked')); | |
685acae4 | 431 | |
77ec5a8d | 432 | $(element).addClass('crm-i lock-button'); |
685acae4 | 433 | var refresh = function () { |
434 | var locked = binding(scope); | |
435 | if (locked) { | |
436 | $(element) | |
77ec5a8d AH |
437 | .removeClass('fa-unlock') |
438 | .addClass('fa-lock') | |
685acae4 | 439 | .prop('title', titleLocked(scope)) |
440 | ; | |
441 | } | |
442 | else { | |
443 | $(element) | |
77ec5a8d AH |
444 | .removeClass('fa-lock') |
445 | .addClass('fa-unlock') | |
685acae4 | 446 | .prop('title', titleUnlocked(scope)) |
447 | ; | |
448 | } | |
449 | }; | |
450 | ||
451 | $(element).click(function () { | |
452 | binding.assign(scope, !binding(scope)); | |
453 | //scope.$digest(); | |
454 | $rootScope.$digest(); | |
455 | }); | |
456 | ||
457 | scope.$watch(attrs.binding, refresh); | |
458 | scope.$watch(attrs.titleLocked, refresh); | |
459 | scope.$watch(attrs.titleUnlocked, refresh); | |
460 | ||
461 | refresh(); | |
462 | } | |
463 | }; | |
464 | }) | |
030dce01 | 465 | |
b21c9cdc TO |
466 | // CrmUiOrderCtrl is a controller class which manages sort orderings. |
467 | // Ex: | |
468 | // JS: $scope.myOrder = new CrmUiOrderCtrl(['+field1', '-field2]); | |
469 | // $scope.myOrder.toggle('field1'); | |
470 | // $scope.myOrder.setDir('field2', ''); | |
471 | // HTML: <tr ng-repeat="... | order:myOrder.get()">...</tr> | |
472 | .service('CrmUiOrderCtrl', function(){ | |
473 | // | |
474 | function CrmUiOrderCtrl(defaults){ | |
475 | this.values = defaults; | |
476 | } | |
477 | angular.extend(CrmUiOrderCtrl.prototype, { | |
478 | get: function get() { | |
479 | return this.values; | |
480 | }, | |
481 | getDir: function getDir(name) { | |
482 | if (this.values.indexOf(name) >= 0 || this.values.indexOf('+' + name) >= 0) { | |
483 | return '+'; | |
484 | } | |
485 | if (this.values.indexOf('-' + name) >= 0) { | |
486 | return '-'; | |
487 | } | |
488 | return ''; | |
489 | }, | |
490 | // @return bool TRUE if something is removed | |
491 | remove: function remove(name) { | |
492 | var idx = this.values.indexOf(name); | |
493 | if (idx >= 0) { | |
494 | this.values.splice(idx, 1); | |
495 | return true; | |
496 | } | |
497 | else { | |
498 | return false; | |
499 | } | |
500 | }, | |
501 | setDir: function setDir(name, dir) { | |
502 | return this.toggle(name, dir); | |
503 | }, | |
504 | // Toggle sort order on a field. | |
505 | // To set a specific order, pass optional parameter 'next' ('+', '-', or ''). | |
506 | toggle: function toggle(name, next) { | |
507 | if (!next && next !== '') { | |
508 | next = '+'; | |
509 | if (this.remove(name) || this.remove('+' + name)) { | |
510 | next = '-'; | |
511 | } | |
512 | if (this.remove('-' + name)) { | |
513 | next = ''; | |
514 | } | |
515 | } | |
516 | ||
517 | if (next == '+') { | |
518 | this.values.unshift('+' + name); | |
519 | } | |
520 | else if (next == '-') { | |
521 | this.values.unshift('-' + name); | |
522 | } | |
523 | } | |
524 | }); | |
525 | return CrmUiOrderCtrl; | |
526 | }) | |
527 | ||
528 | // Define a controller which manages sort order. You may interact with the controller | |
529 | // directly ("myOrder.toggle('fieldname')") order using the helper, crm-ui-order-by. | |
530 | // example: | |
531 | // <span crm-ui-order="{var: 'myOrder', defaults: {'-myField'}}"></span> | |
532 | // <th><a crm-ui-order-by="[myOrder,'myField']">My Field</a></th> | |
533 | // <tr ng-repeat="... | order:myOrder.get()">...</tr> | |
534 | // <button ng-click="myOrder.toggle('myField')"> | |
535 | .directive('crmUiOrder', function(CrmUiOrderCtrl) { | |
536 | return { | |
537 | link: function(scope, element, attrs){ | |
538 | var options = angular.extend({var: 'crmUiOrderBy'}, scope.$eval(attrs.crmUiOrder)); | |
539 | scope[options.var] = new CrmUiOrderCtrl(options.defaults); | |
540 | } | |
541 | }; | |
542 | }) | |
543 | ||
544 | // For usage, see crmUiOrder (above) | |
545 | .directive('crmUiOrderBy', function() { | |
546 | return { | |
547 | link: function(scope, element, attrs) { | |
548 | function updateClass(crmUiOrderCtrl, name) { | |
549 | var dir = crmUiOrderCtrl.getDir(name); | |
550 | element | |
551 | .toggleClass('sorting_asc', dir === '+') | |
552 | .toggleClass('sorting_desc', dir === '-') | |
553 | .toggleClass('sorting', dir === ''); | |
554 | } | |
555 | ||
556 | element.on('click', function(e){ | |
557 | var tgt = scope.$eval(attrs.crmUiOrderBy); | |
558 | tgt[0].toggle(tgt[1]); | |
559 | updateClass(tgt[0], tgt[1]); | |
560 | e.preventDefault(); | |
561 | scope.$digest(); | |
562 | }); | |
563 | ||
564 | var tgt = scope.$eval(attrs.crmUiOrderBy); | |
565 | updateClass(tgt[0], tgt[1]); | |
566 | } | |
567 | }; | |
568 | }) | |
569 | ||
d376f33e | 570 | // Display a fancy SELECT (based on select2). |
ebfe3efb TO |
571 | // usage: <select crm-ui-select="{placeholder:'Something',allowClear:true,...}" ng-model="myobj.field"><option...></select> |
572 | .directive('crmUiSelect', function ($parse, $timeout) { | |
8c632f2b | 573 | return { |
ebfe3efb | 574 | require: '?ngModel', |
8c632f2b | 575 | scope: { |
8674b5ac | 576 | crmUiSelect: '=' |
8c632f2b | 577 | }, |
ebfe3efb | 578 | link: function (scope, element, attrs, ngModel) { |
8c632f2b TO |
579 | // In cases where UI initiates update, there may be an extra |
580 | // call to refreshUI, but it doesn't create a cycle. | |
581 | ||
c81696a6 CW |
582 | if (ngModel) { |
583 | ngModel.$render = function () { | |
584 | $timeout(function () { | |
585 | // ex: msg_template_id adds new item then selects it; use $timeout to ensure that | |
586 | // new item is added before selection is made | |
587 | element.select2('val', ngModel.$viewValue); | |
588 | }); | |
589 | }; | |
590 | } | |
8c632f2b | 591 | function refreshModel() { |
50e25421 | 592 | var oldValue = ngModel.$viewValue, newValue = element.select2('val'); |
8c632f2b | 593 | if (oldValue != newValue) { |
ebfe3efb TO |
594 | scope.$parent.$apply(function () { |
595 | ngModel.$setViewValue(newValue); | |
8c632f2b | 596 | }); |
8c632f2b TO |
597 | } |
598 | } | |
ebfe3efb | 599 | |
8c632f2b TO |
600 | function init() { |
601 | // TODO watch select2-options | |
8674b5ac | 602 | element.select2(scope.crmUiSelect || {}); |
c81696a6 CW |
603 | if (ngModel) { |
604 | element.on('change', refreshModel); | |
605 | $timeout(ngModel.$render); | |
606 | } | |
8099bfee CW |
607 | } |
608 | ||
609 | init(); | |
610 | } | |
611 | }; | |
612 | }) | |
613 | ||
614 | // Render a crmEntityRef widget | |
615 | // usage: <input crm-entityref="{entity: 'Contact', select: {allowClear:true}}" ng-model="myobj.field" /> | |
616 | .directive('crmEntityref', function ($parse, $timeout) { | |
617 | return { | |
618 | require: '?ngModel', | |
619 | scope: { | |
620 | crmEntityref: '=' | |
621 | }, | |
622 | link: function (scope, element, attrs, ngModel) { | |
623 | // In cases where UI initiates update, there may be an extra | |
624 | // call to refreshUI, but it doesn't create a cycle. | |
625 | ||
626 | ngModel.$render = function () { | |
627 | $timeout(function () { | |
628 | // ex: msg_template_id adds new item then selects it; use $timeout to ensure that | |
629 | // new item is added before selection is made | |
630 | element.select2('val', ngModel.$viewValue); | |
631 | }); | |
632 | }; | |
633 | function refreshModel() { | |
634 | var oldValue = ngModel.$viewValue, newValue = element.select2('val'); | |
635 | if (oldValue != newValue) { | |
636 | scope.$parent.$apply(function () { | |
637 | ngModel.$setViewValue(newValue); | |
638 | }); | |
639 | } | |
640 | } | |
641 | ||
642 | function init() { | |
8099bfee CW |
643 | // TODO can we infer "entity" from model? |
644 | element.crmEntityRef(scope.crmEntityref || {}); | |
645 | element.on('change', refreshModel); | |
646 | $timeout(ngModel.$render); | |
8c632f2b | 647 | } |
ebfe3efb | 648 | |
8c632f2b TO |
649 | init(); |
650 | } | |
651 | }; | |
652 | }) | |
653 | ||
030dce01 | 654 | // example <div crm-ui-tab crm-title="ts('My Title')">...content...</div> |
5f3568fd | 655 | // WISHLIST: use a full Angular component instead of an incomplete jQuery wrapper |
030dce01 TO |
656 | .directive('crmUiTab', function($parse) { |
657 | return { | |
5f3568fd TO |
658 | require: '^crmUiTabSet', |
659 | restrict: 'EA', | |
030dce01 | 660 | scope: { |
5f3568fd TO |
661 | crmTitle: '@', |
662 | id: '@' | |
030dce01 | 663 | }, |
5f3568fd | 664 | template: '<div ng-transclude></div>', |
030dce01 | 665 | transclude: true, |
5f3568fd TO |
666 | link: function (scope, element, attrs, crmUiTabSetCtrl) { |
667 | crmUiTabSetCtrl.add(scope); | |
668 | } | |
030dce01 TO |
669 | }; |
670 | }) | |
671 | ||
672 | // example: <div crm-ui-tab-set><div crm-ui-tab crm-title="Tab 1">...</div><div crm-ui-tab crm-title="Tab 2">...</div></div> | |
673 | .directive('crmUiTabSet', function() { | |
674 | return { | |
5f3568fd TO |
675 | restrict: 'EA', |
676 | scope: { | |
677 | crmUiTabSet: '@' | |
678 | }, | |
ef5d18a1 | 679 | templateUrl: '~/crmUi/tabset.html', |
030dce01 | 680 | transclude: true, |
5f3568fd TO |
681 | controllerAs: 'crmUiTabSetCtrl', |
682 | controller: function($scope, $parse) { | |
683 | var tabs = $scope.tabs = []; // array<$scope> | |
684 | this.add = function(tab) { | |
685 | if (!tab.id) throw "Tab is missing 'id'"; | |
686 | tabs.push(tab); | |
687 | }; | |
688 | }, | |
030dce01 TO |
689 | link: function (scope, element, attrs) {} |
690 | }; | |
691 | }) | |
692 | ||
d376f33e | 693 | // Generic, field-independent form validator. |
3afb86ef TO |
694 | // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" /> |
695 | // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" crm-ui-validate-name="myError" /> | |
3afb86ef TO |
696 | .directive('crmUiValidate', function() { |
697 | return { | |
698 | restrict: 'EA', | |
699 | require: 'ngModel', | |
700 | link: function(scope, element, attrs, ngModel) { | |
701 | var validationKey = attrs.crmUiValidateName ? attrs.crmUiValidateName : 'crmUiValidate'; | |
702 | scope.$watch(attrs.crmUiValidate, function(newValue){ | |
703 | ngModel.$setValidity(validationKey, !!newValue); | |
704 | }); | |
705 | } | |
706 | }; | |
707 | }) | |
708 | ||
e84a11d8 TO |
709 | // like ng-show, but hides/displays elements using "visibility" which maintains positioning |
710 | // example <div crm-ui-visible="false">...content...</div> | |
711 | .directive('crmUiVisible', function($parse) { | |
712 | return { | |
713 | restrict: 'EA', | |
714 | scope: { | |
715 | crmUiVisible: '@' | |
716 | }, | |
717 | link: function (scope, element, attrs) { | |
718 | var model = $parse(attrs.crmUiVisible); | |
719 | function updatecChildren() { | |
720 | element.css('visibility', model(scope.$parent) ? 'inherit' : 'hidden'); | |
721 | } | |
722 | updatecChildren(); | |
723 | scope.$parent.$watch(attrs.crmUiVisible, updatecChildren); | |
724 | } | |
725 | }; | |
726 | }) | |
727 | ||
6717e4b9 TO |
728 | // example: <div crm-ui-wizard="myWizardCtrl"><div crm-ui-wizard-step crm-title="ts('Step 1')">...</div><div crm-ui-wizard-step crm-title="ts('Step 2')">...</div></div> |
729 | // Note: "myWizardCtrl" has various actions/properties like next() and $first(). | |
730 | // WISHLIST: Allow each step to determine if it is "complete" / "valid" / "selectable" | |
731 | // WISHLIST: Allow each step to enable/disable (show/hide) itself | |
030dce01 TO |
732 | .directive('crmUiWizard', function() { |
733 | return { | |
6717e4b9 TO |
734 | restrict: 'EA', |
735 | scope: { | |
736 | crmUiWizard: '@' | |
737 | }, | |
ef5d18a1 | 738 | templateUrl: '~/crmUi/wizard.html', |
030dce01 | 739 | transclude: true, |
6717e4b9 TO |
740 | controllerAs: 'crmUiWizardCtrl', |
741 | controller: function($scope, $parse) { | |
742 | var steps = $scope.steps = []; // array<$scope> | |
743 | var crmUiWizardCtrl = this; | |
744 | var maxVisited = 0; | |
745 | var selectedIndex = null; | |
746 | ||
747 | var findIndex = function() { | |
748 | var found = null; | |
749 | angular.forEach(steps, function(step, stepKey) { | |
750 | if (step.selected) found = stepKey; | |
751 | }); | |
752 | return found; | |
753 | }; | |
754 | ||
755 | /// @return int the index of the current step | |
756 | this.$index = function() { return selectedIndex; }; | |
757 | /// @return bool whether the currentstep is first | |
758 | this.$first = function() { return this.$index() === 0; }; | |
759 | /// @return bool whether the current step is last | |
760 | this.$last = function() { return this.$index() === steps.length -1; }; | |
f2bad133 | 761 | this.$maxVisit = function() { return maxVisited; }; |
3f0da451 | 762 | this.$validStep = function() { |
f8c05c87 | 763 | return steps[selectedIndex] && steps[selectedIndex].isStepValid(); |
3f0da451 | 764 | }; |
6717e4b9 TO |
765 | this.iconFor = function(index) { |
766 | if (index < this.$index()) return '√'; | |
767 | if (index === this.$index()) return '»'; | |
768 | return ' '; | |
f2bad133 | 769 | }; |
6717e4b9 TO |
770 | this.isSelectable = function(step) { |
771 | if (step.selected) return false; | |
772 | var result = false; | |
773 | angular.forEach(steps, function(otherStep, otherKey) { | |
774 | if (step === otherStep && otherKey <= maxVisited) result = true; | |
775 | }); | |
776 | return result; | |
777 | }; | |
778 | ||
779 | /*** @param Object step the $scope of the step */ | |
780 | this.select = function(step) { | |
781 | angular.forEach(steps, function(otherStep, otherKey) { | |
782 | otherStep.selected = (otherStep === step); | |
783 | if (otherStep === step && maxVisited < otherKey) maxVisited = otherKey; | |
784 | }); | |
785 | selectedIndex = findIndex(); | |
786 | }; | |
787 | /*** @param Object step the $scope of the step */ | |
788 | this.add = function(step) { | |
789 | if (steps.length === 0) { | |
790 | step.selected = true; | |
791 | selectedIndex = 0; | |
792 | } | |
793 | steps.push(step); | |
8688b463 TO |
794 | steps.sort(function(a,b){ |
795 | return a.crmUiWizardStep - b.crmUiWizardStep; | |
796 | }); | |
797 | selectedIndex = findIndex(); | |
798 | }; | |
799 | this.remove = function(step) { | |
800 | var key = null; | |
801 | angular.forEach(steps, function(otherStep, otherKey) { | |
802 | if (otherStep === step) key = otherKey; | |
803 | }); | |
4109d03e | 804 | if (key !== null) { |
8688b463 TO |
805 | steps.splice(key, 1); |
806 | } | |
6717e4b9 TO |
807 | }; |
808 | this.goto = function(index) { | |
809 | if (index < 0) index = 0; | |
810 | if (index >= steps.length) index = steps.length-1; | |
811 | this.select(steps[index]); | |
812 | }; | |
813 | this.previous = function() { this.goto(this.$index()-1); }; | |
814 | this.next = function() { this.goto(this.$index()+1); }; | |
815 | if ($scope.crmUiWizard) { | |
f2bad133 | 816 | $parse($scope.crmUiWizard).assign($scope.$parent, this); |
6717e4b9 TO |
817 | } |
818 | }, | |
c4991710 TO |
819 | link: function (scope, element, attrs) { |
820 | scope.ts = CRM.ts(null); | |
821 | } | |
030dce01 TO |
822 | }; |
823 | }) | |
824 | ||
6717e4b9 TO |
825 | // Use this to add extra markup to wizard |
826 | .directive('crmUiWizardButtons', function() { | |
030dce01 | 827 | return { |
6717e4b9 TO |
828 | require: '^crmUiWizard', |
829 | restrict: 'EA', | |
830 | scope: {}, | |
831 | template: '<span ng-transclude></span>', | |
030dce01 | 832 | transclude: true, |
6717e4b9 TO |
833 | link: function (scope, element, attrs, crmUiWizardCtrl) { |
834 | var realButtonsEl = $(element).closest('.crm-wizard').find('.crm-wizard-buttons'); | |
835 | $(element).appendTo(realButtonsEl); | |
836 | } | |
030dce01 TO |
837 | }; |
838 | }) | |
839 | ||
77ec5a8d AH |
840 | // Example for Font Awesome: <button crm-icon="fa-check">Save</button> |
841 | // Example for jQuery UI (deprecated): <button crm-icon="check">Save</button> | |
39f4f54f CW |
842 | .directive('crmIcon', function() { |
843 | return { | |
844 | restrict: 'EA', | |
845 | scope: {}, | |
846 | link: function (scope, element, attrs) { | |
aee84611 AH |
847 | if (attrs.crmIcon.substring(0,3) == 'fa-') { |
848 | $(element).prepend('<i class="crm-i ' + attrs.crmIcon + '"></i> '); | |
849 | } | |
850 | else { | |
851 | $(element).prepend('<span class="icon ui-icon-' + attrs.crmIcon + '"></span> '); | |
852 | } | |
39f4f54f CW |
853 | if ($(element).is('button')) { |
854 | $(element).addClass('crm-button'); | |
855 | } | |
856 | } | |
857 | }; | |
858 | }) | |
859 | ||
3f0da451 | 860 | // example: <div crm-ui-wizard-step crm-title="ts('My Title')" ng-form="mySubForm">...content...</div> |
8688b463 TO |
861 | // If there are any conditional steps, then be sure to set a weight explicitly on *all* steps to maintain ordering. |
862 | // example: <div crm-ui-wizard-step="100" crm-title="..." ng-if="...">...content...</div> | |
030dce01 | 863 | .directive('crmUiWizardStep', function() { |
8688b463 | 864 | var nextWeight = 1; |
030dce01 | 865 | return { |
3f0da451 | 866 | require: ['^crmUiWizard', 'form'], |
6717e4b9 | 867 | restrict: 'EA', |
030dce01 | 868 | scope: { |
8688b463 TO |
869 | crmTitle: '@', // expression, evaluates to a printable string |
870 | crmUiWizardStep: '@' // int, a weight which determines the ordering of the steps | |
030dce01 | 871 | }, |
6717e4b9 | 872 | template: '<div class="crm-wizard-step" ng-show="selected" ng-transclude/></div>', |
030dce01 | 873 | transclude: true, |
3f0da451 TO |
874 | link: function (scope, element, attrs, ctrls) { |
875 | var crmUiWizardCtrl = ctrls[0], form = ctrls[1]; | |
8688b463 TO |
876 | if (scope.crmUiWizardStep) { |
877 | scope.crmUiWizardStep = parseInt(scope.crmUiWizardStep); | |
878 | } else { | |
879 | scope.crmUiWizardStep = nextWeight++; | |
880 | } | |
3f0da451 TO |
881 | scope.isStepValid = function() { |
882 | return form.$valid; | |
883 | }; | |
6717e4b9 | 884 | crmUiWizardCtrl.add(scope); |
8688b463 TO |
885 | element.on('$destroy', function(){ |
886 | crmUiWizardCtrl.remove(scope); | |
887 | }); | |
6717e4b9 | 888 | } |
030dce01 TO |
889 | }; |
890 | }) | |
891 | ||
5fb5b3cf | 892 | // Example: <button crm-confirm="{message: ts('Are you sure you want to continue?')}" on-yes="frobnicate(123)">Frobincate</button> |
4b8c8b42 | 893 | // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button> |
6a6d0f41 TO |
894 | // Example: <button crm-confirm="{templateUrl: '~/path/to/view.html', export: {foo: bar}}" on-yes="frobnicate(123)">Frobincate</button> |
895 | .directive('crmConfirm', function ($compile, $rootScope, $templateRequest, $q) { | |
88fcc9f1 | 896 | // Helpers to calculate default options for CRM.confirm() |
4b8c8b42 TO |
897 | var defaultFuncs = { |
898 | 'disable': function (options) { | |
899 | return { | |
900 | message: ts('Are you sure you want to disable this?'), | |
901 | options: {no: ts('Cancel'), yes: ts('Disable')}, | |
902 | width: 300, | |
903 | title: ts('Disable %1?', { | |
904 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
905 | }) | |
906 | }; | |
907 | }, | |
470a458e TO |
908 | 'revert': function (options) { |
909 | return { | |
910 | message: ts('Are you sure you want to revert this?'), | |
911 | options: {no: ts('Cancel'), yes: ts('Revert')}, | |
912 | width: 300, | |
913 | title: ts('Revert %1?', { | |
914 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
915 | }) | |
916 | }; | |
917 | }, | |
4b8c8b42 TO |
918 | 'delete': function (options) { |
919 | return { | |
920 | message: ts('Are you sure you want to delete this?'), | |
921 | options: {no: ts('Cancel'), yes: ts('Delete')}, | |
922 | width: 300, | |
923 | title: ts('Delete %1?', { | |
924 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
925 | }) | |
926 | }; | |
927 | } | |
928 | }; | |
6a6d0f41 | 929 | var confirmCount = 0; |
4b8c8b42 | 930 | return { |
4b8c8b42 TO |
931 | link: function (scope, element, attrs) { |
932 | $(element).click(function () { | |
f2bad133 | 933 | var options = scope.$eval(attrs.crmConfirm); |
b8603a6d CW |
934 | if (attrs.title && !options.title) { |
935 | options.title = attrs.title; | |
936 | } | |
4b8c8b42 | 937 | var defaults = (options.type) ? defaultFuncs[options.type](options) : {}; |
6a6d0f41 TO |
938 | |
939 | var tpl = null, stubId = null; | |
940 | if (!options.message) { | |
941 | if (options.templateUrl) { | |
942 | tpl = $templateRequest(options.templateUrl); | |
943 | } | |
944 | else if (options.template) { | |
945 | tpl = options.template; | |
946 | } | |
947 | if (tpl) { | |
948 | stubId = 'crmUiConfirm_' + (++confirmCount); | |
949 | options.message = '<div id="' + stubId + '"></div>'; | |
950 | } | |
951 | } | |
952 | ||
4b8c8b42 | 953 | CRM.confirm(_.extend(defaults, options)) |
6a6d0f41 TO |
954 | .on('crmConfirm:yes', function() { scope.$apply(attrs.onYes); }) |
955 | .on('crmConfirm:no', function() { scope.$apply(attrs.onNo); }); | |
956 | ||
957 | if (tpl && stubId) { | |
958 | $q.when(tpl, function(html) { | |
959 | var scope = options.scope || $rootScope.$new(); | |
960 | if (options.export) { | |
961 | angular.extend(scope, options.export); | |
962 | } | |
963 | var linker = $compile(html); | |
964 | $('#' + stubId).append($(linker(scope))); | |
965 | }); | |
966 | } | |
4b8c8b42 TO |
967 | }); |
968 | } | |
969 | }; | |
970 | }) | |
02308c07 TO |
971 | .run(function($rootScope, $location) { |
972 | /// Example: <button ng-click="goto('home')">Go home!</button> | |
973 | $rootScope.goto = function(path) { | |
974 | $location.path(path); | |
975 | }; | |
4b8c8b42 | 976 | // useful for debugging: $rootScope.log = console.log || function() {}; |
02308c07 | 977 | }) |
685acae4 | 978 | ; |
979 | ||
5fb5b3cf | 980 | })(angular, CRM.$, CRM._); |