Skip to content
This repository was archived by the owner on Nov 15, 2019. It is now read-only.

Commit b8b13fb

Browse files
author
Filippo Conti
authored
Merge pull request #15 from seeq12/feature-is-editing
Close #14 Merged pull request
2 parents 8ec297a + 2cae95e commit b8b13fb

File tree

5 files changed

+74
-29
lines changed

5 files changed

+74
-29
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and you are ready to go, add the directive to any element you want:
3636
* __focus-select__: if set to true when element goes to focus, all the text inside will be selected
3737
* __render-html__: if set to true allow the text passed as input to be compiled and rendered
3838
* __edit-callback__: a callback that is called wherever the model value is changed
39+
* __is-editing__: optional argument that can be used to programatically enable/disable the editor
3940

4041
Note that, __edit-callback__ has two arguments, that you must specify in your template to use them:
4142
* __text__: the new text inside the element
@@ -120,7 +121,9 @@ grunt build // build the package for distribution
120121
1. Create an issue and describe your idea
121122
2. Fork the project (https://github.com/codekraft-studio/angular-content-editable/fork)
122123
3. Create your feature branch (`git checkout -b my-new-feature`)
123-
4. Commit your changes (`git commit -am 'Add some feature'`)
124-
5. Publish the branch (`git push origin my-new-feature`)
125-
6. Add some test for your new feature
126-
7. Create a new Pull Request
124+
4. Get the development environment set up (`npm install`)
125+
5. Commit your changes (`git commit -am 'Add some feature'`)
126+
6. Add some test for your new feature (`npm test`)
127+
7. Build the directive with the new changes (`grunt build`)
128+
8. Publish the branch (`git push origin my-new-feature`)
129+
9. Create a new Pull Request

dist/angular-content-editable.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ angular.module('angular-content-editable')
77
var directive = {
88
restrict: 'A',
99
require: 'ngModel',
10-
scope: { editCallback: '&?' },
10+
scope: { editCallback: '&?', isEditing: '=?' },
1111
link: _link
12-
}
12+
};
1313

1414
return directive;
1515

@@ -37,10 +37,20 @@ angular.module('angular-content-editable')
3737
// add editable class
3838
attrs.$addClass(options.editableClass);
3939

40+
scope.$watch('isEditing', function(newValue, oldValue) {
41+
if (newValue !== oldValue) {
42+
if (newValue) {
43+
originalElement.click();
44+
} else {
45+
originalElement.blur();
46+
}
47+
}
48+
});
49+
4050
// render always with model value
4151
ngModel.$render = function() {
4252
elem.html( ngModel.$modelValue || elem.html() );
43-
}
53+
};
4454

4555
// handle click on element
4656
function onClick(e) {
@@ -53,9 +63,12 @@ angular.module('angular-content-editable')
5363
// check some option extra
5464
// conditions during focus
5565
function onFocus(e) {
56-
57-
scope.$apply(function() {
58-
66+
67+
// evalAsync in case a digest is already in progress (e.g. changing isEditing to true)
68+
scope.$evalAsync(function() {
69+
70+
scope.isEditing = true;
71+
5972
// turn on the flag
6073
noEscape = true;
6174

@@ -80,12 +93,14 @@ angular.module('angular-content-editable')
8093
}
8194

8295
function onBlur(e) {
83-
96+
8497
scope.$apply(function() {
85-
98+
8699
// the text
87100
var html;
88101

102+
scope.isEditing = false;
103+
89104
// remove active class when editing is over
90105
attrs.$removeClass('active');
91106

@@ -113,7 +128,7 @@ angular.module('angular-content-editable')
113128
* when a controller wants to
114129
* change the view value
115130
*/
116-
ngModel.$setViewValue(html)
131+
ngModel.$setViewValue(html);
117132

118133
// if user passed a variable
119134
// and is a function
@@ -195,7 +210,7 @@ angular.module('angular-content-editable')
195210

196211
}
197212

198-
}])
213+
}]);
199214

200215
angular.module('angular-content-editable')
201216

dist/angular-content-editable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-content-editable.directive.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ angular.module('angular-content-editable')
55
var directive = {
66
restrict: 'A',
77
require: 'ngModel',
8-
scope: { editCallback: '&?' },
8+
scope: { editCallback: '&?', isEditing: '=?' },
99
link: _link
10-
}
10+
};
1111

