Source: tools/extensions/CommonUtilsV1Compatibility.js

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