1 /*
  2  * 	Copyright (C) 2012-2013 DFKI GmbH
  3  * 	Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4  * 	German Research Center for Artificial Intelligence
  5  * 	http://www.dfki.de
  6  * 
  7  * 	Permission is hereby granted, free of charge, to any person obtaining a 
  8  * 	copy of this software and associated documentation files (the 
  9  * 	"Software"), to deal in the Software without restriction, including 
 10  * 	without limitation the rights to use, copy, modify, merge, publish, 
 11  * 	distribute, sublicense, and/or sell copies of the Software, and to 
 12  * 	permit persons to whom the Software is furnished to do so, subject to 
 13  * 	the following conditions:
 14  * 
 15  * 	The above copyright notice and this permission notice shall be included 
 16  * 	in all copies or substantial portions of the Software.
 17  * 
 18  * 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 19  * 	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 20  * 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 21  * 	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 22  * 	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 23  * 	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 24  * 	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 25  */
 26 
 27 
 28 define(['parsingResult', 'storageUtils'],
 29 	//this comment is needed by jsdoc2 [copy of comment for: function YieldDeclaration(...]
 30 	/**
 31 	 * The YieldDeclaration class holds the name of the yield-declaration (which is a place-holder for the contentFor-fields and is used in the layouts: content, header, footer, dialogs, ...)
 32 	 * and its starting and ending position within the content-definition.
 33 	 * 
 34 	 * @param {Object} parsingElement with properties <code>name</code> {String}, <code>start</code> {Integer}, <code>end</code> {Integer}
 35 	 * @param {Integer} contentAreaType the type of the content area within the layout that this yield-declaration refers to (e.g. ViewConstants.CONTENT_AREA_BODY )
 36 	 * 
 37 	 * borrows the getValue function from ParsingResult:
 38 	 * 	@requires mmir.parser.ParsingResult#getValue
 39 	 * 
 40 	 * @name YieldDeclaration
 41 	 * @class
 42 	 */
 43 	function(
 44 		ParsingResult, parser
 45 ) {
 46 	/** @scope YieldDeclaration.prototype *///for jsdoc2
 47 	
 48 	//set to @ignore in order to avoid doc-duplication in jsdoc3
 49 	/**
 50 	 * @ignore
 51 	 * 
 52 	 * The YieldDeclaration class holds the name of the yield-declaration (which is a place-holder for the contentFor-fields and is used in the layouts: content, header, footer, dialogs, ...)
 53 	 * and its starting and ending position within the content-definition.
 54 	 * 
 55 	 * @constructs YieldDeclaration
 56 	 * @param {Object} parsingElement with properties <code>name</code> {String}, <code>start</code> {Integer}, <code>end</code> {Integer}
 57 	 * @param {Integer} contentAreaType the type of the content area within the layout that this yield-declaration refers to (e.g. ViewConstants.CONTENT_AREA_BODY )
 58 	 * 
 59 	 * borrows the getValue function from ParsingResult:
 60 	 * 	@requires mmir.parser.ParsingResult#getValue
 61 	 * 
 62 	 */ 
 63 	function YieldDeclaration(parsingElement, contentAreaType){
 64 		
 65 		if(parsingElement){
 66 			this.name     = parsingElement.name;
 67 			this.nameType = parsingElement.nameType;
 68 			
 69 			this.start    = parsingElement.start;
 70 			this.end      = parsingElement.end;
 71 		}
 72 		
 73 		this.contentAreaType = contentAreaType;
 74 	    
 75 		return this;
 76 	}
 77 
 78 
 79 
 80 	/**
 81 	 * Gets the name of a {@link mmir.YieldDeclaration} object (e.g. content, header, footer, dialogs, ...).
 82 	 * 
 83 	 * @function
 84 	 * @returns {String} Name - used by yield tags in layout
 85 	 * @public
 86 	 */ 
 87 	YieldDeclaration.prototype.getName = function(){
 88 	    return this.name;
 89 	};
 90 
 91 	YieldDeclaration.prototype.getNameType = function(){
 92 	    return this.nameType;
 93 	};
 94 
 95 	/**
 96 	 * Gets the type of the content area that this {@link mmir.YieldDeclaration} object refers to (i.e. "areas" in the layout, e.g. bodyContents, dialogsContent).
 97 	 * 
 98 	 * @function
 99 	 * @returns {Integer} Content area type (see {@link mmir.Layout}, e.g. ViewConstants.CONTENT_AREA_BODY)
100 	 * @public
101 	 */ 
102 	YieldDeclaration.prototype.getAreaType = function(){
103 	    return this.contentAreaType;
104 	};
105 
106 	/**
107 	 * Gets the start position (index) of a {@link mmir.YieldDeclaration} object.
108 	 * 
109 	 * @function
110 	 * @returns {Integer} Start position of the Yield within the content (e.g. the bodyContent or the dialogsContent)
111 	 * @public
112 	 */ 
113 	YieldDeclaration.prototype.getStart = function(){
114 	    return this.start;
115 	};
116 
117 	/**
118 	 * Gets the end position (index) of a {@link mmir.YieldDeclaration} object.
119 	 * 
120 	 * @function
121 	 * @returns {Integer} End position of the Yield within the content (e.g. the bodyContent or the dialogsContent)
122 	 * @public
123 	 */ 
124 	YieldDeclaration.prototype.getEnd = function(){
125 	    return this.end;
126 	};
127 	
128 
129 	/**
130 	 * Get the value for property <code>name</code> with the proper type
131 	 * (as specified by <code>nameType</code>).
132 	 * This may be neccessary, if the nameType is not e.g. STRING but a VARIABLE, 
133 	 * in which case <code>name</code> does not reference the value itself, but the name 
134 	 * for the variable
135 	 * 
136 	 * This is a shortcut to the function 
137 	 * mobileDS.parser.ParsingResult.prototype.getValue
138 	 * 
139 	 * I.e. for YieldDeclration yield with a nameType of VARIABLE, to not use:
140 	 * <s><code>yield.getName()</code></s>
141 	 * 
142 	 * but
143 	 * 
144 	 * <code>yield.getValue(yield.getName(), yield.getNameType(), theRenderingData)</code>
145 	 * where theRenderingData is an object that contains a property from which the variable value can be retrieved, i.e.
146 	 * where <em>theRenderingData[yield.getName()]</em> contains the YieldDeclaration's name.
147 	 * 
148 	 */
149 	YieldDeclaration.prototype.getValue = ParsingResult.prototype.getValue; 
150 
151 	YieldDeclaration.prototype.stringify = function(){
152 		
153 		// "plain properties" list
154 		var propList = [
155 	   	     'name', 
156 	   	     'nameType',
157 	   	     'start',
158 	   	     'end',
159 	   	     'contentAreaType'
160 	   	];
161 
162 	   	//function for iterating over the property-list and generating JSON-like entries in the string-buffer
163 	   	var appendStringified = parser.appendStringified;
164 	   	
165 	   	var sb = ['require("storageUtils").restoreObject({ classConstructor: "yield"', ','];
166 	   	
167 	   	appendStringified(this, propList, sb);
168 	   	
169 	   	//if last element is a comma, remove it
170 	   	if(sb[sb.length - 1] === ','){
171 	   		sb.splice( sb.length - 1, 1);
172 	   	}
173 	   	
174 	   	sb.push(' })');
175 	   	return sb.join('');
176 	};
177 		
178 	return YieldDeclaration;
179 	
180 });
181