-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathneed-widget.js
88 lines (67 loc) · 2.16 KB
/
need-widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
angular.module('Teem')
.factory('needWidget', [
'$compile', '$timeout',
function($compile, $timeout) {
function getWidget (scope) {
return {
onInit: function(parent, needId) {
var element = angular.element(document.createElement('need-widget')),
compiled = $compile(element)(scope),
need = scope.project.findNeed(needId),
stopEvents = ['keypress', 'keyup', 'keydown', 'paste'],
isolateScope;
// Cancel widget contenteditable attributes
element.attr('contenteditable', 'false');
function stopEvent (e) { e.stopPropagation(); }
stopEvents.forEach(function (eventName) {
parent.addEventListener(eventName, stopEvent);
});
$timeout(() => {
isolateScope = element.isolateScope();
isolateScope.project = scope.project;
isolateScope.need = need;
});
// Wait for the directive to be compiled before adding it
$timeout(() => {
angular.element(parent).append(compiled);
// on blur, bump need version to generate SwellRT event
compiled[0].querySelector('textarea').addEventListener('blur', function(){
need.version =
((parseInt(need.version) || 0) + 1).toString();
});
});
}
};
}
function add (editor, scope) {
var need = {
text: ''
},
selection = editor.getSelection(),
widget;
if (selection.text) {
need.text = selection.text;
editor.deleteText(selection);
}
need = scope.project.addNeed(need);
// To generate need added event after all the info is available
$timeout();
if (selection.text) {
need.version = '1';
}
$timeout(() => {
widget = editor.addWidget('need', need._id);
});
$timeout(() => {
var textarea = angular.element(widget).find('textarea');
if (textarea) {
textarea.focus();
}
}, 500);
}
return {
getWidget,
add
};
}]);