-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTags.js
132 lines (112 loc) · 2.78 KB
/
parseTags.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//parsing tag input
var ParseTags = {
/**
* Quotes a single tag
*/
quoteTag: function (tag) {
tag = tag
.replace(/"/g, "'")
.replace(/\s+/g, ' ')
.replace(/^\s+|\s+$/g, '');
if (tag.match(/\s+|,/)) {
tag = '"' + tag + '"';
}
return tag;
},
//input text => tag array
//tags input: comma or space separated. double quotes allowed
//merge quoted if necessary
//TEST: 'a b c' => ['a', 'b', 'c']
//TEST: 'a,b,c' => ['a', 'b', 'c']
//TEST: 'a b,c' => ['a', 'b', 'c']
//TEST: 'a "b,c"' => ['a', 'bc']
//TEST: '"a b" c' => ['a b', 'c']
//TEST: '"a b c' => ['abc']
parseTags: function (strTags, validate) {
var stack = [], tags = [];
var begin_delimiter = false;
for (var i = 0, len = strTags.length, c; c = strTags.charAt(i), i < len; i++) {
if (c == '"') {
if (!begin_delimiter)
begin_delimiter = true;
else {
begin_delimiter = false;
clearStack();
}
} else {
if (begin_delimiter) {
stack.push(c);
} else {
if (/\s/.test(c) || c == ',')
clearStack();
else {
stack.push(c);
}
}
}
}
clearStack(); //if there is unclosed delimiter
if (validate)
tags = map2(unique(tags), function(t) {return trim(t) || null;});
return tags;
function clearStack() {
if (stack.length > 0) {
tags.push(stack.join(''));
stack.length = 0;
}
}
},
/**
* tag array => input text
*/
unparseTags: function (tagArray, joinBy) {
joinBy = joinBy || ' ';
return map(tagArray, function(t) {return this.quoteTag(t);}, this).join(joinBy);
}
};
function placeCursorAtLast(input){
var pos = input.value.length;
setTimeout(function() {
placeInputCursor(input, pos);
},13)
}
function placeInputCursor(inputEle, pos){
var ele = inputEle;
ele.focus();
if(ele.createTextRange) {
/* Create a TextRange, set the internal pointer to
a specified position and show the cursor at this
position
*/
var range = ele.createTextRange();
range.move("character", pos);
range.select();
} else
if(ele.selectionStart >= 0) {
/* Gecko is a little bit shorter on that. Simply
focus the element and set the selection to a
specified position
*/
ele.focus();
ele.setSelectionRange(pos, pos);
}
}
function toggleTag(tag){
var input = $('#diigobm-tag-input');
var _tags = ParseTags.parseTags(input.val(),true);
var i = $.inArray(tag, _tags);
if(i >= 0){
_tags.splice(i, 1);
}
else if(i == -1){
_tags.push(tag);
}
var newTag = ParseTags.unparseTags(_tags);
if(newTag.length){
newTag += ' ';
}
input.val(newTag);
placeCursorAtLast(input[0]);
updateTagStatus();
}
//extend(D, ParseTags);