1 define(['require', 'Collections', 'MouseListener', 'NexcelCellList'], 
  2 	function(require, Collection, MouseListener, nexcelCellList) {
  3 		var ClipBoard = {
  4 			DATA : '',
  5 			init:function(){
  6 				
  7 			},
  8 			cut : function(ev) {
  9 				require('ClipBoard').copy(ev);
 10 				var selection = MouseListener.getOrderedSelectionPos(HANDLER.SELECTION_HANDLER);
 11 				require('GridView').clearArea(selection);
 12 			},
 13 			copy : function(ev) {
 14 				var HANDLER = require('HANDLER');
 15 				if (!HANDLER.SELECTION_HANDLER)
 16 					return;
 17 				HANDLER.MENU_COPY_EVENT = true;
 18 				var ClipBoard = require('ClipBoard');
 19 				ClipBoard.DATA = undefined;
 20 				if (ev && ev.clipboardData)
 21 					ev.clipboardData.setData('text/plain', '');
 22 				var sheetId = require('WorkBook').ACTIVE_SHEET_ID;
 23 				var selection = MouseListener.getOrderedSelectionPos(HANDLER.SELECTION_HANDLER);
 24 				var copyData = ClipBoard.getSelectionCSVData(sheetId, selection);
 25 				$('textarea').val(copyData);
 26 				$('textarea').select();
 27 				document.execCommand('copy');
 28 				/*if (ev && ev.clipboardData)
 29 					ev.clipboardData.setData('text/plain', copyData);
 30 				//if(copyData && copyData.length >0)
 31 				
 32 				ClipBoard.DATA = require('NexcelCellList').getRangeCellArray(sheetId, selection.START_ROW + 1, selection.START_COL + 1, selection.CURR_ROW + 1, selection.CURR_COL + 1);*/
 33 				// Allways keep in DATA Object.
 34 			},
 35 			getSelectionCSVData : function(sheetId, selection) {
 36 				var data = require('NexcelCellList').getRangeCellArray(sheetId, selection.START_ROW + 1, selection.START_COL + 1, selection.CURR_ROW + 1, selection.CURR_COL + 1);
 37 				var copyData = "";
 38 				for (var i = 0; i < data.length; i++) {
 39 					var row = data[i];
 40 					for (var j = 0; j < row.length; j++) {
 41 						if (row[j])
 42 							copyData = copyData.concat(row[j].getValue()).concat('\t');
 43 						else
 44 							copyData = copyData.concat('').concat('\t');
 45 					}
 46 					copyData = copyData.substring(0, copyData.length - 1);
 47 					copyData = copyData.concat('\n');
 48 				}
 49 				copyData = copyData.substring(0, copyData.length - 1);
 50 				return copyData;
 51 			},
 52 			/**
 53 			 *	It will copy the entire group values to the Clip Board.
 54 			 */
 55 			copyGroup : function() {
 56 				var ContextMenu = require('contextmenu');
 57 				if (ContextMenu.EVENT_TARGET.length > 1) {
 58 					modal({
 59 						type : 'alert',
 60 						title : 'Warning! Cannot copy multiple groups',
 61 						text : "The selected cell is a part of multiple groups, please select a cell unique to this group."
 62 					});
 63 					return false;
 64 				}
 65 	
 66 				var groupObj = ContextMenu.EVENT_TARGET[0];
 67 				var SELECTION_AREA = new Object();
 68 				SELECTION_AREA.START_ROW = (groupObj.rowStart - 1);
 69 				var endRow = groupObj.rowEnd;
 70 				if (groupObj.rowEnd == -1)
 71 					endRow = require('WorkBook').getSheetObj(groupObj.sheetId).rows;
 72 				SELECTION_AREA.CURR_ROW = (endRow - 1);
 73 				SELECTION_AREA.START_COL = (groupObj.colStart - 1);
 74 				SELECTION_AREA.CURR_COL = (groupObj.colEnd - 1);
 75 				var copyData = require('ClipBoard').getSelectionCSVData(groupObj.sheetId, SELECTION_AREA);
 76 				if (copyData && copyData.length > 0){
 77 					$('textarea').val(copyData);
 78 					$('textarea').select();
 79 					document.execCommand('copy');
 80 				}
 81 				// Allways keep in DATA Object.
 82 			},
 83 			paste:function(data){
 84 				//data = data[0][0];
 85 				var rowData = data.split('\n');
 86 				var WorkBook = require('WorkBook');
 87 				var HANDLER = require('HANDLER');
 88 				var sheetObj = WorkBook.getSheetObj(WorkBook.ACTIVE_SHEET_ID);
 89 				var sheetId = WorkBook.ACTIVE_SHEET_ID;
 90 				var row = HANDLER.SELECTION_HANDLER.START_ROW;
 91 				var col = HANDLER.SELECTION_HANDLER.START_COL;
 92 				var trList = $('#dataTable tr');
 93 				var updatedCellList = Collection.ArrayList();
 94 				var maxRow = sheetObj.rows;
 95 				var maxCol = sheetObj.cols;
 96 				for (var i = 0; i < rowData.length; i++) {
 97 					if (!rowData[i] || rowData[i].length == 0)
 98 						break;
 99 					var colData = rowData[i].split('\t');
100 					colPos = col;
101 					for (var j = 0; j < colData.length; j++) {
102 						if (colData[j] == 0 || colData[j] == "false" || colData[j] != 'undefined') {
103 							var nCell = require('NexcelCellList').isExist(sheetId, (row + i + 1), (colPos + j + 1));
104 							if (require('GridView').isCellAssociatedWithGroupWithFormula(row, col, WorkBook.ACTIVE_SHEET_ID))
105 								continue;
106 							try {
107 								$($(trList[row+i]).find('td')[colPos + j]).html(colData[j]);
108 								// Just Updating teh value in GRID_VIEW. Need to Update the TD_LIST as well
109 							} catch(e) {
110 							}
111 							if (nCell && colData[j])
112 								nCell.setValue(colData[j]);
113 							else
114 								nCell = require('NexcelCellList').addCellWithDetails(sheetId, (row + i + 1), colPos + j + 1, undefined, colData[j]);
115 							updatedCellList.add(nCell);
116 						}
117 						if ((colPos + j) > maxCol)
118 							maxCol = colPos + j;
119 					}
120 					if ((row + i) > maxRow)
121 						maxRow = row + i;
122 				}
123 				if (maxRow > sheetObj.rows || maxCol > sheetObj.cols) {
124 					sheetObj.rows = maxRow + 5;
125 					sheetObj.cols = maxCol + 2;
126 					require('WorkBookDataService').updateSheetSize(sheetId, parseInt(maxRow) + 5, parseInt(maxCol) + 2);
127 				}
128 				//require('GridView').setHeaderRowColumnSize(HANDLER.SELECTION_HANDLER.START_ROW);
129 				// TODO Update Sheet Size.
130 				require('NexcelUI').CELL.DATA.updateListOfCells(updatedCellList);
131 			},
132 			pasteOld : function(ev) {
133 				$('textarea').val('').focus();
134 				document.execCommand('paste');
135 				var dataType = 'CELL_ARRAY';
136 				var ClipBoard = require('ClipBoard');
137 				var WorkBook = require('WorkBook');
138 				var HANDLER = require('HANDLER');
139 				var data;
140 				if (ClipBoard.DATA) {
141 					data = ClipBoard.DATA;
142 					ClipBoard.DATA = null;
143 				} else if (ev && ev.clipboardData) {
144 					data = ev.clipboardData.getData('text/plain');
145 					dataType = 'CSV';
146 				} else
147 					return;
148 				var updatedCellList = Collection.ArrayList();
149 				var trList = $('#dataTable tr');
150 				var sheetId = WorkBook.ACTIVE_SHEET_ID;
151 				var selection = MouseListener.getOrderedSelectionPos(HANDLER.SELECTION_HANDLER);
152 				var sheetObj = WorkBook.getSheetObj(WorkBook.ACTIVE_SHEET_ID);
153 				var maxRow = sheetObj.rows;
154 				var maxCol = sheetObj.cols;
155 				var row = HANDLER.SELECTION_HANDLER.START_ROW;
156 				var col = HANDLER.SELECTION_HANDLER.START_COL;
157 				if (MouseListener.isSingleCellSelection(selection) && dataType == 'CSV') {
158 					var rowData = data.split('\n');
159 					for (var i = 0; i < rowData.length; i++) {
160 						if (!rowData[i] || rowData[i].length == 0)
161 							break;
162 						var colData = rowData[i].split('\t');
163 						colPos = col;
164 						for (var j = 0; j < colData.length; j++) {
165 							if (colData[j] == 0 || colData[j] == "false" || colData[j] != 'undefined') {
166 								var nCell = nexcelCellList.isExist(sheetId, (row + i + 1), (colPos + j + 1));
167 								if (require('GridView').isCellAssociatedWithGroupWithFormula(row, col, WorkBook.ACTIVE_SHEET_ID))
168 									continue;
169 								try {
170 									$($(trList[row+i]).find('td')[colPos + j]).html(colData[j]);
171 									// Just Updating teh value in GRID_VIEW. Need to Update the TD_LIST as well
172 								} catch(e) {
173 								}
174 								if (nCell && colData[j])
175 									nCell.setValue(colData[j]);
176 								else
177 									nCell = nexcelCellList.addCellWithDetails(sheetId, (row + i + 1), colPos + j + 1, undefined, colData[j]);
178 								updatedCellList.add(nCell);
179 							}
180 							if ((colPos + j) > maxCol)
181 								maxCol = colPos + j;
182 						}
183 						if ((row + i) > maxRow)
184 							maxRow = row + i;
185 					}
186 				} else if (MouseListener.isSingleCellSelection(selection) && dataType == 'CELL_ARRAY') {
187 					if (!$.isArray(data)) {
188 						var array = [];
189 						array[0] = [];
190 						array[0][0] = data;
191 						data = array;
192 					}
193 					for (var i = 0; i < data.length; i++) {
194 						var colData = data[i];
195 						colPos = col;
196 						for (var j = 0; j < colData.length; j++) {
197 							if (colData[j]) {
198 								var nCell = nexcelCellList.isExist(sheetId, (row + i + 1), (colPos + j + 1));
199 								if (nCell && colData[j].getValue()) {
200 									nCell.setValue(colData[j].getValue());
201 								} else
202 									nCell = nexcelCellList.addCellWithDetails(sheetId, (row + i + 1), colPos + j + 1, undefined, colData[j].getValue());
203 								if (require('GridView').isCellAssociatedWithGroupWithFormula(row, col, require('WorkBook').ACTIVE_SHEET_ID))
204 									continue;
205 								if (colData[j].getFormula()) {
206 									var relFormula = require('RelativeFormulaController').getRelativeFormula(colData[j].getFormula(), colData[j].getCellName(), nCell.getCellName());
207 									var newVal = require('ParsingController').parseAndEvaluateFormula(relFormula);
208 									if (newVal && !isNaN(newVal.groupValue)) {
209 										nCell.setValue(newVal.groupValue);
210 									} else
211 										nCell.setValue(0);
212 									nCell.setFormula(relFormula);
213 								} else
214 									nCell.setFormula(null);
215 	
216 								try {
217 									$($(trList[row+i]).find('td')[colPos + j]).html(nCell.getValue());
218 									// Just Updating teh value in GRID_VIEW. Need to Update the TD_LIST as well
219 								} catch(e) {
220 								}
221 								updatedCellList.add(nCell);
222 							}
223 							if ((colPos + j) > maxCol)
224 								maxCol = colPos + j;
225 						}
226 						if ((row + i) > maxRow)
227 							maxRow = row + i;
228 					}
229 				} else if (!MouseListener.isSingleCellSelection(selection) && dataType == 'CELL_ARRAY') {
230 					for (var i = 0; i < data.length; i++) {
231 						var colData = data[i];
232 						colPos = col;
233 						for (var j = 0; j < colData.length; j++) {
234 							if (colData[j]) {
235 								var nCell = nexcelCellList.isExist(sheetId, (row + i + 1), (colPos + j + 1));
236 								if (nCell && colData[j].getValue()) {
237 									nCell.setValue(colData[j].getValue());
238 								} else
239 									nCell = nexcelCellList.addCellWithDetails(sheetId, (row + i + 1), colPos + j + 1, undefined, colData[j].getValue());
240 								if (require('GridView').isCellAssociatedWithGroupWithFormula(row, col, require('WorkBook').ACTIVE_SHEET_ID))
241 									continue;
242 								if (colData[j].getFormula()) {
243 									var relFormula = require('RelativeFormulaController').getRelativeFormula(colData[j].getFormula(), colData[j].getCellName(), nCell.getCellName());
244 									var newVal = require('ParsingController').parseAndEvaluateFormula(relFormula);
245 									if (newVal && !isNaN(newVal.groupValue)) {
246 										nCell.setValue(newVal.groupValue);
247 									} else
248 										nCell.setValue(0);
249 									nCell.setFormula(relFormula);
250 								} else
251 									nCell.setFormula(null);
252 								try {
253 									$($(trList[row+i]).find('td')[colPos + j]).html(nCell.getValue());
254 									// Just Updating teh value in GRID_VIEW. Need to Update the TD_LIST as well
255 								} catch(e) {
256 								}
257 								updatedCellList.add(nCell);
258 							}
259 							if ((colPos + j) > maxCol)
260 								maxCol = colPos + j;
261 						}
262 						if ((row + i) > maxRow)
263 							maxRow = row + i;
264 					}
265 				}
266 				if (maxRow > sheetObj.rows || maxCol > sheetObj.cols) {
267 					sheetObj.rows = maxRow + 5;
268 					sheetObj.cols = maxCol + 2;
269 					require('WorkBookDataService').updateSheetSize(sheetId, parseInt(maxRow) + 5, parseInt(maxCol) + 2);
270 				}
271 				require('GridView').setHeaderRowColumnSize(HANDLER.SELECTION_HANDLER.START_ROW);
272 				// TODO Update Sheet Size.
273 				require('NexcelUI').CELL.DATA.updateListOfCells(updatedCellList);
274 			}
275 		};
276 		return ClipBoard;
277 });
278