utils.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * utils.js
  3. * (c) 2009 Signavio GmbH
  4. *
  5. * @author Willi Tscheschner
  6. *
  7. * Utils is a shared javascript file which provides common
  8. * functionalities for all applications in the Signavio Process Editor
  9. *
  10. */
  11. // define namespace
  12. if(!Signavio){ var Signavio = {} };
  13. if(!Signavio.Utils){ Signavio.Utils = {} };
  14. new function(){
  15. /**
  16. * Splits the text by " " and \n and adds those
  17. * strings to the resulting set of the tokenization
  18. * @param {String} text The text which should be tokenized
  19. */
  20. Signavio.Utils.tokenize = function(text) {
  21. var token = text.split(" ");
  22. token = token.map(function(t, i){ return i == 0 ? t : [" ", t]}).flatten();
  23. token = token.map(function(t){
  24. var tn= t.split("\n");
  25. return tn.map(function(tt, i){ return i == 0 ? tt : ["\n", tt]}).flatten();
  26. }).flatten();
  27. return token.findAll(function(t){ return t});
  28. }
  29. /**
  30. * Strips all none letter and digits
  31. * out of the string
  32. *
  33. * @param {String} phrase
  34. * @return {String}
  35. */
  36. Signavio.Utils.strip = function(phrase){
  37. return phrase.toLowerCase()
  38. .replace(/[^a-zäöüß0-9]/g, '')
  39. .replace(/ü/g, 'u')
  40. .replace(/ä/g, 'a')
  41. .replace(/ö/g, 'o')
  42. }
  43. /**
  44. * Returns true if a is in b included or other way around.
  45. *
  46. * @param {String} a
  47. * @param {String} b
  48. * @return {boolean}
  49. */
  50. Signavio.Utils.isEqualTerms = function(a, b){
  51. // Check if initial values are equal
  52. if (a && b && (a.include(b) || b.include(a))){
  53. return true;
  54. }
  55. // Strip
  56. a = this.strip(a);
  57. b = this.strip(b);
  58. // Check if included
  59. return a && b && (a.include(b) || b.include(a));
  60. }
  61. /**
  62. * Replace all common stings from string1 with those
  63. * which are in the string2
  64. *
  65. * @param {String} string1
  66. * @param {String} string2
  67. * @param {int} cursor (Optional)
  68. */
  69. Signavio.Utils.replaceCommonSubString = function(string1, string2, cursor) {
  70. // If string1 and string2 includes each other after stripingv
  71. var s1 = this.strip(string1);
  72. var s2 = this.strip(string2);
  73. if(s1 === s2 || s2.include(s1)){
  74. return string2;
  75. }
  76. // Get cursor pos
  77. var pos = cursor === undefined ? string1.length : cursor;
  78. // Tokenize
  79. var textT = this.tokenize(string1);
  80. var token = string2.split(/\s+/g); // Replace all special chars and split by " "
  81. // Get index of the token where the cursor is
  82. var selectedIndex = textT.indexOf(textT.find(function(t){ pos -= t == "\n" ? 0 : (t == " " ? 1 : t.length+1) ; return pos <= 0}));
  83. selectedIndex = Math.max(selectedIndex , 0);
  84. var rounds = textT.length;
  85. var found = false;
  86. // Go from the selected index to 0 and then up from the length of the textT to the selected index, but only one round
  87. for (var i=selectedIndex; i != selectedIndex+1 && rounds >= 0; i == 0 ? i = textT.length -1 : --i, --rounds ){
  88. // If current text phrase is empty, go furhter
  89. if (textT[i] == " " || textT[i] == "\n") { continue }
  90. // Find the token from the suggestion
  91. var tok = token.find(function(t){ return this.isEqualTerms(t, textT[i]) }.bind(this));
  92. // If there is a suggested phrases
  93. if (tok) {
  94. found = true;
  95. // Partition all words to word which occur before and after this word
  96. var front = token.slice(0, token.indexOf(tok)).reverse();
  97. var back = token.slice(token.indexOf(tok)+1);
  98. // Replace current phrase with the word
  99. textT[i] = tok;
  100. var lastKnownIndex = i;
  101. // Go through every word which occurs before
  102. for (var j=i; j>=0; --j){
  103. if (textT[j] == " " || textT[j] == "\n") { continue }
  104. tok = front.find(function(t){ return this.isEqualTerms(t, textT[j]) }.bind(this));
  105. if (tok) {
  106. var index = front.indexOf(tok);
  107. textT[lastKnownIndex] = front.slice(0,index).reverse().join(" ") +(index >0?" ":"")+ textT[lastKnownIndex];
  108. lastKnownIndex = j;
  109. textT[j] = tok;
  110. front= front.slice(index+1);
  111. }
  112. }
  113. if (front.length >0){
  114. textT[lastKnownIndex] = front.reverse().join(" ") + " " + textT[lastKnownIndex];
  115. }
  116. var lastKnownIndex = i;
  117. // Go through every word which occurs after
  118. for (var j=i; j<textT.length; j++){
  119. if (textT[j] == " " || textT[j] == "\n") { continue }
  120. tok = back.find(function(t){ return this.isEqualTerms(t, textT[j]) }.bind(this));
  121. if (tok) {
  122. var index = back.indexOf(tok);
  123. textT[lastKnownIndex] += (index>0?" ":"")+back.slice(0, index).join(" ");
  124. lastKnownIndex = j;
  125. textT[j] = tok;
  126. back = back.slice(index+1);
  127. }
  128. }
  129. if (back.length >0){
  130. textT[lastKnownIndex] += " "+back.join(" ");
  131. }
  132. break;
  133. }
  134. if (selectedIndex===0&&textT.length===1){break;}
  135. }
  136. var nString1 = textT.join("");
  137. if (nString1 == string1 && !found) {
  138. nString1 += (nString1.endsWith(" ") ? "" : " ") + string2;
  139. }
  140. return nString1;
  141. }
  142. /**
  143. * Returns a string which is
  144. * HTML unescaped.
  145. *
  146. * @param {String} str
  147. * @return {String}
  148. */
  149. Signavio.Utils.unescapeHTML = function(str){
  150. str = str || "";
  151. var d = document.createElement("div");
  152. try {
  153. d.innerHTML = str;
  154. } catch (e){
  155. d.textContent = str;
  156. }
  157. var nstr = d.textContent || d.innerText || "";
  158. // Unescape the unecaped string till no changes are there
  159. return nstr && str && nstr !== str ? Signavio.Utils.unescapeHTML(nstr) : nstr;
  160. }
  161. /**
  162. * Returns a string which is
  163. * HTML escaped.
  164. *
  165. * @param {String} str
  166. * @return {String}
  167. */
  168. Signavio.Utils.escapeHTML = function(str){
  169. str = str || "";
  170. var d = document.createElement("div");
  171. try {
  172. d.innerHTML = str;
  173. } catch (e){
  174. if (!!str.match(/&[aAoOuU]uml;/g)||!!str.match(/&szlig;/g)){
  175. $H({
  176. "ä" : "&auml;",
  177. "Ä" : "&Auml;",
  178. "ö" : "&ouml;",
  179. "Ö" : "&Ouml;",
  180. "ü" : "&uuml;",
  181. "Ü" : "&Uuml;",
  182. "ß" : "&szlig;"
  183. }).each(function(map){
  184. str = str.gsub(map.value, map.key)
  185. })
  186. }
  187. try {
  188. d.innerHTML = str;
  189. } catch (ee) {
  190. d.textContent = str;
  191. }
  192. }
  193. return d.innerHTML;
  194. }
  195. /**
  196. * Return the value in the record
  197. * for a given query.
  198. * @param {Object} record Record
  199. * @param {Object} query Query to extract the value (e.g. "rep.title")
  200. */
  201. Signavio.Utils.extractValue = function(record, query){
  202. if( !record || !query ){ return null }
  203. // Split data field
  204. var o = query.split(".");
  205. // Get value
  206. var val = record instanceof Ext.data.Record ? record.get(o[0]) : record[o[0]];
  207. var i = 0;
  208. // Iterate over value since there is no
  209. // value of the end is reached
  210. while( val && ++i < o.length ){
  211. val = val[o[i]];
  212. }
  213. return typeof val == "string" ? val.unescapeHTML() : val;
  214. }
  215. /*** SPECIFIC BROWSER FIXES ***/
  216. /**
  217. * Chrome Fixes
  218. */
  219. if (Ext.isChrome){
  220. (function(){
  221. // Fixes for Chrome Bug
  222. // http://code.google.com/p/chromium/issues/detail?id=58493
  223. // Check if there exists the bug in the current chrome version
  224. // var parseNode = (new DOMParser()).parseFromString("<div ext:qtip='tooltip'></div>", "text/xml");
  225. // if (parseNode.getElementsByTagName("parsererror").length == 0){
  226. // return;
  227. // }
  228. // @overwrite
  229. var inHtml = Ext.DomHelper.insertHtml;
  230. Ext.DomHelper.insertHtml = function(foo, bar, html){
  231. html = html.gsub("ext:qtip=", "title=");
  232. html = html.gsub("ext:tree-node-id=", "tree-node-id=");
  233. return inHtml.call(this, foo, bar, html);
  234. };
  235. // Remove namespace awareness of node ids
  236. Ext.tree.TreeEventModel.prototype.getNode = function(e){
  237. var t;
  238. if(t = e.getTarget('.x-tree-node-el', 10)){
  239. var id = Ext.fly(t, '_treeEvents').dom.getAttributeNS(null, 'tree-node-id');
  240. if(id){
  241. return this.tree.getNodeById(id);
  242. }
  243. }
  244. return null;
  245. };
  246. // Fix use of ext namespaces
  247. Ext.Template.prototype.overwrite = function(el, values, returnElement){
  248. el = Ext.getDom(el);
  249. el.innerHTML = this.applyTemplate(values).gsub(" ext:qtip=", " title=");
  250. return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
  251. };
  252. }());
  253. }
  254. }()