-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorWithTokens.controller.js
66 lines (49 loc) · 2.09 KB
/
EditorWithTokens.controller.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
angular.module("umbraco").controller("smb.editorWithTokensController",
function($scope) {
$scope.lastFocused;
angular.element("textarea").focus(function() {
$scope.lastFocused = document.activeElement;
});
$scope.addToken = function() {
$scope.insertText($scope.token.value);
};
//http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery
$scope.insertText = function(text) {
var input = $scope.lastFocused;
console.log(input);
if (input === undefined) {
return;
}
var scrollPos = input.scrollTop;
var pos = 0;
var browser = ((input.selectionStart || input.selectionStart == "0")
? "ff"
: (document.selection ? "ie" : false));
if (browser == "ie") {
input.focus();
var range = document.selection.createRange();
range.moveStart("character", -input.value.length);
pos = range.text.length;
} else if (browser == "ff") {
pos = input.selectionStart;
};
var front = (input.value).substring(0, pos);
var back = (input.value).substring(pos, input.value.length);
input.value = front + text + back;
pos = pos + text.length;
if (browser == "ie") {
input.focus();
var range = document.selection.createRange();
range.moveStart("character", -input.value.length);
range.moveStart("character", pos);
range.moveEnd("character", 0);
range.select();
} else if (browser == "ff") {
input.selectionStart = pos;
input.selectionEnd = pos;
input.focus();
}
input.scrollTop = scrollPos;
$scope.model.value = angular.element(input).val();
};
});