1212
return directive;
1313

@@ -35,10 +35,20 @@ angular.module('angular-content-editable')
3535
// add editable class
3636
attrs.$addClass(options.editableClass);
3737

38+
scope.$watch('isEditing', function(newValue, oldValue) {
39+
if (newValue !== oldValue) {
40+
if (newValue) {
41+
originalElement.click();
42+
} else {
43+
originalElement.blur();
44+
}
45+
}
46+
});
47+
3848
// render always with model value
3949
ngModel.$render = function() {
4050
elem.html( ngModel.$modelValue || elem.html() );
41-
}
51+
};
4252

4353
// handle click on element
4454
function onClick(e) {
@@ -51,9 +61,12 @@ angular.module('angular-content-editable')
5161
// check some option extra
5262
// conditions during focus
5363
function onFocus(e) {
54-
55-
scope.$apply(function() {
56-
64+
65+
// evalAsync in case a digest is already in progress (e.g. changing isEditing to true)
66+
scope.$evalAsync(function() {
67+
68+
scope.isEditing = true;
69+
5770
// turn on the flag
5871
noEscape = true;
5972

@@ -78,12 +91,14 @@ angular.module('angular-content-editable')
7891
}
7992

8093
function onBlur(e) {
81-
94+
8295
scope.$apply(function() {
83-
96+
8497
// the text
8598
var html;
8699

100+
scope.isEditing = false;
101+
87102
// remove active class when editing is over
88103
attrs.$removeClass('active');
89104

@@ -111,7 +126,7 @@ angular.module('angular-content-editable')
111126
* when a controller wants to
112127
* change the view value
113128
*/
114-
ngModel.$setViewValue(html)
129+
ngModel.$setViewValue(html);
115130

116131
// if user passed a variable
117132
// and is a function
@@ -193,4 +208,4 @@ angular.module('angular-content-editable')
193208

194209
}
195210

196-
})
211+
});

test/angular-content-editable.directive.test.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@ describe("Angular Content Editable: Directive", function () {
1818
scope = $rootScope.$new();
1919
scope.myModel = 'Text to be modified.';
2020
scope.onEdit = jasmine.createSpy('onEdit');
21-
element = $compile('<h1 ng-model="myModel" edit-callback="onEdit(text, elem)" content-editable></h1>')(scope);
21+
scope.isEditing = false;
22+
element = angular.element('<h1 ng-model="myModel" edit-callback="onEdit(\'extraArg\', text, elem)" is-editing="isEditing" content-editable></h1>');
23+
$compile(element)(scope);
2224
scope.$digest();
2325
});
2426

2527
it('should init the viewValue with ngModel', function() {
2628
expect(element.html()).toContain("Text to be modified.");
2729
});
2830

29-
it("should set contenteditable on click", function () {
31+
it('should set contenteditable on click', function () {
32+
expect(element.attr('contenteditable')).toBeUndefined();
3033
element.triggerHandler('click');
3134
scope.$digest();
3235
expect(element.attr('contenteditable')).toBe('true');
3336
});
3437

35-
it("should fire a edit callback", function () {
38+
it('should fire an edit callback with the correct arguments', function () {
3639
element.triggerHandler('click');
3740
element.html('Some random text.');
3841
element.triggerHandler('blur');
3942
scope.$digest();
40-
expect(scope.onEdit).toHaveBeenCalled();
43+
expect(scope.onEdit).toHaveBeenCalledWith('extraArg', 'Some random text.', element);
4144
});
4245

46+
it('should enable editing programatically using isEditing', function () {
47+
expect(element.attr('contenteditable')).toBeUndefined();
48+
scope.isEditing = true;
49+
scope.$digest();
50+
expect(element.attr('contenteditable')).toBe('true');
51+
element.triggerHandler('blur');
52+
scope.$digest();
53+
expect(scope.isEditing).toBe(false);
54+
});
4355
});

0 commit comments

Comments
 (0)