Source: mvc/views/partial.js

  1. define ( ['mmirf/commonUtils','mmirf/contentElement','mmirf/storageUtils','require'],
  2. function (
  3. commonUtils, ContentElement, parser, require
  4. ){
  5. /**
  6. *
  7. * The Partial class is a containing the definition of the partial and methods to access the definition.
  8. *
  9. * @class
  10. * @name Partial
  11. * @memberOf mmir.view
  12. * @param {Object} ctrl
  13. * Controller instance / object
  14. * @param {String} name
  15. * Name of the Partial
  16. * @param {String} definition
  17. * Partial description, i.e. the raw template code that will be processed.
  18. * May be empty: in this case the processed contents must be
  19. * added manually (cf. parser.StorageUtils)
  20. *
  21. * @requires if param definition is NOT empty: parser.RenderUtils (must be loaded beforehand via <code>require(["mmirf/renderUtils"]...</code>)
  22. * @requires if param definition is NOT empty: parser.ParseUtils (must be loaded beforehand via <code>require(["mmirf/parseUtils"]...</code>)
  23. *
  24. */
  25. function Partial(ctrl, name, definition){
  26. // var HTMLCommentRegExp = /<!--[\s\S]*?-->/g;
  27. if(definition){
  28. //remove HTML comments:
  29. definition = definition.replace(commonUtils.regexHTMLComment, '');
  30. }
  31. this.controller = ctrl;
  32. this.def = definition;
  33. this.name = name;
  34. // console.log("[Partial] parsed Partial '" +this.controller + "-"+this.name+ "'.");
  35. if(definition){
  36. var contentElementInfo = {
  37. //this name is purely informational:
  38. name : this.controller.getName() + 'Partial',
  39. content : this.def
  40. };
  41. var parserUtils = typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD? __webpack_require__('mmirf/parseUtils') : require('mmirf/parseUtils');
  42. var renderUtils = typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD? __webpack_require__('mmirf/renderUtils') : require('mmirf/renderUtils');
  43. this.contentElement = new ContentElement(contentElementInfo, this, parserUtils, renderUtils);
  44. }
  45. }
  46. /**
  47. * Gets the definition of a partial.
  48. *
  49. * @function getDefinition
  50. * @returns {String} The partial description string
  51. * @memberOf mmir.view.Partial#
  52. */
  53. Partial.prototype.getDefinition = function(){
  54. return this.def;
  55. };
  56. /**
  57. * Gets the name of a partial.
  58. *
  59. * @function getName
  60. * @returns {String} The name of the partial
  61. * @memberOf mmir.view.Partial#
  62. */
  63. Partial.prototype.getName = function(){
  64. return this.name;
  65. };
  66. /**
  67. * Gets the controller of a partial - each partial is assigned to a specific controller, although they can be used from different controllers.
  68. *
  69. * @function getController
  70. * @returns {Object} The controller of the partial
  71. * @memberOf mmir.view.Partial#
  72. */
  73. Partial.prototype.getController = function(){
  74. return this.controller;
  75. };
  76. /**
  77. * Gets the {@link mmir.view.ContentElement}, i.e. the content that this instance represents.
  78. *
  79. * @function getContentElement
  80. * @returns {mmir.view.ContentElement} The ContentElement object
  81. * @memberOf mmir.view.Partial#
  82. */
  83. Partial.prototype.getContentElement = function(){
  84. return this.contentElement;
  85. };
  86. /**
  87. * Gets the {@link mmir.view.ContentElement}, i.e. the content that this instance represents.
  88. *
  89. * @function stringify
  90. * @memberOf mmir.view.Partial#
  91. *
  92. * @param {Boolean} [disableStrictMode] OPTIONAL disable JavaScript strict mode in the generated view code
  93. * @returns {String} the stringified representation for the Partial
  94. */
  95. Partial.prototype.stringify = function(disableStrictMode){
  96. // "plain properties" list
  97. var propList = [
  98. 'name',
  99. 'def'
  100. ];
  101. //Array-properties
  102. var stringifyablePropList = [
  103. 'contentElement' //element type: ContentElement (stringify-able)
  104. ];
  105. //function for iterating over the property-list and generating JSON-like entries in the string-buffer
  106. var appendStringified = parser.appendStringified;
  107. var moduleNameString = '"'+this.name+this.getController().getName()+'Partial"';
  108. var sb = [parser.getCodeWrapPrefix(disableStrictMode), 'require("mmirf/storageUtils").restoreObject({ classConstructor: "mmirf/partial"', ','];
  109. appendStringified(this, propList, sb);
  110. //non-primitives properties with stringify() function:
  111. appendStringified(this, stringifyablePropList, sb, null, function arrayValueExtractor(name, stringifyableValue){
  112. return stringifyableValue.stringify(disableStrictMode);
  113. });
  114. //NOTE the use of require() here, assumes that the dependency has already been loaded (i.e. has already been request by some other module!)
  115. sb.push( 'initPublish: function(){ require("mmirf/presentationManager").addPartial(this.getController(), this); }');
  116. sb.push(',');
  117. //TODO is there a better way to store the controller? -> by its contoller's name, and add a getter function...
  118. if(this['controller']){
  119. //getter/setter function for controller
  120. // (NOTE: this init-function needs to be called before controller can be accessed!)
  121. sb.push( 'initController: function(){');
  122. // store controller-name:
  123. sb.push( ' var ctrlName = ');
  124. sb.push( JSON.stringify(this.getController().getName()) );
  125. // ... and the getter/setter code:
  126. sb.push( '; this.controller = require("mmirf/controllerManager").get(ctrlName); },' );//TODO see remark about use of require() above
  127. //add initializer function
  128. // (NOTE: needs to be called before controller or renderer can be accessed!)
  129. sb.push( 'init: function(){');
  130. sb.push( ' this.initController(); ' );
  131. sb.push( ' }' );
  132. //NOTE: need to add comma in a separate entry
  133. // (-> in order to not break the removal method of last comma, see below)
  134. sb.push( ',' );
  135. }
  136. //if last element is a comma, remove it
  137. if(sb[sb.length - 1] === ','){
  138. sb.splice( sb.length - 1, 1);
  139. }
  140. //TODO use requirejs mechanism? (see remark above)
  141. // sb.push(' }, true); });\n require([' //<- add require-call, so that this JS-file adds itself to the loaded dependencies in requirejs
  142. // + moduleNameString + ']);');
  143. sb.push(' }, true, '+parser.STORAGE_FILE_FORMAT_NUMBER+');');
  144. sb.push(parser.STORAGE_CODE_WRAP_SUFFIX);
  145. return sb.join('');
  146. };
  147. return Partial;
  148. });