1 define(['require', 'Collections'], 
  2     function ( require, Collection ) {
  3     	// Old File --->>>>Nexcel.sheet.js
  4    		/**
  5 		 * A Class to handle with all Nexcel WorkBook data related functionalities with Some UI for the Modals
  6 		 * @name WorkBook
  7 		 * @class WorkBook
  8 		 * @example require('WorkBook')
  9 		 * @exports WorkBook
 10 		 * @version 1.0
 11 		 * @module WorkBook
 12 		 * @augments Collections
 13 		 **/
 14 		var WorkBook = {
 15 			ID:0,
 16 			ACTIVE_SHEET_ID:0,
 17 			SCROLLBAR_WIDTH:0,
 18 			TABLE_ROW_HTML:[],
 19 			LAST_LEFT:0,
 20 			LAST_TOP:0,
 21 			MAX_ROW:0,
 22 			SHEET_DETAILS:[],
 23 			SHEET_ORDER:[],
 24 			MDI:new Object(),
 25 			/**
 26 			 * WorkBook UI Based all functionality comes under this module
 27 			 * @name UI
 28 			 * @class UI
 29 			 * @example require('WorkBook').UI
 30 			 * @exports UI
 31 			 * @version 1.0
 32 			 * @module WorkBook
 33 			 * @memberOf WorkBook
 34 			 **/
 35 			
 36 			UI:{
 37 				/**
 38 				 * @name init
 39 				 * @memberOf WorkBook.UI
 40 				 * @method 
 41 				 * @description Setup the Drag functionality to the workbsheet
 42 				 * @function
 43 				 */
 44 				init:function(){
 45 					require('GridView').CELL_DRAG.init();
 46 					require('GridView').highlightCell(require('GridView').lastHighlighedTD, false);
 47 				},
 48 				/**
 49 				 * @name openSubFolder
 50 				 * @memberOf WorkBook.UI
 51 				 * @method 
 52 				 * @param {integer} parentFolderId The foder in which the details have to be presented.
 53 				 * @description The function opens the modal panel for selecting the files and folders in the folderId provided
 54 				 * @function
 55 				 */
 56 				openSubFolder:function(parentId){
 57 					console.log('opening Sub Folder :'+parentId);
 58 					require("WorkBookDataService").loadWorkBooks(WorkBook.UI.setUpWBListItems, parentId);
 59 				},
 60 				/**
 61 				 * @name searchInWBDialog
 62 				 * @memberOf WorkBook.UI
 63 				 * @method 
 64 				 * @param {String} the search string to filter out the folder and the file name.
 65 				 * @description Search in the Work Book List items and updates the list of items in the folder/file list by filtering with the provided sting.
 66 				 * @function
 67 				 */
 68 				searchInWBDialog:function(text){
 69 					var parentId = WorkBook.MDI.WBDialogMDI.parentStack.pop();
 70 					WorkBook.UI.setUpWBListItems(parentId, WorkBook.MDI.WBDialogMDI.data, text);
 71 				},
 72 				/**
 73 				 * @name setUpWBListItems
 74 				 * @memberOf WorkBook.UI
 75 				 * @method 
 76 				 * @param {integer} parentId The folder id in which the current view is.
 77 				 * @param {Object[]} result The list of items of the current folder.
 78 				 * @param {String} searchtext The search Text the user provided.
 79 				 * @description The method will update the UI by applying the searchText Filter and reRender entire List in the UI.
 80 				 * @function
 81 				 */
 82 				setUpWBListItems:function(parentId, result, searchText){
 83 					WorkBook.MDI.WBDialogMDI.parentStack.push(parentId);
 84 					$(WorkBook.MDI.WBDialogMDI.content).empty();
 85 					WorkBook.MDI.WBDialogMDI.data = result;
 86 					var contentDiv = $('<div class="list-group">  </div>');
 87 					$(contentDiv).appendTo($(WorkBook.MDI.WBDialogMDI.content));
 88 					for(var i=0; i< result.length ; i++){
 89 						var obj=result[i];
 90 						var row;
 91 						if(!searchText || obj.name.toUpperCase().indexOf(searchText.toUpperCase())>=0){
 92 							if(obj.type == 'FD')
 93 								row=$('<a class="list-group-item" href="#" onclick="require(\'WorkBook\').UI.openSubFolder('+obj.id+')"><i class="fa fa-folder fa-lx"></i>  '+obj.name+'</a>');
 94 							else
 95 								row=$('<a class="list-group-item" href="#" onclick="require(\'WorkBook\').openWorkBook('+obj.id+')"><i class="fa fa-file-text fa-lx"></i>  '+obj.name+'</a>');
 96 							$(row).appendTo(contentDiv);
 97 						}
 98 					}
 99 				},
100 				/**
101 				 * @name openExplanationGraphMDI
102 				 * @memberOf WorkBook.UI
103 				 * @method 
104 				 * @description The method will openup the blank Explanation Graph MDI with no data for explanation.
105 				 * @function
106 				 */
107 				openExplanationGraphMDI:function(){
108 					require(['GraphController'],function(graphController){
109 						graphController.showExplanationGraph();
110 					});
111 				},
112 				/**
113 				 * @name openDependencyGraphMDI
114 				 * @memberOf WorkBook.UI
115 				 * @method 
116 				 * @description The method will openup the blank Dependancy Graph MDI with prepopulated dependancies.
117 				 * @function
118 				 */
119 				openDependencyGraphMDI:function(){
120 					require(['GraphController'], function(graphController){
121 						graphController.showDependencyGraph();
122 					});
123 				},
124 				/**
125 				 * @name setUpWBDialog
126 				 * @memberOf WorkBook.UI
127 				 * @method
128 				 * @param {integer} parentId  The folder id in which the current view is.
129 				 * @param {Object[]} result The list of items of the current folder.
130 				 * @description The method will setUp the Modal dialog for listing the Select workbook or subfolders with call back function for all the buttons. This method is called by WorkBook.UI.openWBDialog on Ajax Call Back, 
131 				 * @function
132 				 */
133 				setUpWBDialog:function(parentId, result){
134 					WorkBook.UI.closeWBDialog();
135 					var headerToolBar = [
136 						{
137 							//Toolbar Item : Back to Parent folder 
138 							item: '<div style="float:left; width:40px; border-radius: 8px; padding-right:45px;"><a class="btn btn-sm btn-action" tipsytitle="Back" style="color:#555555;width:40px; height:33px;font-size:14px; font-family:arial,sans-serif;" href="#"> <i class="fa fa-arrow-left fa-lg" title="Click to go back to parent folder"></i></a></div>',
139 							event: 'click',
140 							callback: function(){ 
141 								//var search = $(this).siblings().find('input').val();
142 								//WorkBook.UI.searchInWBDialog(search);
143 								if(!WorkBook.MDI.WBDialogMDI.parentStack.isEmpty()){
144 									var folderId = WorkBook.MDI.WBDialogMDI.parentStack.pop();// First pop current folder id.
145 									var parentId = WorkBook.MDI.WBDialogMDI.parentStack.pop();
146 									if(!parentId){
147 										WorkBook.MDI.WBDialogMDI.parentStack.push(null);
148 										WorkBook.MDI.WBDialogMDI.parentStack.push(folderId);
149 									}else
150 										WorkBook.UI.openSubFolder(parentId);
151 								}
152 							}
153 						},
154 						{
155 							//Toolbar Item : Create folder in the current folder
156 							item:'<div style="float:left; width:40px; border-radius: 8px; padding-right:45px;"><a class="btn btn-sm btn-action" tipsytitle="Click to search" style="color:#555555;width:40px; height:33px;font-size:14px; font-family:arial,sans-serif;" href="#"> <i class="fa fa-folder fa-lg" title="Create folder"></i></a></div>',
157 							event:'click',
158 							callback:function(){
159 									var arr = [				    
160 										{
161 											item: '<button class="btn btn-default" type="button"><span class="..."><i class="fa fa-close fa-fw"></i></span> Cancel</button>',
162 											event: 'click',
163 											callback: function(){ WorkBook.MDI.folderNameMDI.close(); }
164 										},
165 										{
166 											item: '<button class="btn btn-default" type="button"><span class="..."><i class="fa fa-check fa-fw"></i></span> OK</button>',
167 											event: 'click',
168 											callback: function(){ 
169 												var folderName = $(WorkBook.MDI.folderNameMDI.content).find('input').val();
170 												if(!folderName)
171 													return;
172 												if(folderName.length<3){
173 													modal({
174 														type  : 'alert',
175 														title : 'Alert',
176 														text  : "Invalid name, please provide the name with minimum length 3 !"
177 													});
178 													return;
179 												}
180 												var folderId = WorkBook.MDI.WBDialogMDI.parentStack.pop();
181 												WorkBook.MDI.WBDialogMDI.parentStack.push(folderId);
182 												//require('UserDataService.addFolder(folderName, folderId, function(){WorkBook.UI.openSubFolder(folderId);});
183 												require(['UserDataService'],function(userDataService){
184 													userDataService.addFolder(folderName, folderId, function(){
185 																										require('WorkBook').UI.openSubFolder(folderId);
186 																										});
187 												});
188 												WorkBook.MDI.folderNameMDI.close();
189 											}
190 										}
191 									];
192 								WorkBook.MDI.folderNameMDI = $.jsPanel({
193 									selector: "#jspanel-func .panel-body",
194 									title:    "Folder name",
195 									toolbarFooter : arr,
196 									modal: true,
197 									content:'<div style="padding:25px;"><div class="input-group margin-bottom-sm">  <span class="input-group-addon" id="folderNameTxt"><i class="fa fa-folder fa-fw"></i></span>  <input class="form-control" type="text" placeholder="Folder name"></div></div>' ,
198 									position: "center",
199 									resizable: {
200 										minWidth: 300,
201 										minHeight: 150
202 									},
203 									size:     { width: 400, height: 80 },
204 									callback: [
205 										function(panel){
206 										 //  Nexcel.GRID_VIEW.jsPanelTipsy();
207 										}
208 									]
209 								});
210 							   setTimeout(function(){WorkBook.MDI.folderNameMDI.front();},100);
211 							}
212 						},
213 						{
214 							//Toolbar Item : Create workbook in the current folder.
215 							item: '<div style="float:left; width:40px; border-radius: 8px; padding-right:4px;"><a class="btn btn-sm btn-action" tipsytitle="Create workbook" style="color:#555555;width:40px; height:33px;font-size:14px; font-family:arial,sans-serif;" href="#"> <i class="fa fa-file-text fa-lg" title="Create workbook"></i></a></div>',
216 							event: 'click',
217 							callback: function(){ 
218 								console.log('Create Workbook on folder......');
219 								var folderId = WorkBook.MDI.WBDialogMDI.parentStack.pop();
220 								WorkBook.MDI.WBDialogMDI.parentStack.push(folderId);
221 								WorkBook.addWorkBook(folderId, function(workBookId){
222 											WorkBook.UI.openSubFolder(folderId);
223 											WorkBook.MDI.addWorkBookMDI.close();
224 											WorkBook.openWorkBook(workBookId);
225 										});
226 								setTimeout(function(){WorkBook.MDI.addWorkBookMDI.front();},100);
227 							}
228 						},
229 						{
230 							item:'<div style="float:left; width:calc(100% - 220px); padding-left:4px; padding-right:4px;"><input class="form-control explanation" id="queryString" type="text" placeholder="Search.."></div>'
231 						},
232 						{
233 							item: '<a class="btn btn-sm btn-action" tipsyTitle="Click to search" style="border-color:#555555;color:#555555;width:90px; height:30px;font-size:14px; font-family:arial,sans-serif;"href="#"> Search</a>',
234 							event: 'click',
235 							callback: function(){ 
236 								var search = $(this).siblings().find('input').val();
237 								WorkBook.UI.searchInWBDialog(search);
238 							}
239 						},
240 					] ;
241 					WorkBook.MDI.WBDialogMDI = $.jsPanel({
242 							selector: "#jspanel-func .panel-body",
243 							title:    "Open Workbook",
244 							position: "center",
245 							toolbarHeader: headerToolBar,
246 							size:     { width: 500, height: 400 },
247 							resizable: {
248 								minWidth: 500,
249 								minHeight: 400
250 							},
251 							callback: [
252 								function(panel){
253 								   //Nexcel.GRID_VIEW.jsPanelTipsy();
254 								}
255 							]
256 					});
257 					$(WorkBook.MDI.WBDialogMDI).find('input').keyup(function(){WorkBook.UI.searchInWBDialog($(this).val());});
258 					WorkBook.MDI.WBDialogMDI.parentStack=Collection.Stack();
259 					WorkBook.MDI.WBDialogMDI.parentStack.push(null);
260 					WorkBook.MDI.WBDialogMDI.data = result;
261 					WorkBook.MDI.WBDialogMDI.parentId = parentId;
262 					WorkBook.UI.setUpWBListItems(parentId, result);
263 					$(WorkBook.MDI.WBDialogMDI.content).css('overflow','scroll');
264 				},
265 
266 				/**
267 				 * @name openWBDialog
268 				 * @memberOf WorkBook.UI
269 				 * @method
270 				 * @description WorkBook Dialog is a single instance MDI, The method will make ajax request for the items in the root folder id for the user, The user root folder id is saved in the hidden input $('#rootFolderId'), on Ajax Call back it calls WorkBook.UI.setUpWBDialog
271 				 * @function
272 				 */
273 				openWBDialog:function(){
274 					$('#openWBDiv').show();
275 					var rootFolderId = $('#rootFolderId').val();
276 					require(['WorkBookDataService'], function(workBookDataService){
277 						workBookDataService.loadWorkBooks(WorkBook.UI.setUpWBDialog, rootFolderId);
278 					});
279 				},
280 				/**
281 				 * @name closeWBDialog
282 				 * @memberOf WorkBook.UI
283 				 * @method
284 				 * @description The method will close the WorkBook MDI if it is already opened.
285 				 * @function
286 				 */
287 				closeWBDialog:function(){
288 					if(WorkBook.MDI.openWBDialog)
289 						  WorkBook.MDI.openWBDialog.close();
290 				},
291 				
292 				/**
293 				 * @name setUpWBDialog
294 				 * @memberOf WorkBook.UI
295 				 * @method
296 				 * @param {integer} parentId  The folder id in which the current view is.
297 				 * @param {Object[]} result The list of items of the current folder.
298 				 * @description The method will setUp the Modal dialog for listing the Select workbook or subfolders with call back function for all the buttons. This method is called by WorkBook.UI.openWBDialog on Ajax Call Back, 
299 				 * @function
300 				 */
301 				openGroupListDialog:function(){
302 					var headerToolBar = [
303 						{
304 							item:'<div style="float:left; width:calc(100% - 90px); padding-left:4px; padding-right:4px;"><input class="form-control explanation" id="queryString" type="text" placeholder="Search.."></div>'
305 						},
306 						{
307 							item: '<a class="btn btn-sm btn-action" tipsyTitle="Click to search" style="border-color:#555555;color:#555555;width:90px; height:30px;font-size:14px; font-family:arial,sans-serif;"href="#"> Search</a>',
308 							event: 'click',
309 							callback: function(){ 
310 								var search = $(this).siblings().find('input').val();
311 								WorkBook.UI.setUpGroupListItems(search);
312 							}
313 						},
314 					] ;
315 					WorkBook.MDI.GroupListMDI = $.jsPanel({
316 							selector: "#jspanel-func .panel-body",
317 							title:    "List of groups",
318 							position: "center",
319 							toolbarHeader: headerToolBar,
320 							size:     { width: 600, height: 400 },
321 							resizable: {
322 								minWidth: 600,
323 								minHeight: 400
324 							},
325 							callback: [
326 								function(panel){
327 								   //Nexcel.GRID_VIEW.jsPanelTipsy();
328 								}
329 							]
330 					});
331 					$(WorkBook.MDI.GroupListMDI).find('input').keyup(function(){WorkBook.UI.setUpGroupListItems($(this).val());});
332 					WorkBook.UI.setUpGroupListItems();
333 					$(WorkBook.MDI.GroupListMDI.content).css('overflow','scroll');
334 				},
335 				/**
336 				 * @name setUpGroupListItems
337 				 * @memberOf WorkBook.UI
338 				 * @method 
339 				 * @param {String} searchtext The search Text the user provided.
340 				 * @description The method will update the UI by applying the searchText Filter and reRender entire List in the UI.
341 				 * @function
342 				 */
343 				setUpGroupListItems:function(searchText){
344 					$(WorkBook.MDI.GroupListMDI.content).empty();
345 					var row = $('<div class="row" style="margin-right: 2px; margin-left: -5px;">');
346 					$(row).appendTo($(WorkBook.MDI.GroupListMDI.content));
347 					var col1 = $('<div class="col-md-4" style="padding-right:0px;"><div class="panel panel-default"> <div class="panel-heading" style="background-color: #f5f5f5;border-color: #ddd;">Group Names</div><div class="panel-body;"><div class="list-group" style="    margin-bottom: 0px;">  </div></div> </div></div>');
348 					$(col1).appendTo($(row));
349 					var col2 = $('<div class="col-md-8" style="padding-right:0px;padding-left: 0px;"><div class="panel panel-default"> <div class="panel-heading" style="background-color: #f5f5f5;border-color: #ddd;">Group Details</div><div class="panel-body;"><table class="table table-striped" id="groupObjDetTable"></table></div> </div></div>');
350 					$(col2).appendTo($(row));
351 					var contentDiv = $(col1).find('.list-group');
352 					
353 					var groupArray = require('NexcelGroupList').getNamesSortByName();
354 					for(var i=0; i< groupArray.length; i++){
355 						var group=groupArray[i];
356 						if(!searchText || group.toUpperCase().indexOf(searchText.toUpperCase())>=0){
357 							row=$('<a class="list-group-item" href="#" onclick="require(\'WorkBook\').UI.showGroupDetails(\''+groupArray[i]+'\')"><i class="glyphicon glyphicon-list-alt fa-lx"></i>  '+groupArray[i]+'</a>');
358 							$(row).appendTo(contentDiv);
359 						}
360 					}
361 				},
362 				showGroupDetails:function(name){
363 					var groupObj = require('NexcelGroupList').getGroupObjByName(name);
364 					$('#groupObjDetTable tr').remove();
365 					$('<tr><td>Group name</td><td>'+name+'</td></tr>').appendTo($('#groupObjDetTable'));
366 					$('<tr><td>Show header</td><td>'+(groupObj.groupTitle=="1"?"True":"False")+'</td></tr>').appendTo($('#groupObjDetTable'));
367 					$('<tr><td>Show column name</td><td>'+(groupObj.colNameAvailable=="1"?"True":"False")+'</td></tr>').appendTo($('#groupObjDetTable'));
368 					var formula= groupObj.formula==undefined?"False":groupObj.formula;
369 					formula = formula.replaceAll("\n","<br/>");
370 					formula = formula.replaceAll("\t","    ");
371 					$('<tr><td>Group formula</td><td>'+formula+'</td></tr>').appendTo($('#groupObjDetTable'));
372 				},
373 				
374 				//TODO Edit workbook name functionality is not integrated with the new UI.
375 				editWBName:function(){
376 					var div = $('<div style="padding-left:45px;padding-top:5px" id="editWBTxtFieldDiv" class="editWBTxtFieldDiv" />');
377 					var width = $('div.wsName').html().length <20?200:$('div.wsName').html().length*10;
378 					console.log(width);
379 					var txtField = $('<input type="text"/>');
380 					$(txtField).blur(function(){
381 							$('div.wsName').html($(this).val());
382 							$('div.editWBTxtFieldDiv').remove();
383 							workBookDataService.renameWorkBook($('#wsId').val(), $(this).val());
384 						});
385 					$(txtField).keydown(function(e){
386 						//console.log(e.keyCode+'  '+e.which+"  "+e.shiftKey);
387 						if(e.keyCode == KEY.BACK_SPACE){// Back Space
388 							var val = $(txtField).val();
389 							$(txtField).val(val.substring(0,val.length-1));
390 						}else if(e.keyCode == KEY.ENTER){// Back Space
391 							$('div.wsName').html($(txtField).val());
392 							$(txtField).blur();
393 						}else{
394 							var val = $(txtField).val()+String.fromCharCode(e.keyCode);
395 							val = val.trimLeft();
396 							var first = val.substring(0,1);
397 							var result = ''+first.toUpperCase();
398 							if(val.length>1){
399 								var second = val.substring(1,val.length);
400 								result = result+second.toLowerCase();
401 							}
402 							$(txtField).val(result);
403 						}
404 						e.preventDefault();
405 					});
406 					$(txtField).val($('div.wsName').html());
407 					$('div.wsName').html('');
408 					var divTop = $(div).offset().top;
409 					var divLeft = $(div).offset().left;
410 					$(txtField).css({width:width+'px', height:'24px', font:'20px arial,sans-serif'});
411 					$(div).css({'z-index':12,position:"absolute", left:divLeft,top:divTop, width:width+'px', height:'2px', border: '1px dotted #4285F4;', 'background-color':'#EEEEEE;'});
412 					$(txtField).appendTo($(div));
413 					$(div).appendTo($('div.fileMenuDiv'));
414 					$(txtField).focus();
415 				}
416 			},
417 			
418 			/**
419 			 * @name loadWorkBookSheetsCallBack
420 			 * @memberOf WorkBook
421 			 * @param {JSON} result the Ajx response from sheet request.
422 			 * @method
423 			 * @description This is the call back function called after loading the list of Sheet present in the WorkBook, from the method NexcelController.init
424 			 * @function
425 			 */
426 			loadWorkBookSheetsCallBack:function(result) {
427 				var sheetDetails = WorkBook.SHEET_DETAILS = JSON.parse(result);
428 				var workBookDataService = require('WorkBookDataService');
429 				require(['SheetController'], function(SheetController){
430 					for(var i=0; i<sheetDetails.length; i++){
431 						workBookDataService.loadCellsOnSheet(WorkBook.ID, sheetDetails[i], SheetController.loadCellsOnSheetCallBack);
432 					}	
433 				});
434 		   },
435 		   
436 		   
437 			/**
438 			 * @name getSheetObj
439 			 * @memberOf WorkBook
440 			 * @method
441 			 * @param {integer} sheetId
442 			 * @description It returns the sheet object by passing the sheetId.
443 			 * @function
444 			 * @example require('WorkBook').getSheetObj(sheetId)
445 			 */
446 		   getSheetObj:function(sheetId){
447 				for(var i=0; i<WorkBook.SHEET_DETAILS.length; i++){
448 					if(WorkBook.SHEET_DETAILS[i].id == sheetId)
449 						return WorkBook.SHEET_DETAILS[i];
450 				}
451 				return null;
452 		   },
453 			/**
454 			 * @name getServiceURL
455 			 * @memberOf WorkBook
456 			 * @method
457 			 * @description It returns the the web service URL, 
458 			 * @function
459 			 * @example require('WorkBook').getServiceURL()
460 			 */
461 		   getServiceURL:function(){
462 			   	var url = window.location.pathname;
463 				url = url.split('/');
464 				url.splice(0, 1);
465 				url.splice(url.length - 1, 1);
466 				var serviceUrl = window.location.protocol + '//' + window.location.host + "/" + url.join('/'); 
467 				return serviceUrl; 
468 		   },
469 			/**
470 			 * @name getSheetObjByName
471 			 * @memberOf WorkBook
472 			 * @method
473 			 * @description It returns the the sheet Object by passing the name of the sheet. 
474 			 * @param {String} sheetName
475 			 * @function
476 			 */
477 		   getSheetObjByName:function(sheetName){
478 				for(var i=0; i<WorkBook.SHEET_DETAILS.length; i++){
479 					if(WorkBook.SHEET_DETAILS[i].name.trim()== sheetName.trim())
480 						return WorkBook.SHEET_DETAILS[i];
481 				}
482 				return null;
483 		   },
484 		   
485 		   	/**
486 			 * @name addWorkBook
487 			 * @memberOf WorkBook
488 			 * @method
489 			 * @description It opens the MDI for add workbook, this method contains the action listeners for the Ok and cancel Buttons. 
490 			 * @param {Integer} folderId The folder id where which the workbook is created.
491 			 * @param {Function(wsId)} callBack the function will be called back after the workbook created, once the new id is recieved through Ajax, the call back will called through WorkBookDataService @example require('WorkBook').openWorkBook(wsId)  
492 			 * @function
493 			 * @example require('WorkBook').addWorkBook()
494 			 */
495 		   addWorkBook:function(folderId, callBack){
496 			var arr = [				    
497 						{
498 							item: '<button class="btn btn-default" type="button"><span class="..."><i class="fa fa-close fa-fw"></i></span> Cancel</button>',
499 							event: 'click',
500 							callback: function(){ WorkBook.MDI.addWorkBookMDI.close(); }
501 						},
502 						{
503 							item: '<button class="btn btn-default" type="button"><span class="..."><i class="fa fa-check fa-fw"></i></span> OK</button>',
504 							event: 'click',
505 							callback: function(){ 
506 								var wbName = $(WorkBook.MDI.addWorkBookMDI.content).find('input').val();
507 								if(!wbName)
508 									return;
509 								if(wbName.length<3){
510 									modal({
511 										type  : 'alert',
512 										title : 'Alert',
513 										text  : "Invalid name, please provide the name with minimum length 3 !"
514 									});
515 									return;
516 								}
517 								var rootFolderId = $('#rootFolderId').val();
518 								require('WorkBookDataService').addWorkBook(wbName, WorkBook.MDI.addWorkBookMDI.folderId, callBack);
519 							}
520 						}
521 					];
522 				WorkBook.MDI.addWorkBookMDI = $.jsPanel({
523 					selector: "#jspanel-func .panel-body",
524 					title:    "New Workbook",
525 					toolbarFooter : arr,
526 					modal: true,
527 					content:'<div style="padding:25px;"><div class="input-group margin-bottom-sm">  <span class="input-group-addon" id="workBookNameTxt"><i class="fa fa-table fa-fw"></i></span>  <input class="form-control" type="text" placeholder="Workbook name"></div></div>' ,
528 					position: "center",
529 					resizable: {
530 						minWidth: 300,
531 						minHeight: 150
532 					},
533 					size:     { width: 400, height: 80 },
534 					callback: [
535 						function(panel){
536 						   $(panel).find('input').focus();
537 						}
538 					]
539 				});
540 				WorkBook.MDI.addWorkBookMDI.folderId = folderId;
541 		   },
542 		   	/**
543 			 * @name duplicateWorkBook
544 			 * @memberOf WorkBook
545 			 * @method
546 			 * @description It duplicates the workbook to a new workbook exactly the same hseet and data with groups. 
547 			 * @param {Function(wsId)} callBack the function will be called back after the workbook created, once the new id is recieved through Ajax, the call back will called through WorkBookDataService @example require('WorkBook').openWorkBook(wsId)  
548 			 * @function
549 			 * @example require('WorkBook').duplicateWorkBook()
550 			 */
551 		   duplicateWorkBook:function(wsId){
552 		  		modal({
553 					type  : 'confirm',
554 					title : 'Confirm',
555 					text  : 'Do you want to duplicate this workbook ?',
556 					callback: function(result){ 
557 						if(result){
558 							require(['WorkBookDataService'],function(workBookDataService){
559 								var result = require('WorkBookDataService').duplicateWorkBook(wsId, require('WorkBook').openWorkBook);
560 							});
561 						} 
562 					}
563 				});
564 		   },
565 		   	/**
566 			 * @name openWorkBook
567 			 * @memberOf WorkBook
568 			 * @method
569 			 * @description It opens new workbook with the provided workbook Id, It uses  
570 			 * @param {Integer} wsId The workBook Id
571 			 * @function
572 			 * @example require('WorkBook').openWorkBook(12)
573 			 */
574 			openWorkBook:function(wsId){
575 				var url = window.location.pathname;
576 				url = url.split('/');
577 				url.splice(0,1);
578 				url.splice(url.length-1,1);
579 				//TODO replace with WorkBook.getServiceUrl function
580 				var url = window.location.protocol+'//'+window.location.host+"/"+url.join('/')+'/nexcel.php?wsId='+wsId;
581 		
582 				//var url = window.location.protocol+'//'+window.location.host+"/"+window.location.pathname.split('/')[1]+'/nexcel.php?wsId='+wsId;
583 				window.location.replace(url);
584 			},
585 			/**
586 			 * @name switchSheet
587 			 * @memberOf WorkBook
588 			 * @method
589 			 * @description UI Bases swich sheet action handler, once the user clicks on any sheet other than the active sheet, it will switch to the new sheet. It uses WorkBook.switchSheetAction(sheetId) 
590 			 * @param {DIV} tab The sheet Tab HTML Object
591 			 * @function
592 			 */
593 		   switchSheet:function(tab){
594 				if($(tab).attr('class')=='active')
595 					return;
596 				require('loading').show();
597 				var sheetId=$(tab).data('id');
598 				if(!WorkBook.getSheetObj(sheetId)){
599 					console.log('System is not loaded yet.....');
600 					return;
601 				}
602 				$('.active').attr('class','SHEET_TAB inactive inactiveTab tabZIndex');
603 				$(tab).attr('class','SHEET_TAB active tabZIndex');
604 				
605 				WorkBook.switchSheetAction(sheetId);
606 				require('loading').hide();
607 				GridView = require('GridView');
608 				GridView.setUpgroupHeaderOnSheet(sheetId);
609 				GridView.highlightGroupsOnSheet(sheetId);
610 		   },
611 			/**
612 			 * @name switchSheetAction
613 			 * @memberOf WorkBook
614 			 * @method
615 			 * @description This function supports programmatic switching of the sheet by passing the sheetId 
616 			 * @param {Integer} sheetId The sheet Id
617 			 * @function
618 			 */
619 		   switchSheetAction:function(sheetId){
620 			   	var GridView = require('GridView');
621 			   	var WorkBook = require('WorkBook');
622 				$('#dataTable tr').remove();
623 				var maxRow = GridView.getViewRows();
624 				WorkBook.ACTIVE_SHEET_ID = sheetId;
625 				var size =  rowCount = $('#dataTable tr').length;
626 				var KeyListener = require('KeyListener'); 		
627 				var MouseListener = require('MouseListener');
628 				for(i=0;i<maxRow;i++){
629 					var row=i+1;
630 					var rowHtml = GridView.getRowHtml(sheetId, row);
631 					$(rowHtml).find('td').each (function() {
632 							MouseListener.setCellListener(this);
633 							KeyListener.setCellListener(this);
634 						});  
635 					$('#dataTable').append(rowHtml);
636 					$('#rowNumTable').append("<tr><td>"+parseInt(row)+"</td></tr>");
637 				}
638 				$(DomObjects.dataTableDiv).scrollTop(0);
639 				require('MouseListener').setSelectionHandler(0,0,0,0);
640 				GridView.highlightCell();
641 				GridView.setUserColWidth();
642 				GridView.rePositionGroupHeaderDiv();
643 				GridView.setUserRowHeight();
644 				GridView.setViewRowHeight();
645 				require(['WorkBookDataService'],function(workBookDataService){
646 					workBookDataService.setActiveSheet(WorkBook.ID, WorkBook.ACTIVE_SHEET_ID);	
647 				});
648 			}
649 		};
650 		return WorkBook;
651 });