1 define( 2 /** 3 * Set to "backwards compatibility mode" (for pre version 2.0) for CommonUtils. 4 * 5 * This function re-adds deprecated and removed functions and 6 * properties to the CommonUtils instance. 7 * 8 * NOTE that once set to compatibility mode, it cannot be reset to 9 * non-compatibility mode. 10 * 11 * <p> 12 * In addition, the following functions of CommonUtils are made accessible 13 * on the <code>mmir.CommonUtils</code> instance with these additional names 14 * <ul> 15 * <li> {@link mmir.CommonUtils#regexHTMLComment} as 16 * <b><u>html_comment_regex : RegExpr</u></b> 17 * </li><li> 18 * {@link mmir.CommonUtils#resizeFitToSourroundingBox} as 19 * <b><u>html_resize_font_to_fit_surrounding_box()</u></b> 20 * </li><li> 21 * {@link mmir.CommonUtils#toJSONStringValue} as 22 * <b><u>to_json_string_value(String: theObjectValue) : String</u></b> 23 * </li><li> 24 * {@link mmir.CommonUtils#convertJSONStringValueToHTML} as 25 * <b><u>convert_to_json_value_HTML_string(String: str) : String</u></b> 26 * </li><li> 27 * {@link mmir.CommonUtils#convertJSONStringToHTML} as 28 * <b><u>convert_json_to_HTML_string(String: str) : String</u></b> 29 * </li><li> 30 * {@link mmir.CommonUtils#parseParamsToDictionary} as 31 * <b><u>get_params_as_dict(String: str) : Object</u></b> 32 * </li> 33 * </ul> 34 * 35 * @requires document (DOM element) 36 * 37 * @param {mmir.CommonUtils} compatibilitySelf 38 * the instance of mmir.CommonUtils to which the compatibility functions etc. 39 * will be attached 40 * 41 * @class 42 * @name mmir.CommonUtils.setToCompatibilityModeExtension 43 * @static 44 * 45 * @example 46 * require(['commonUtilsCompatibility'], function(setCompatibility){ 47 * setCompatibility(mmir.CommonUtils); 48 * }); 49 * 50 * @requires document (DOM element) 51 * 52 * @public 53 */ 54 function( 55 ) { 56 57 /** 58 * Set to "backwards compatibility mode" (for pre version 2.0). 59 * 60 * This function re-adds deprecated and removed functions and 61 * properties to the CommonUtils instance. 62 * 63 * NOTE that once set to compatibility mode, it cannot be reset to 64 * non-compatibility mode. 65 * 66 * @requires document (DOM element) 67 * 68 * @param {mmir.CommonUtils} compatibilitySelf 69 * the instance of mmir.CommonUtils to which the compatibility functions etc. 70 * will be attached 71 * 72 * @constructs mmir.CommonUtils.setToCompatibilityModeExtension 73 * 74 * @borrows mmir.CommonUtils#regexHTMLComment as 75 * html_comment_regex 76 * @borrows mmir.CommonUtils#resizeFitToSourroundingBox as 77 * this.html_resize_font_to_fit_surrounding_box 78 * @borrows mmir.CommonUtils#toJSONStringValue as 79 * this.to_json_string_value 80 * @borrows mmir.CommonUtils#convertJSONStringValueToHTML as 81 * this.convert_to_json_value_HTML_string 82 * @borrows mmir.CommonUtils#convertJSONStringToHTML as 83 * this.convert_json_to_HTML_string 84 * @borrows mmir.CommonUtils#parseParamsToDictionary as 85 * this.get_params_as_dict 86 */ 87 return setToCompatibilityMode = function(compatibilitySelf) { 88 89 /** @scope mmir.CommonUtils.setToCompatibilityModeExtension.prototype *///for jsdoc2 90 91 92 /** 93 * Array of strings for the conversion of month represented by integers 94 * to strings Default Language for months is english, 'en' 95 * 96 * @type Object 97 * @private 98 */ 99 compatibilitySelf.months = { 100 '01': 'January', 101 '02': 'February', 102 '03': 'March', 103 '04': 'April', 104 '05': 'May', 105 '06': 'June', 106 '07': 'July', 107 '08': 'August', 108 '09': 'September', 109 '10': 'October', 110 '11': 'November', 111 '12': 'December' 112 }; 113 /** 114 * @private 115 */ 116 compatibilitySelf.months.de = { 117 '01': 'Januar', 118 '02': 'Februar', 119 '03': 'M\u00E4rz',//'März', 120 '04': 'April', 121 '05': 'Mai', 122 '06': 'Juni', 123 '07': 'Juli', 124 '08': 'August', 125 '09': 'September', 126 '10': 'Oktober', 127 '11': 'November', 128 '12': 'Dezember' 129 }; 130 131 // /** 132 // * The instance that holds the extensions for compatibility 133 // * mode, which really is the CommonUtils instance. 134 // * 135 // * @type 136 // * @private 137 // */ 138 // var compatibilitySelf = this; 139 140 /** 141 * HTML-Dom-Element for logging directly on the main HTML-Page 142 * as of now there is no element with the id "log" in the 143 * index.html 144 * 145 * @type Element 146 * @private 147 * @deprecated unused 148 */ 149 var debugNode = document.getElementById("log"); 150 151 /** 152 * Regular Expression to identify a styleSheet-tag for the 153 * transformation of ehtml to html 154 * 155 * @type String|RegExp 156 * @private 157 * @deprecated unused 158 */ 159 var styleSheetRegExp = /<(%=\s*stylesheet_link_tag)\s* (\"(.*)\" %)>/; 160 161 /** 162 * Regular Expression to identify a javascript for the 163 * transformation of ehtml to html 164 * 165 * @type String|RegExp 166 * @private 167 * @deprecated unused 168 */ 169 var javaScriptRegExp = /<(%=\s*javascript_include_tag)\s* (\"(.*)\" %)>/; 170 171 /** 172 * Regular Expression to identify content for a view-element:<br> 173 * either _header_, _footer_, _dialogs_ or _content_ 174 * 175 * @deprecated old template syntax format 176 * 177 * @type String|RegExp 178 * @private 179 */ 180 var contentForRegExp = /<%\s*content_for\s*:([^\s]*)\s*do\s*%>(([\s|\n]*.*[\s|\n]*)*)<%\s*end\s*%>/i; 181 182 /** 183 * Regular Expression to identify if a partial should be 184 * rendered inside a view (ehtml-String) 185 * 186 * @deprecated old template syntax format 187 * 188 * @type String|RegExp 189 * @private 190 */ 191 var renderPartialRegExp = /<%\s*render\s*([^\s]*)\s*\{\}\s*%>/i; 192 193 /** 194 * Regular Expression for matching a translation-tag for the 195 * localization of view content (ehtml-String) 196 * 197 * @deprecated old template syntax format 198 * 199 * @type String|RegExp 200 * @private 201 */ 202 var translationRegExpString = '<%t\\s*:([^\\s]*)\\s*%>'; 203 204 // /** 205 // * The Prefix for the names of view-files - currently unused 206 // and deprecated. 207 // * 208 // * @type String 209 // * @private 210 // * @deprecated has no further value 211 // */ 212 // var viewsPrefix = '#'; 213 214 /** 215 * See Property: 216 * {@link #setToCompatibilityModeExtension-render_partial_regex} 217 * <br> 218 * This regular expression is an extension for the parsing of 219 * the parameters of the partial (for customization) to get the 220 * name of the corresponding controller of the partial.<br> 221 * 222 * Regular Expression to identify if a partial is to be inserted 223 * in a view. <br> 224 * Partials are in principle customizable views, which can be 225 * used independently from a controller and furthermore accept 226 * parameters to customize the partial.<br> 227 * A partial is first processed and then integrated into the 228 * view. 229 * 230 * @deprecated old template syntax format 231 * 232 * @example <% render googlemap/poi_details {:curr_poi_data_jpath => new JPath(mmir.ControllerManager.getInstance().getController("googlemap").script['current_poi_meta_data'])} %> 233 * @type String|RegExp 234 * @public 235 */ 236 var partial_name_regex = /^([^\/]+)\/(.+)$/i; 237 compatibilitySelf.partial_name_regex = partial_name_regex; 238 239 /** 240 * Regular expression for the parsing of partial-files.<br> 241 * This expression detects all variables and data-instructions 242 * for the customization of the partial. There are 3 types of 243 * variables or instructions: 244 * + <b>if-else-statement</b>, controls which part of the 245 * partial will be displayed - depending on the condition + 246 * <b>data-instruction</b>, which is evaluated, but not 247 * displayed + <b>variable</b> or <b>javascript-code</b>, 248 * which are evaluated and displayed in the view 249 * 250 * Partials are principally customizable views, which can be 251 * used independently from a controller and furthermore accept 252 * parameters to customize the partial.<br> 253 * A partial is first processed and then integrated into the 254 * view. 255 * 256 * @deprecated old template syntax format 257 * 258 * @example {::address = address + " " + {:curr_poi_data}.query('addressBean/housenumber')} 259 * @type String|RegExp 260 * @public 261 */ 262 var partial_var_pattern_regex = /(\{[^\}\{]+\})|(\{[^\{]*(\{(?=[^\}]*\}).*)\})/gmi; 263 compatibilitySelf.partial_var_pattern_regex = partial_var_pattern_regex; 264 265 /** 266 * Regular expression for the parsing of partial-files.<br> 267 * This expression detects all simple variables for the 268 * customization of the partial in the form of 269 * <b>{:curr_poi_data}</b>.<br> 270 * Form of <b>simple object</b>: <b>{:SIMPLE_OBJECT}</b><br> 271 * 272 * Partials are principally customizable views, which can be 273 * used independently from a controller and furthermore accept 274 * parameters to customize the partial.<br> 275 * A partial is first processed and then integrated into the 276 * view. 277 * 278 * 279 * @deprecated old template syntax format 280 * 281 * @example {:curr_poi_data} 282 * @type String|RegExp 283 * @public 284 */ 285 var partial_var_pattern_simpleobject_regex = /\{:([^\}]+)\}/; 286 compatibilitySelf.partial_var_pattern_simpleobject_regex = partial_var_pattern_simpleobject_regex; 287 288 /** 289 * Regular expression for the parsing of partial-files.<br> 290 * This expression detects all <b>data objects</b> for the 291 * customization of the partial in the form of 292 * <b>{::curr_poi_data={:curr_poi_data_jpath}}</b>.<br> 293 * Form of <b>data object</b>: <b>{::DATA_OBJECT}</b><br> 294 * 295 * Partials are principally customizable views, which can be 296 * used independently from a controller and furthermore accept 297 * parameters to customize the partial.<br> 298 * A partial is first processed and then integrated into the 299 * view. 300 * 301 * 302 * @deprecated old template syntax format 303 * 304 * @example {::address = address + "<br/>"} 305 * @type String|RegExp 306 * @public 307 */ 308 var partial_var_pattern_dataobject_regex = /\{::([^\}\{]+)\}|\{::([^\{]*(?:\{(?:[^\}]*\}).*))\}/ig; 309 compatibilitySelf.partial_var_pattern_dataobject_regex = partial_var_pattern_dataobject_regex; 310 311 /** 312 * Regular expression for detecting an assignment expression in 313 * templates, e.g. <code>{::theVariable=... }</code>. 314 * 315 * 316 * @deprecated old template syntax format 317 * 318 * @example {::address = address + "<br/>"} 319 * or 320 * <code>{::address = {:anotherVariable} + "<br/>"}</code> 321 * @type String|RegExp 322 * @public 323 */ 324 var partial_var_pattern_assignment_regex = /\{::([^\}\{=]+)=([^\}\{]+)\}|\{::([^\}\{=]+)=([^\{]*(?:\{(?:[^\}]*\}).*))\}/ig; 325 compatibilitySelf.partial_var_pattern_assignment_regex = partial_var_pattern_assignment_regex; 326 327 // /** 328 // * Deprecated regular expression for partials. 329 // * @type String|RegExp 330 // * @public 331 // * @deprecated unused 332 // */ 333 // var partial_var_pattern_object_with_function_regex = 334 // /\{?([^\.]+)([\.\[])([^\s\}]+)()/ig 335 // compatibilitySelf.partial_var_pattern_object_with_function_regex 336 // = partial_var_pattern_object_with_function_regex; 337 338 /** 339 * Regular Expression to identify content in a view that will be 340 * inserted.<br> 341 * The content is generated by a helper function of the 342 * controller and usually saved as a JSON-Object with a _helper_ 343 * and _content_ part.<br> 344 * If the string is escaped and must be unescaped a second 345 * parameter can be given to ensure that the string will be 346 * unescaped before the insertion in the view. 347 * 348 * @deprecated old template syntax format 349 * 350 * @example <%= value_of(languageMenu::header, true) %> 351 * @type String|RegExp 352 * @public 353 */ 354 var value_of_regex = /<%=\s*value_of\s*\(([^\)]*)\)\s*%>/igm; 355 compatibilitySelf.value_of_regex = value_of_regex; 356 357 /** 358 * See Property: 359 * {@link #setToCompatibilityModeExtension-value_of_regex} 360 * <br> 361 * This regular expression is an extension to parse the 362 * parameters of the <b>value_of</b>-function.<br> 363 * 364 * Regular Expression to identify content in a view that will be 365 * inserted.<br> 366 * The content is generated by a helper function of the 367 * controller and usually saved as a JSON-Object with a _helper_ 368 * and _content_ part.<br> 369 * If the string is escaped and must be unescaped a second 370 * parameter can be given to ensure that the string will be 371 * unescaped before the insertion in the view. 372 * 373 * @deprecated old template syntax format 374 * 375 * @example <%= value_of(languageMenu::header, true) %> 376 * @type String|RegExp 377 * @public 378 */ 379 var value_of_path_regex = /\(\s*([^\),]*),?\s*([^\)]*)\s*\)/i; 380 compatibilitySelf.value_of_path_regex = value_of_path_regex; 381 382 /** 383 * Regular Expression to identify if a partial is to be inserted 384 * in a view. <br> 385 * Partials are in principle customizable views, which can be 386 * used independently from a controller and furthermore accept 387 * parameters to customize the partial.<br> 388 * A partial is first processed and then integrated into the 389 * view. 390 * 391 * @deprecated old template syntax format 392 * 393 * @example <% render googlemap/poi_details {:curr_poi_data_jpath => new JPath(mmir.ControllerManager.getInstance().getController("googlemap").script['current_poi_meta_data'])} %> 394 * @type String|RegExp 395 * @public 396 */ 397 var render_partial_regex = /<%\s*render\s*([^\s]*)\s*\{([^\}]*)\}\s*%>/igm; 398 compatibilitySelf.render_partial_regex = render_partial_regex; 399 400 /** 401 * See Property: 402 * {@link #setToCompatibilityModeExtension-render_partial_regex} 403 * <br> 404 * This regular expression is an extension for the parsing of 405 * the parameters of the partial (for customization).<br> 406 * 407 * Regular Expression to identify if a partial is to be inserted 408 * in a view. <br> 409 * Partials are in principle customizable views, which can be 410 * used independently from a controller and furthermore accept 411 * parameters to customize the partial.<br> 412 * A partial is first processed and then integrated into the 413 * view. 414 * 415 * @deprecated old template syntax format 416 * 417 * @example <% render googlemap/poi_details {:curr_poi_data_jpath => new JPath(mmir.ControllerManager.getInstance().getController("googlemap").script['current_poi_meta_data'])} %> 418 * @type String|RegExp 419 * @public 420 */ 421 var partial_parameter_regex = /\s*:(\S*)\s*=>\s*(("([\S ]+)")|([^,]+))/i; 422 compatibilitySelf.partial_parameter_regex = partial_parameter_regex; 423 424 /** 425 * Appends a log-message to the main document (index.html) and 426 * prints it in the console 427 * 428 * @function 429 * @param {String} 430 * clazz A prefix for the output of the log message 431 * in the console 432 * @param {String} 433 * logMessage The log message which should be printed 434 * @public 435 * @deprecated 436 */ 437 var log = function(clazz, logMessage) { 438 debugNode = document.getElementById("log"); 439 if (debugNode) { 440 debugNode.innerHTML += "<pre>\n" + logMessage + "\n</pre>\n"; 441 } 442 console.log(clazz + ":" + logMessage); 443 }; 444 compatibilitySelf.log = log; 445 446 /** 447 * Function which transforms a ehtml string (while parsing 448 * views) into html by replacing stylesheet-, javascript- and 449 * content_for-tags with corresponding contents. 450 * 451 * 452 * @deprecated used for parsing/rendering old template syntax 453 * format 454 * 455 * @function 456 * @param {String} 457 * eHtmlTag A string that should be transformed from 458 * ehtml to html 459 * @public 460 * @returns {String} From ehtml into html transformed string 461 */ 462 var ehtml2Html = function(eHtmlTag) { 463 464 var result; 465 if (eHtmlTag.match(styleSheetRegExp)) { 466 var group = eHtmlTag.match(styleSheetRegExp); 467 result = eHtmlTag.replace(group[1], "link rel=\"stylesheet\" ").replace(group[2], "href=\"content/stylesheets/" + group[3] + ".css\"/"); 468 }else if (eHtmlTag.match(javaScriptRegExp)) { 469 var group = eHtmlTag.match(javaScriptRegExp); 470 result = eHtmlTag.replace(group[1], "script type=\"text/javascript\" charset=\"utf-8\" ").replace(group[2], "src=\"" + group[3] + ".js\"></script"); 471 }else if (eHtmlTag.match(contentForRegExp)) { 472 var group = eHtmlTag.match(contentForRegExp); 473 return group; 474 } 475 else { 476 return eHtmlTag; 477 } 478 479 return result; 480 }; 481 compatibilitySelf.ehtml2Html = ehtml2Html; 482 483 /** 484 * Similar to the jQuery.getScript() function - appending a url 485 * of a javascript-source to the header of the main document. 486 * 487 * @function 488 * @param {String} 489 * scriptSrc source of javascript-file 490 * @public 491 * @deprecated superseded by getLocalScript 492 */ 493 var appendJsSrcToHeader = function(scriptSrc) { 494 // appends '<script src=scriptSrc type = 495 // "text/javascript"></script>' to header 496 // thus loading it dynamically 497 var newScript = document.createElement('script'); 498 newScript.type = "text/javascript"; 499 newScript.src = scriptSrc; 500 document.head.appendChild(newScript); 501 }; 502 compatibilitySelf.appendJsSrcToHeader = appendJsSrcToHeader; 503 504 // /** 505 // * Get the prefix for views. 506 // * @function 507 // * @public 508 // * @returns {String} The Prefix for the file names of views 509 // * @deprecated This function is unused and superfluous 510 // */ 511 // var compatibilitySelf.getViewsPrefix= function(){ 512 // return viewsPrefix; 513 // }; 514 // compatibilitySelf.getViewsPrefix = getViewsPrefix; 515 516 /** 517 * Gets the Regular Expression for translation tags. 518 * 519 * @function 520 * @public 521 * @returns {String} The regular expression for matching a 522 * translation-tag - used inside a ehtml-String 523 */ 524 var getTranslationRegExp = function() { 525 return new RegExp(translationRegExpString, 'gi'); 526 }; 527 compatibilitySelf.getTranslationRegExp = getTranslationRegExp; 528 529 /** 530 * Reformat the String representation of a date. 531 * 532 * @example converts <code>2012-07-23 16:37:33.0</code> into 533 * → <code>23. July 2012</code> 534 * 535 * @function 536 * @param {String} 537 * the date String in format 538 * <code>yyyy-mm-dd HH:mm:ss.S</code> 539 * @param {String} 540 * <em>[Optional]</em> the language code (currently 541 * used to format the name of the month). Currently 542 * supported languages: <code>en, de</code>. If 543 * unkown or omitted, default <code>en</code> is 544 * used. 545 * @return {String} a new String representation for the date 546 * @public 547 */ 548 var get_date_as_string = function(date, languageCode) { 549 var self = this; 550 var day, month, year; 551 var date_time = date.split(" "); 552 var splited_date = date_time[0].split("-"); 553 year = splited_date[0]; 554 month = splited_date[1]; 555 556 // add leading zero if necessary 557 if (month.length == 1) { 558 month = '0' + month; 559 } 560 day = splited_date[2]; 561 562 var theLanguage = typeof languageCode === 'string'? languageCode.toLowerCase() : null; 563 var monthName; 564 if(theLanguage !== null && languageCode !== 'en' && self.months[theLanguage]){ 565 //get language specific name for month, if possible 566 monthName = self.months[theLanguage][month]; 567 } 568 else { 569 //get default name for month 570 monthName = self.months[month]; 571 } 572 return day +". "+monthName+" "+year; 573 }; 574 compatibilitySelf.get_date_as_string = get_date_as_string; 575 576 /** 577 * Convert a duration (in seconds) into a String representation. 578 * 579 * @example 580 * 2:09:19 h 581 * 12:05 min 582 * 583 * @function 584 * @param {Integer} 585 * the duration in seconds 586 * @return {String} a String representation for the duration 587 * @public 588 */ 589 var get_duration_as_string = function(duration) { 590 var sec = duration % 60; 591 var min = (duration - sec) / 60; 592 593 var hour = 0; 594 if (min > 59) { 595 min = min % 60; 596 hour = ((duration - (min * 60)) - sec) / 3600; 597 } 598 if (sec < 10) { 599 sec = "0" + sec; 600 } 601 if (min < 10) { 602 min = "0" + min; 603 } 604 605 if (hour > 0) { 606 return hour + ":" + min + ":" + sec + " h"; 607 } 608 else { 609 return min + ":" + sec + " min"; 610 } 611 612 }; 613 compatibilitySelf.get_duration_as_string = get_duration_as_string; 614 615 // //////////////////////////////////////////////////////////////////////////// 616 // comp: make renamed functions available under their old name again: 617 618 compatibilitySelf.html_comment_regex = compatibilitySelf.regexHTMLComment; 619 compatibilitySelf.html_resize_font_to_fit_surrounding_box = compatibilitySelf.resizeFitToSourroundingBox; 620 compatibilitySelf.to_json_string_value = compatibilitySelf.toJSONStringValue; 621 compatibilitySelf.convert_to_json_value_HTML_string = compatibilitySelf.convertJSONStringValueToHTML; 622 compatibilitySelf.convert_json_to_HTML_string = compatibilitySelf.convertJSONStringToHTML; 623 compatibilitySelf.get_params_as_dict = compatibilitySelf.parseParamsToDictionary; 624 625 };// END: setToCompatibilityModeExtension 626 627 }); 628