ab17e8ebb0d769a0a3888ee68fb601cf4159a6e3.svn-base
8.21 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* utils.js
* (c) 2009 Signavio GmbH
*
* @author Willi Tscheschner
*
* Utils is a shared javascript file which provides common
* functionalities for all applications in the Signavio Process Editor
*
*/
// define namespace
if(!Signavio){ var Signavio = {} };
if(!Signavio.Utils){ Signavio.Utils = {} };
new function(){
/**
* Splits the text by " " and \n and adds those
* strings to the resulting set of the tokenization
* @param {String} text The text which should be tokenized
*/
Signavio.Utils.tokenize = function(text) {
var token = text.split(" ");
token = token.map(function(t, i){ return i == 0 ? t : [" ", t]}).flatten();
token = token.map(function(t){
var tn= t.split("\n");
return tn.map(function(tt, i){ return i == 0 ? tt : ["\n", tt]}).flatten();
}).flatten();
return token.findAll(function(t){ return t});
}
/**
* Strips all none letter and digits
* out of the string
*
* @param {String} phrase
* @return {String}
*/
Signavio.Utils.strip = function(phrase){
return phrase.toLowerCase()
.replace(/[^a-zäöüß0-9]/g, '')
.replace(/ü/g, 'u')
.replace(/ä/g, 'a')
.replace(/ö/g, 'o')
}
/**
* Returns true if a is in b included or other way around.
*
* @param {String} a
* @param {String} b
* @return {boolean}
*/
Signavio.Utils.isEqualTerms = function(a, b){
// Check if initial values are equal
if (a && b && (a.include(b) || b.include(a))){
return true;
}
// Strip
a = this.strip(a);
b = this.strip(b);
// Check if included
return a && b && (a.include(b) || b.include(a));
}
/**
* Replace all common stings from string1 with those
* which are in the string2
*
* @param {String} string1
* @param {String} string2
* @param {int} cursor (Optional)
*/
Signavio.Utils.replaceCommonSubString = function(string1, string2, cursor) {
// If string1 and string2 includes each other after stripingv
var s1 = this.strip(string1);
var s2 = this.strip(string2);
if(s1 === s2 || s2.include(s1)){
return string2;
}
// Get cursor pos
var pos = cursor === undefined ? string1.length : cursor;
// Tokenize
var textT = this.tokenize(string1);
var token = string2.split(/\s+/g); // Replace all special chars and split by " "
// Get index of the token where the cursor is
var selectedIndex = textT.indexOf(textT.find(function(t){ pos -= t == "\n" ? 0 : (t == " " ? 1 : t.length+1) ; return pos <= 0}));
selectedIndex = Math.max(selectedIndex , 0);
var rounds = textT.length;
var found = false;
// Go from the selected index to 0 and then up from the length of the textT to the selected index, but only one round
for (var i=selectedIndex; i != selectedIndex+1 && rounds >= 0; i == 0 ? i = textT.length -1 : --i, --rounds ){
// If current text phrase is empty, go furhter
if (textT[i] == " " || textT[i] == "\n") { continue }
// Find the token from the suggestion
var tok = token.find(function(t){ return this.isEqualTerms(t, textT[i]) }.bind(this));
// If there is a suggested phrases
if (tok) {
found = true;
// Partition all words to word which occur before and after this word
var front = token.slice(0, token.indexOf(tok)).reverse();
var back = token.slice(token.indexOf(tok)+1);
// Replace current phrase with the word
textT[i] = tok;
var lastKnownIndex = i;
// Go through every word which occurs before
for (var j=i; j>=0; --j){
if (textT[j] == " " || textT[j] == "\n") { continue }
tok = front.find(function(t){ return this.isEqualTerms(t, textT[j]) }.bind(this));
if (tok) {
var index = front.indexOf(tok);
textT[lastKnownIndex] = front.slice(0,index).reverse().join(" ") +(index >0?" ":"")+ textT[lastKnownIndex];
lastKnownIndex = j;
textT[j] = tok;
front= front.slice(index+1);
}
}
if (front.length >0){
textT[lastKnownIndex] = front.reverse().join(" ") + " " + textT[lastKnownIndex];
}
var lastKnownIndex = i;
// Go through every word which occurs after
for (var j=i; j<textT.length; j++){
if (textT[j] == " " || textT[j] == "\n") { continue }
tok = back.find(function(t){ return this.isEqualTerms(t, textT[j]) }.bind(this));
if (tok) {
var index = back.indexOf(tok);
textT[lastKnownIndex] += (index>0?" ":"")+back.slice(0, index).join(" ");
lastKnownIndex = j;
textT[j] = tok;
back = back.slice(index+1);
}
}
if (back.length >0){
textT[lastKnownIndex] += " "+back.join(" ");
}
break;
}
if (selectedIndex===0&&textT.length===1){break;}
}
var nString1 = textT.join("");
if (nString1 == string1 && !found) {
nString1 += (nString1.endsWith(" ") ? "" : " ") + string2;
}
return nString1;
}
/**
* Returns a string which is
* HTML unescaped.
*
* @param {String} str
* @return {String}
*/
Signavio.Utils.unescapeHTML = function(str){
str = str || "";
var d = document.createElement("div");
try {
d.innerHTML = str;
} catch (e){
d.textContent = str;
}
var nstr = d.textContent || d.innerText || "";
// Unescape the unecaped string till no changes are there
return nstr && str && nstr !== str ? Signavio.Utils.unescapeHTML(nstr) : nstr;
}
/**
* Returns a string which is
* HTML escaped.
*
* @param {String} str
* @return {String}
*/
Signavio.Utils.escapeHTML = function(str){
str = str || "";
var d = document.createElement("div");
try {
d.innerHTML = str;
} catch (e){
if (!!str.match(/&[aAoOuU]uml;/g)||!!str.match(/ß/g)){
$H({
"ä" : "ä",
"Ä" : "Ä",
"ö" : "ö",
"Ö" : "Ö",
"ü" : "ü",
"Ü" : "Ü",
"ß" : "ß"
}).each(function(map){
str = str.gsub(map.value, map.key)
})
}
try {
d.innerHTML = str;
} catch (ee) {
d.textContent = str;
}
}
return d.innerHTML;
}
/**
* Return the value in the record
* for a given query.
* @param {Object} record Record
* @param {Object} query Query to extract the value (e.g. "rep.title")
*/
Signavio.Utils.extractValue = function(record, query){
if( !record || !query ){ return null }
// Split data field
var o = query.split(".");
// Get value
var val = record instanceof Ext.data.Record ? record.get(o[0]) : record[o[0]];
var i = 0;
// Iterate over value since there is no
// value of the end is reached
while( val && ++i < o.length ){
val = val[o[i]];
}
return typeof val == "string" ? val.unescapeHTML() : val;
}
/*** SPECIFIC BROWSER FIXES ***/
/**
* Chrome Fixes
*/
if (Ext.isChrome){
(function(){
// Fixes for Chrome Bug
// http://code.google.com/p/chromium/issues/detail?id=58493
// Check if there exists the bug in the current chrome version
// var parseNode = (new DOMParser()).parseFromString("<div ext:qtip='tooltip'></div>", "text/xml");
// if (parseNode.getElementsByTagName("parsererror").length == 0){
// return;
// }
// @overwrite
var inHtml = Ext.DomHelper.insertHtml;
Ext.DomHelper.insertHtml = function(foo, bar, html){
html = html.gsub("ext:qtip=", "title=");
html = html.gsub("ext:tree-node-id=", "tree-node-id=");
return inHtml.call(this, foo, bar, html);
};
// Remove namespace awareness of node ids
Ext.tree.TreeEventModel.prototype.getNode = function(e){
var t;
if(t = e.getTarget('.x-tree-node-el', 10)){
var id = Ext.fly(t, '_treeEvents').dom.getAttributeNS(null, 'tree-node-id');
if(id){
return this.tree.getNodeById(id);
}
}
return null;
};
// Fix use of ext namespaces
Ext.Template.prototype.overwrite = function(el, values, returnElement){
el = Ext.getDom(el);
el.innerHTML = this.applyTemplate(values).gsub(" ext:qtip=", " title=");
return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
};
}());
}
}()