1 define(['require'], 
  2     function ( require) {
  3 		/**
  4 	 * @description NexcelGroupList is a Collection of Groups in the workbook, It is a custom class to handle with all the Group in the WorkBook
  5 	 * @name NexcelGroupList
  6 	 * @class NexcelGroupList
  7 	 * @example require('NexcelGroupList')
  8 	 * @exports NexcelGroupList
  9 	 * @version 1.0
 10 	 * @module NexcelGroupList
 11 	 **/
 12 		var NexcelGroup = {
 13 	
 14 			List:function(){
 15 				var Collection = require('Collections');
 16 				var idList=[];
 17 				var names=[];
 18 				var obj=[];
 19 				var id=1;
 20 				var colNameAvailable=[];
 21 				var groupTitleAvailable=[];
 22 				var highlightedGroup = Collection.Set();
 23 				var groupList = {
 24 					/**
 25 					 * @name isExist
 26 					 * @memberOf NexcelGroupList
 27 					 * @method 
 28 					 * @description The function checks whether the given group is available in the GroupList Object , if yest return that object otherwise return false.
 29 					 * @param groupName The name of teh Group.
 30 					 * @return {Object} if the groupNameexists it returns Object, otherwise false. 
 31 					 * @function
 32 					 */
 33 					isExist: function(groupName){
 34 						return names.indexOf(groupName)>=0;
 35 					},
 36 					/**
 37 					 * @name getId
 38 					 * @memberOf NexcelGroupList
 39 					 * @method 
 40 					 * @param groupName The name of the Group.
 41 					 * @return {Integer} if the groupName exists it returns Id of teh Group, otherwise false. 
 42 					 * @function
 43 					 */
 44 					getId:function(groupName){
 45 						return names.indexOf(groupName);
 46 					},
 47 					/**
 48 					 * @name getName
 49 					 * @memberOf NexcelGroupList
 50 					 * @method 
 51 					 * @param groupId The id  of the Group.
 52 					 * @return {Integer} if the groupId exists it returns Name of teh Group, otherwise false. 
 53 					 * @function
 54 					 */
 55 					getName:function(groupId){
 56 						return names[groupId];
 57 					},
 58 					/**
 59 					 * @name isColNameAvailable
 60 					 * @memberOf NexcelGroupList
 61 					 * @method 
 62 					 * @description Checks whether the groups have the header bar.
 63 					 * @param groupId The id  of the Group.
 64 					 * @return {Boolean} if the group have header exists it returns true otherwise false. 
 65 					 * @function
 66 					 */
 67 					isColNameAvailable:function(groupId){
 68 						if(colNameAvailable[groupId]) 
 69 							return true;
 70 						return false;
 71 					},
 72 					
 73 					/**
 74 					 * @name setColNameAvailable
 75 					 * @memberOf NexcelGroupList
 76 					 * @method 
 77 					 * @description It sets whether the First Row is COnsidered as Header, otherwise No header Available.
 78 					 * @param groupId The id  of the Group.
 79 					 * @return {Void} 
 80 					 * @function
 81 					 */
 82 					setColNameAvailable:function(groupId){
 83 						colNameAvailable[groupId] = true;
 84 					},
 85 					
 86 					isGroupTitleAvailable:function(groupId){
 87 						if(groupTitleAvailable[groupId]) return true ; 
 88 						return false;
 89 					},
 90 					setGroupTitleAvailable:function(groupId){
 91 						groupTitleAvailable[groupId] = true;
 92 					},
 93 					/**
 94 					 * It sets whether the Group is Highlighted in the Sheet
 95 					 */
 96 					highlightGroup:function(groupId){
 97 						highlightedGroup.add(groupId);
 98 					},
 99 					isGroupHighlighted:function(groupId){
100 						return highlightedGroup.contains(groupId);
101 					},
102 					removeGroupHighlight:function(groupId){
103 						highlightedGroup.remove(groupId);
104 					},
105 					getAllHighlighedGroup:function(){
106 						return highlightedGroup.getElements();
107 					},
108 					
109 					getGroupObjByName:function(groupName){
110 						if (names.indexOf(groupName) >= 0){
111 							return obj[names.indexOf(groupName)];
112 						}
113 					},
114 					getGroupObjById:function(groupId){
115 						return obj[groupId];
116 					},
117 					/**
118 					 * @name getGroupObjListOnSheet
119 					 * @memberOf NexcelGroupList
120 					 * @method 
121 					 * @description Returns a List of Groups in a Specific Sheet Id.
122 					 * @param sheetId The id  of the sheet.
123 					 * @return {ArrayList} A List of GroupObjects in the sheet 
124 					 * @function
125 					 */
126 					getGroupObjListOnSheet:function(sheetId){
127 						var groupList=new Collection.ArrayList();
128 						for(var i=0; i<idList.length; i++){
129 							if(obj[idList[i]].sheetId == sheetId)
130 								groupList.add(obj[idList[i]]);
131 						}
132 						return groupList;
133 					},
134 					
135 					/**
136 					 * @name getGroupObjListOnWorkBook
137 					 * @memberOf NexcelGroupList
138 					 * @method 
139 					 * @description Returns a List of Groups in a entire WorkBook.
140 					 * @return {ArrayList} A List of GroupObjects in the sheet 
141 					 * @function
142 					 */
143 					getGroupObjListOnWorkBook:function(){
144 						var groupList=new Collection.ArrayList();
145 						for(var i=0; i<idList.length; i++){
146 							groupList.add(obj[idList[i]]);
147 						}
148 						return groupList;
149 					},
150 					/**
151 					 * @name add
152 					 * @memberOf NexcelGroupList
153 					 * @method 
154 					 * @description Adds a new group to the GroupList Collection. The global value of id will be incremented by one to keep new value for upcoming groups.
155 					 * @param groupId The group Id received from database.
156 					 * @param groupName The name of teh Group
157 					 * @param details It contains all teh details of teh group.like start row, column, header etc..
158 					 * @function
159 					 */
160 					add:function(groupId, groupName, details){
161 						names[groupId]=groupName;
162 						obj[groupId]=details;
163 						idList.push(groupId);
164 						if(details.colNameAvailable == 1)
165 							colNameAvailable[groupId] = true;
166 						if(details.groupTitle == 1)
167 							groupTitleAvailable[groupId] = true;
168 						details.headerPinned = parseInt(details.headerPinned);
169 						id = id +1;
170 					},
171 					
172 					getIdList:function(){
173 						return idList;
174 					},
175 					/**
176 					 * @name getNameList
177 					 * @memberOf NexcelGroupList
178 					 * @method 
179 					 * @description Returns a List of Groups Names in a entire WorkBook.
180 					 * @return {ArrayList} A List of GroupObjects in the sheet 
181 					 * @function
182 					 */
183 					getNameList:function(){
184 						var nameList = Collection.ArrayList();
185 						for(var i=0; i<idList.length; i++){
186 							nameList.add(this.getName(idList[i]));
187 						}
188 						return nameList;
189 					},
190 					/**
191 					 * @name getNamesSortByNameLength
192 					 * @memberOf NexcelGroupList
193 					 * @method 
194 					 * @description Returns a List of Groups Names in a entire WorkBook sorted by Name length.
195 					 * @return {ArrayList} A List of Group names in the sheet 
196 					 * @function
197 					 */
198 					getNamesSortByNameLength:function(){
199 						return this.getNameList().getElements().sort(function(a,b){return b.length-a.length;});
200 					},
201 					/**
202 					 * @name getNamesSortByName
203 					 * @memberOf NexcelGroupList
204 					 * @method 
205 					 * @description Returns a List of Groups Names in a entire WorkBook sorted by Name.
206 					 * @return {ArrayList} A List of Group names in the sheet 
207 					 * @function
208 					 */
209 					getNamesSortByName:function(){
210 						return this.getNameList().getElements().sort(
211 							function(a, b) {
212 								if (a.toLowerCase() < b.toLowerCase()) return -1;
213 								if (a.toLowerCase() > b.toLowerCase()) return 1;
214 								return 0;
215 							  }
216 						);
217 					},
218 					/**
219 					 * @name getNameList
220 					 * @memberOf NexcelGroupList
221 					 * @method 
222 					 * @description Removes the given group from the Collection
223 					 * @function
224 					 */
225 					remove:function(groupName){
226 						if (names.indexOf(groupName) >= 0){
227 							var id = names.indexOf(groupName);
228 							names[id]=undefined;
229 							obj[id]=undefined;
230 							var temp =[];
231 							for(var i =0; i< idList.length; i++){
232 								if(idList[i] != id)
233 									temp.push(idList[i]);
234 							}
235 							idList = temp;
236 						}
237 					},
238 					
239 					/**
240 					 * @name size
241 					 * @memberOf NexcelGroupList
242 					 * @method 
243 					 * @description Returns teh size of teh groups in the Collection.
244 					 * @return size Size of the Collection.
245 					 * @function
246 					 */
247 					size:function(){
248 						//TODO Size is returning wrong.
249 						return names.length;
250 					},
251 					/**
252 					 * @name reName
253 					 * @memberOf NexcelGroupList
254 					 * @method 
255 					 * @description Renames the Given Group with the new Name.
256 					 * @return {Void}
257 					 * @param oldName
258 					 * @param newName
259 					 * @function
260 					 */
261 					reName:function(oldName, newName){
262 						var index = names.indexOf(oldName);
263 						if (index >= 0){
264 							names[index] = newName;
265 						}
266 					},
267 					
268 					/**
269 					 * @name getGroupObjListWithGroupFormula
270 					 * @memberOf NexcelGroupList
271 					 * @method 
272 					 * @description Returns a List of Groups Which have a GroupFormula 
273 					 * @param sheetId
274 					 * @return {ArrayList<GroupObj>}
275 					 * @function
276 					 */
277 					getGroupObjListWithGroupFormula:function(sheetId){
278 						var groupList= new Collection.ArrayList();
279 						for(var i=0; i<idList.length; i++){
280 							if(obj[idList[i]].formula){
281 								if(sheetId){
282 									if(sheetId == obj[idList[i]].sheetId)
283 										groupList.add(obj[idList[i]]);
284 								}else
285 									groupList.add(obj[idList[i]]);
286 							}
287 						}
288 						return groupList;
289 					},
290 					/**
291 					 * @name getListOfGroupWithGivenCellIsPartOf
292 					 * @memberOf NexcelGroupList
293 					 * @method 
294 					 * @description Returns a List of Groups Which have the cell is a part of 
295 					 * @param sheetId
296 					 * @param row	Row of teh Cell
297 					 * @param column Column of teh cell
298 					 * @return {ArrayList<GroupObj>}
299 					 * @function
300 					 */
301 					getListOfGroupWithGivenCellIsPartOf:function(sheetId, row, Col){
302 						var groupList=new Collection.ArrayList();
303 						for(var i=0; i<idList.length; i++){
304 							var groupObj = obj[idList[i]];
305 							if(groupObj.sheetId == sheetId && row<=groupLoc.rowEnd && row>=groupLoc.rowStart && col>=groupLoc.colStart && col<=groupLoc.colEnd)
306 								groupList.add(obj[idList[i]]);
307 						}
308 						return groupList;
309 					},
310 					/**
311 					 * @name isGroupFormulaPresent
312 					 * @memberOf NexcelGroupList
313 					 * @method 
314 					 * @description Checks whether group formula present in the group Object. 
315 					 * @param groupName
316 					 * @return {Boolean}
317 					 * @function
318 					 */
319 					isGroupFormulaPresent:function(groupName){
320 						var groupObj = this.getGroupObjByName(groupName);
321 						if(!groupObj) return false;
322 						return groupObj.formula;
323 					}
324 				};
325 				return groupList;
326 			}
327 		};		
328 		return NexcelGroup.List();
329 	}
330 );