1 /// crmUi: Sundry UI helpers
2 (function (angular
, $, _
) {
6 var partialUrl = function (relPath
) {
7 return CRM
.resourceUrls
['civicrm'] + '/partials/crmUi/' + relPath
;
10 angular
.module('crmUi', [])
12 // example <div crm-ui-accordion crm-title="ts('My Title')" crm-collapsed="true">...content...</div>
13 // WISHLIST: crmCollapsed should support two-way/continous binding
14 .directive('crmUiAccordion', function() {
20 template
: '<div class="crm-accordion-wrapper" ng-class="cssClasses"><div class="crm-accordion-header">{{$parent.$eval(crmTitle)}}</div><div class="crm-accordion-body" ng-transclude></div></div>',
22 link: function (scope
, element
, attrs
) {
24 collapsed
: scope
.$parent
.$eval(attrs
.crmCollapsed
)
30 // example: <input crm-ui-date="myobj.datefield" />
31 // example: <input crm-ui-date="myobj.datefield" crm-ui-date-format="yy-mm-dd" />
32 .directive('crmUiDate', function ($parse
, $timeout
) {
36 crmUiDate
: '@', // expression, model binding
37 crmUiDateFormat
: '@' // expression, date format (default: "yy-mm-dd")
39 link: function (scope
, element
, attrs
) {
40 var fmt
= attrs
.crmUiDateFormat
? $parse(attrs
.crmUiDateFormat
)() : "yy-mm-dd";
41 var model
= $parse(attrs
.crmUiDate
);
43 element
.addClass('dateplugin');
44 $(element
).datepicker({
48 var updateChildren
= (function() {
49 element
.off('change', updateParent
);
50 $(element
).datepicker('setDate', model(scope
.$parent
));
51 element
.on('change', updateParent
);
53 var updateParent
= (function() {
54 $timeout(function () {
55 model
.assign(scope
.$parent
, $(element
).val());
60 scope
.$parent
.$watch(attrs
.crmUiDate
, updateChildren
);
61 element
.on('change', updateParent
);
66 // example: <div crm-ui-date-time="myobj.mydatetimefield"></div>
67 .directive('crmUiDateTime', function ($parse
) {
73 template
: '<input crm-ui-date="dtparts.date" placeholder="{{dateLabel}}"/> <input crm-ui-time="dtparts.time" placeholder="{{timeLabel}}"/>',
74 link: function (scope
, element
, attrs
) {
75 var model
= $parse(attrs
.crmUiDateTime
);
76 scope
.dateLabel
= ts('Date');
77 scope
.timeLabel
= ts('Time');
79 var updateChildren
= (function () {
80 var value
= model(scope
.$parent
);
82 var dtparts
= value
.split(/ /);
83 scope
.dtparts
= {date
: dtparts
[0], time
: dtparts
[1]};
86 scope
.dtparts
= {date
: '', time
: ''};
89 var updateParent
= (function () {
90 model
.assign(scope
.$parent
, scope
.dtparts
.date
+ " " + scope
.dtparts
.time
);
94 scope
.$parent
.$watch(attrs
.crmUiDateTime
, updateChildren
);
95 scope
.$watch('dtparts.date', updateParent
),
96 scope
.$watch('dtparts.time', updateParent
)
101 // Display a field/row in a field list
102 // example: <div crm-ui-field crm-title="My Field"> {{mydata}} </div>
103 // example: <div crm-ui-field="subform.myfield" crm-title="'My Field'"> <input crm-ui-id="subform.myfield" name="myfield" /> </div>
104 // example: <div crm-ui-field="subform.myfield" crm-title="'My Field'"> <input crm-ui-id="subform.myfield" name="myfield" required /> </div>
105 .directive('crmUiField', function() {
106 // Note: When writing new templates, the "label" position is particular. See/patch "var label" below.
108 default: partialUrl('field.html'),
109 checkbox
: partialUrl('field-cb.html')
113 require
: '^crmUiIdScope',
119 templateUrl: function(tElement
, tAttrs
){
120 var layout
= tAttrs
.crmLayout
? tAttrs
.crmLayout
: 'default';
121 return templateUrls
[layout
];
124 link: function (scope
, element
, attrs
, crmUiIdCtrl
) {
125 $(element
).addClass('crm-section');
126 scope
.crmUiField
= attrs
.crmUiField
;
127 scope
.crmTitle
= attrs
.crmTitle
;
132 // 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>
133 .directive('crmUiId', function () {
135 require
: '^crmUiIdScope',
138 pre: function (scope
, element
, attrs
, crmUiIdCtrl
) {
139 var id
= crmUiIdCtrl
.get(attrs
.crmUiId
);
140 element
.attr('id', id
);
146 // 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>
147 .directive('crmUiFor', function ($parse
, $timeout
) {
149 require
: '^crmUiIdScope',
151 template
: '<span ng-class="cssClasses"><span ng-transclude/><span crm-ui-visible="crmIsRequired" class="crm-marker" title="This field is required.">*</span></span>',
153 link: function (scope
, element
, attrs
, crmUiIdCtrl
) {
154 scope
.crmIsRequired
= false;
155 scope
.cssClasses
= {};
157 if (!attrs
.crmUiFor
) return;
159 var id
= crmUiIdCtrl
.get(attrs
.crmUiFor
);
160 element
.attr('for', id
);
163 var updateCss = function () {
164 scope
.cssClasses
['crm-error'] = !ngModel
.$valid
&& !ngModel
.$pristine
;
167 // Note: if target element is dynamically generated (eg via ngInclude), then it may not be available
168 // immediately for initialization. Use retries/retryDelay to initialize such elements.
169 var init = function (retries
, retryDelay
) {
170 var input
= $('#' + id
);
171 if (input
.length
== 0) {
174 init(retries
-1, retryDelay
);
180 var tgtScope
= scope
;//.$parent;
181 if (attrs
.crmDepth
) {
182 for (var i
= attrs
.crmDepth
; i
> 0; i
--) {
183 tgtScope
= tgtScope
.$parent
;
187 if (input
.attr('ng-required')) {
188 scope
.crmIsRequired
= scope
.$parent
.$eval(input
.attr('ng-required'));
189 scope
.$parent
.$watch(input
.attr('ng-required'), function (isRequired
) {
190 scope
.crmIsRequired
= isRequired
;
194 scope
.crmIsRequired
= input
.prop('required')
197 ngModel
= $parse(attrs
.crmUiFor
)(tgtScope
);
199 ngModel
.$viewChangeListeners
.push(updateCss
);
210 // 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>
211 .directive('crmUiIdScope', function () {
215 controllerAs
: 'crmUiIdCtrl',
216 controller: function($scope
) {
218 this.get = function(name
) {
220 ids
[name
] = "crmUiId_" + (++uidCount
);
225 link: function (scope
, element
, attrs
) {}
229 // example: <iframe crm-ui-iframe="getHtmlContent()"></iframe>
230 .directive('crmUiIframe', function ($parse
) {
233 crmUiIframe
: '@' // expression which evalutes to HTML content
235 link: function (scope
, elm
, attrs
) {
236 var iframe
= $(elm
)[0];
237 iframe
.setAttribute('width', '100%');
238 iframe
.setAttribute('frameborder', '0');
240 var refresh = function () {
241 // var iframeHtml = '<html><head><base target="_blank"></head><body onload="parent.document.getElementById(\'' + iframe.id + '\').style.height=document.body.scrollHeight + \'px\'"><scr' + 'ipt type="text/javascript" src="https://gist.github.com/' + iframeId + '.js"></sc' + 'ript></body></html>';
242 var iframeHtml
= scope
.$parent
.$eval(attrs
.crmUiIframe
);
244 var doc
= iframe
.document
;
245 if (iframe
.contentDocument
) {
246 doc
= iframe
.contentDocument
;
248 else if (iframe
.contentWindow
) {
249 doc
= iframe
.contentWindow
.document
;
253 doc
.writeln(iframeHtml
);
257 scope
.$parent
.$watch(attrs
.crmUiIframe
, refresh
);
258 //setTimeout(function () { refresh(); }, 50);
263 // example: <textarea crm-ui-id="myForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html"></textarea>
264 .directive('crmUiRichtext', function ($timeout
) {
267 link: function (scope
, elm
, attr
, ngModel
) {
268 var ck
= CKEDITOR
.replace(elm
[0]);
274 ck
.on('pasteState', function () {
275 scope
.$apply(function () {
276 ngModel
.$setViewValue(ck
.getData());
280 ck
.on('insertText', function () {
281 $timeout(function () {
282 ngModel
.$setViewValue(ck
.getData());
286 ngModel
.$render = function (value
) {
287 ck
.setData(ngModel
.$viewValue
);
293 // example: <a crm-ui-lock binding="mymodel.boolfield"></a>
294 // example: <a crm-ui-lock
295 // binding="mymodel.boolfield"
296 // title-locked="ts('Boolfield is locked')"
297 // title-unlocked="ts('Boolfield is unlocked')"></a>
298 .directive('crmUiLock', function ($parse
, $rootScope
) {
299 var defaultVal = function (defaultValue
) {
300 var f = function (scope
) {
303 f
.assign = function (scope
, value
) {
309 // like $parse, but accepts a defaultValue in case expr is undefined
310 var parse = function (expr
, defaultValue
) {
311 return expr
? $parse(expr
) : defaultVal(defaultValue
);
316 link: function (scope
, element
, attrs
) {
317 var binding
= parse(attrs
['binding'], true);
318 var titleLocked
= parse(attrs
['titleLocked'], ts('Locked'));
319 var titleUnlocked
= parse(attrs
['titleUnlocked'], ts('Unlocked'));
321 $(element
).addClass('ui-icon lock-button');
322 var refresh = function () {
323 var locked
= binding(scope
);
326 .removeClass('ui-icon-unlocked')
327 .addClass('ui-icon-locked')
328 .prop('title', titleLocked(scope
))
333 .removeClass('ui-icon-locked')
334 .addClass('ui-icon-unlocked')
335 .prop('title', titleUnlocked(scope
))
340 $(element
).click(function () {
341 binding
.assign(scope
, !binding(scope
));
343 $rootScope
.$digest();
346 scope
.$watch(attrs
.binding
, refresh
);
347 scope
.$watch(attrs
.titleLocked
, refresh
);
348 scope
.$watch(attrs
.titleUnlocked
, refresh
);
355 // usage: <select crm-ui-select="{placeholder:'Something',allowClear:true,...}" ng-model="myobj.field"><option...></select>
356 .directive('crmUiSelect', function ($parse
, $timeout
) {
362 link: function (scope
, element
, attrs
, ngModel
) {
363 // In cases where UI initiates update, there may be an extra
364 // call to refreshUI, but it doesn't create a cycle.
366 ngModel
.$render = function () {
367 $timeout(function () {
368 // ex: msg_template_id adds new item then selects it; use $timeout to ensure that
369 // new item is added before selection is made
370 $(element
).select2('val', ngModel
.$viewValue
);
373 function refreshModel() {
374 var oldValue
= ngModel
.$viewValue
, newValue
= $(element
).select2('val');
375 if (oldValue
!= newValue
) {
376 scope
.$parent
.$apply(function () {
377 ngModel
.$setViewValue(newValue
);
383 // TODO watch select2-options
384 var options
= attrs
.crmUiSelect
? scope
.$parent
.$eval(attrs
.crmUiSelect
) : {};
385 $(element
).select2(options
);
386 $(element
).on('change', refreshModel
);
387 $timeout(ngModel
.$render
);
395 // example <div crm-ui-tab crm-title="ts('My Title')">...content...</div>
396 // WISHLIST: use a full Angular component instead of an incomplete jQuery wrapper
397 .directive('crmUiTab', function($parse
) {
399 require
: '^crmUiTabSet',
405 template
: '<div ng-transclude></div>',
407 link: function (scope
, element
, attrs
, crmUiTabSetCtrl
) {
408 crmUiTabSetCtrl
.add(scope
);
413 // 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>
414 .directive('crmUiTabSet', function() {
420 templateUrl
: partialUrl('tabset.html'),
422 controllerAs
: 'crmUiTabSetCtrl',
423 controller: function($scope
, $parse
) {
424 var tabs
= $scope
.tabs
= []; // array<$scope>
425 this.add = function(tab
) {
426 if (!tab
.id
) throw "Tab is missing 'id'";
430 link: function (scope
, element
, attrs
) {}
434 // example: <input crm-ui-time="myobj.mytimefield" />
435 .directive('crmUiTime', function ($parse
, $timeout
) {
441 link: function (scope
, element
, attrs
) {
442 var model
= $parse(attrs
.crmUiTime
);
444 element
.addClass('crm-form-text six');
445 $(element
).timeEntry({show24Hours
: true});
447 var updateChildren
= (function() {
448 element
.off('change', updateParent
);
449 $(element
).timeEntry('setTime', model(scope
.$parent
));
450 element
.on('change', updateParent
);
452 var updateParent
= (function () {
453 $timeout(function () {
454 model
.assign(scope
.$parent
, element
.val());
459 scope
.$parent
.$watch(attrs
.crmUiTime
, updateChildren
);
460 element
.on('change', updateParent
);
465 // like ng-show, but hides/displays elements using "visibility" which maintains positioning
466 // example <div crm-ui-visible="false">...content...</div>
467 .directive('crmUiVisible', function($parse
) {
473 link: function (scope
, element
, attrs
) {
474 var model
= $parse(attrs
.crmUiVisible
);
475 function updatecChildren() {
476 element
.css('visibility', model(scope
.$parent
) ? 'inherit' : 'hidden');
479 scope
.$parent
.$watch(attrs
.crmUiVisible
, updatecChildren
);
484 // 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>
485 // Note: "myWizardCtrl" has various actions/properties like next() and $first().
486 // WISHLIST: Allow each step to determine if it is "complete" / "valid" / "selectable"
487 // WISHLIST: Allow each step to enable/disable (show/hide) itself
488 .directive('crmUiWizard', function() {
494 templateUrl
: partialUrl('wizard.html'),
496 controllerAs
: 'crmUiWizardCtrl',
497 controller: function($scope
, $parse
) {
498 var steps
= $scope
.steps
= []; // array<$scope>
499 var crmUiWizardCtrl
= this;
501 var selectedIndex
= null;
503 var findIndex = function() {
505 angular
.forEach(steps
, function(step
, stepKey
) {
506 if (step
.selected
) found
= stepKey
;
511 /// @return int the index of the current step
512 this.$index = function() { return selectedIndex
; };
513 /// @return bool whether the currentstep is first
514 this.$first = function() { return this.$index() === 0; };
515 /// @return bool whether the current step is last
516 this.$last = function() { return this.$index() === steps
.length
-1; };
517 this.$maxVisit = function() { return maxVisited
; }
518 this.iconFor = function(index
) {
519 if (index
< this.$index()) return '√';
520 if (index
=== this.$index()) return '»';
523 this.isSelectable = function(step
) {
524 if (step
.selected
) return false;
526 angular
.forEach(steps
, function(otherStep
, otherKey
) {
527 if (step
=== otherStep
&& otherKey
<= maxVisited
) result
= true;
532 /*** @param Object step the $scope of the step */
533 this.select = function(step
) {
534 angular
.forEach(steps
, function(otherStep
, otherKey
) {
535 otherStep
.selected
= (otherStep
=== step
);
536 if (otherStep
=== step
&& maxVisited
< otherKey
) maxVisited
= otherKey
;
538 selectedIndex
= findIndex();
540 /*** @param Object step the $scope of the step */
541 this.add = function(step
) {
542 if (steps
.length
=== 0) {
543 step
.selected
= true;
547 steps
.sort(function(a
,b
){
548 return a
.crmUiWizardStep
- b
.crmUiWizardStep
;
550 selectedIndex
= findIndex();
552 this.remove = function(step
) {
554 angular
.forEach(steps
, function(otherStep
, otherKey
) {
555 if (otherStep
=== step
) key
= otherKey
;
558 steps
.splice(key
, 1);
561 this.goto = function(index
) {
562 if (index
< 0) index
= 0;
563 if (index
>= steps
.length
) index
= steps
.length
-1;
564 this.select(steps
[index
]);
566 this.previous = function() { this.goto(this.$index()-1); };
567 this.next = function() { this.goto(this.$index()+1); };
568 if ($scope
.crmUiWizard
) {
569 $parse($scope
.crmUiWizard
).assign($scope
.$parent
, this)
572 link: function (scope
, element
, attrs
) {}
576 // Use this to add extra markup to wizard
577 .directive('crmUiWizardButtons', function() {
579 require
: '^crmUiWizard',
582 template
: '<span ng-transclude></span>',
584 link: function (scope
, element
, attrs
, crmUiWizardCtrl
) {
585 var realButtonsEl
= $(element
).closest('.crm-wizard').find('.crm-wizard-buttons');
586 $(element
).appendTo(realButtonsEl
);
591 // example: <div crm-ui-wizard-step crm-title="ts('My Title')">...content...</div>
592 // If there are any conditional steps, then be sure to set a weight explicitly on *all* steps to maintain ordering.
593 // example: <div crm-ui-wizard-step="100" crm-title="..." ng-if="...">...content...</div>
594 .directive('crmUiWizardStep', function() {
597 require
: '^crmUiWizard',
600 crmTitle
: '@', // expression, evaluates to a printable string
601 crmUiWizardStep
: '@' // int, a weight which determines the ordering of the steps
603 template
: '<div class="crm-wizard-step" ng-show="selected" ng-transclude/></div>',
605 link: function (scope
, element
, attrs
, crmUiWizardCtrl
) {
606 if (scope
.crmUiWizardStep
) {
607 scope
.crmUiWizardStep
= parseInt(scope
.crmUiWizardStep
);
609 scope
.crmUiWizardStep
= nextWeight
++;
611 crmUiWizardCtrl
.add(scope
);
612 element
.on('$destroy', function(){
613 crmUiWizardCtrl
.remove(scope
);
619 // Example: <button crm-confirm="{message: ts('Are you sure you want to continue?')}" on-yes="frobnicate(123)">Frobincate</button>
620 // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button>
621 .directive('crmConfirm', function () {
622 // Helpers to calculate default options for CRM.confirm()
624 'disable': function (options
) {
626 message
: ts('Are you sure you want to disable this?'),
627 options
: {no
: ts('Cancel'), yes
: ts('Disable')},
629 title
: ts('Disable %1?', {
630 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
634 'revert': function (options
) {
636 message
: ts('Are you sure you want to revert this?'),
637 options
: {no
: ts('Cancel'), yes
: ts('Revert')},
639 title
: ts('Revert %1?', {
640 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
644 'delete': function (options
) {
646 message
: ts('Are you sure you want to delete this?'),
647 options
: {no
: ts('Cancel'), yes
: ts('Delete')},
649 title
: ts('Delete %1?', {
650 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
657 link: function (scope
, element
, attrs
) {
658 $(element
).click(function () {
659 var options
= scope
.$eval(attrs
['crmConfirm']);
660 var defaults
= (options
.type
) ? defaultFuncs
[options
.type
](options
) : {};
661 CRM
.confirm(_
.extend(defaults
, options
))
662 .on('crmConfirm:yes', function () { scope
.$apply(attrs
['onYes']); })
663 .on('crmConfirm:no', function () { scope
.$apply(attrs
['onNo']); });
668 .run(function($rootScope
, $location
) {
669 /// Example: <button ng-click="goto('home')">Go home!</button>
670 $rootScope
.goto = function(path
) {
671 $location
.path(path
);
673 // useful for debugging: $rootScope.log = console.log || function() {};
677 })(angular
, CRM
.$, CRM
._
);