Commit | Line | Data |
---|---|---|
cb46dc65 CW |
1 | // https://civicrm.org/licensing |
2 | (function(angular, $, _) { | |
3 | "use strict"; | |
4 | ||
5 | // Editable titles using ngModel & html5 contenteditable | |
6 | // Cribbed from ContactLayoutEditor | |
7 | angular.module('afGuiEditor').directive("afGuiEditable", function() { | |
8 | return { | |
9 | restrict: "A", | |
10 | require: "ngModel", | |
11 | scope: { | |
12 | defaultValue: '=' | |
13 | }, | |
14 | link: function(scope, element, attrs, ngModel) { | |
15 | var ts = CRM.ts(); | |
16 | ||
17 | function read() { | |
18 | var htmlVal = element.html(); | |
19 | if (!htmlVal) { | |
20 | htmlVal = scope.defaultValue; | |
21 | element.text(htmlVal); | |
22 | } | |
23 | ngModel.$setViewValue(htmlVal); | |
24 | } | |
25 | ||
26 | ngModel.$render = function() { | |
27 | element.text(ngModel.$viewValue || scope.defaultValue); | |
28 | }; | |
29 | ||
30 | // Special handling for enter and escape keys | |
31 | element.on('keydown', function(e) { | |
32 | // Enter: prevent line break and save | |
33 | if (e.which === 13) { | |
34 | e.preventDefault(); | |
35 | element.blur(); | |
36 | } | |
37 | // Escape: undo | |
38 | if (e.which === 27) { | |
39 | element.html(ngModel.$viewValue || scope.defaultValue); | |
40 | element.blur(); | |
41 | } | |
42 | }); | |
43 | ||
44 | element.on("blur change", function() { | |
45 | scope.$apply(read); | |
46 | }); | |
47 | ||
48 | element.attr('contenteditable', 'true').addClass('crm-editable-enabled'); | |
49 | } | |
50 | }; | |
51 | }); | |
52 | ||
53 | })(angular, CRM.$, CRM._); |