Merge pull request #3582 from totten/master-auto-reconcile
[civicrm-core.git] / js / angular-crm-ui.js
CommitLineData
685acae4 1/// crmUi: Sundry UI helpers
2(function (angular, $, _) {
3
4 angular.module('crmUi', [])
5
6 // example: <a crm-ui-lock binding="mymodel.boolfield"></a>
7 // example: <a crm-ui-lock
8 // binding="mymodel.boolfield"
9 // title-locked="ts('Boolfield is locked')"
10 // title-unlocked="ts('Boolfield is unlocked')"></a>
11 .directive('crmUiLock', function ($parse, $rootScope) {
12 var defaultVal = function (defaultValue) {
13 var f = function (scope) {
14 return defaultValue;
15 }
16 f.assign = function (scope, value) {
17 // ignore changes
18 }
19 return f;
20 };
21
22 // like $parse, but accepts a defaultValue in case expr is undefined
23 var parse = function (expr, defaultValue) {
24 return expr ? $parse(expr) : defaultVal(defaultValue);
25 };
26
27 return {
28 template: '',
29 link: function (scope, element, attrs) {
30 var binding = parse(attrs['binding'], true);
31 var titleLocked = parse(attrs['titleLocked'], ts('Locked'));
32 var titleUnlocked = parse(attrs['titleUnlocked'], ts('Unlocked'));
33
34 $(element).addClass('ui-icon lock-button');
35 var refresh = function () {
36 var locked = binding(scope);
37 if (locked) {
38 $(element)
39 .removeClass('ui-icon-unlocked')
40 .addClass('ui-icon-locked')
41 .prop('title', titleLocked(scope))
42 ;
43 }
44 else {
45 $(element)
46 .removeClass('ui-icon-locked')
47 .addClass('ui-icon-unlocked')
48 .prop('title', titleUnlocked(scope))
49 ;
50 }
51 };
52
53 $(element).click(function () {
54 binding.assign(scope, !binding(scope));
55 //scope.$digest();
56 $rootScope.$digest();
57 });
58
59 scope.$watch(attrs.binding, refresh);
60 scope.$watch(attrs.titleLocked, refresh);
61 scope.$watch(attrs.titleUnlocked, refresh);
62
63 refresh();
64 }
65 };
66 })
67 ;
68
69})(angular, CRM.$, CRM._);