Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / libs / tableExport.js
1 /*The MIT License (MIT)
2
3 Copyright (c) 2014 https://github.com/kayalshri/
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.*/
22
23 (function($){
24         $.fn.extend({
25             tableExport: function(options) {
26                 var defaults = {
27                                                 separator: ',',
28                                                 ignoreColumn: [],
29                                                 tableName:'yourTableName',
30                                                 type:'csv',
31                                                 pdfFontSize:14,
32                                                 pdfLeftMargin:20,
33                                                 escape:'true',
34                                                 htmlContent:'false',
35                                                 consoleLog:'false'
36                                 };
37                 
38                                 var options = $.extend(defaults, options);
39                                 var el = this;
40                                 
41                                 if(defaults.type == 'csv' || defaults.type == 'txt'){
42                                 
43                                         // Header
44                                         var tdData ="";
45                                         $(el).find('thead').find('tr').each(function() {
46                                         tdData += "\n";                                 
47                                                 $(this).filter(':visible').find('th').each(function(index,data) {
48                                                         if ($(this).css('display') != 'none'){
49                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
50                                                                         tdData += '"' + parseString($(this)) + '"' + defaults.separator;                                                                        
51                                                                 }
52                                                         }
53                                                         
54                                                 });
55                                                 tdData = $.trim(tdData);
56                                                 tdData = $.trim(tdData).substring(0, tdData.length -1);
57                                         });
58                                         
59                                         // Row vs Column
60                                         $(el).find('tbody').find('tr').each(function() {
61                                         tdData += "\n";
62                                                 $(this).filter(':visible').find('td').each(function(index,data) {
63                                                         if ($(this).css('display') != 'none'){
64                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
65                                                                         tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
66                                                                 }
67                                                         }
68                                                 });
69                                                 //tdData = $.trim(tdData);
70                                                 tdData = $.trim(tdData).substring(0, tdData.length -1);
71                                         });
72                                         
73                                         //output
74                                         if(defaults.consoleLog == 'true'){
75                                                 console.log(tdData);
76                                         }
77                                         var base64data = "base64," + $.base64.encode(tdData);
78                                         window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
79                                 }else if(defaults.type == 'sql'){
80                                 
81                                         // Header
82                                         var tdData ="INSERT INTO `"+defaults.tableName+"` (";
83                                         $(el).find('thead').find('tr').each(function() {
84                                         
85                                                 $(this).filter(':visible').find('th').each(function(index,data) {
86                                                         if ($(this).css('display') != 'none'){
87                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
88                                                                         tdData += '`' + parseString($(this)) + '`,' ;                                                                   
89                                                                 }
90                                                         }
91                                                         
92                                                 });
93                                                 tdData = $.trim(tdData);
94                                                 tdData = $.trim(tdData).substring(0, tdData.length -1);
95                                         });
96                                         tdData += ") VALUES ";
97                                         // Row vs Column
98                                         $(el).find('tbody').find('tr').each(function() {
99                                         tdData += "(";
100                                                 $(this).filter(':visible').find('td').each(function(index,data) {
101                                                         if ($(this).css('display') != 'none'){
102                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
103                                                                         tdData += '"'+ parseString($(this)) + '",';
104                                                                 }
105                                                         }
106                                                 });
107                                                 
108                                                 tdData = $.trim(tdData).substring(0, tdData.length -1);
109                                                 tdData += "),";
110                                         });
111                                         tdData = $.trim(tdData).substring(0, tdData.length -1);
112                                         tdData += ";";
113                                         
114                                         //output
115                                         //console.log(tdData);
116                                         
117                                         if(defaults.consoleLog == 'true'){
118                                                 console.log(tdData);
119                                         }
120                                         
121                                         var base64data = "base64," + $.base64.encode(tdData);
122                                         window.open('data:application/sql;filename=exportData;' + base64data);
123                                         
124                                 
125                                 }else if(defaults.type == 'json'){
126                                 
127                                         var jsonHeaderArray = [];
128                                         $(el).find('thead').find('tr').each(function() {
129                                                 var tdData =""; 
130                                                 var jsonArrayTd = [];
131                                         
132                                                 $(this).filter(':visible').find('th').each(function(index,data) {
133                                                         if ($(this).css('display') != 'none'){
134                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
135                                                                         jsonArrayTd.push(parseString($(this)));                                                                 
136                                                                 }
137                                                         }
138                                                 });                                                                     
139                                                 jsonHeaderArray.push(jsonArrayTd);                                              
140                                                 
141                                         });
142                                         
143                                         var jsonArray = [];
144                                         $(el).find('tbody').find('tr').each(function() {
145                                                 var tdData =""; 
146                                                 var jsonArrayTd = [];
147                                         
148                                                 $(this).filter(':visible').find('td').each(function(index,data) {
149                                                         if ($(this).css('display') != 'none'){
150                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
151                                                                         jsonArrayTd.push(parseString($(this)));                                                                 
152                                                                 }
153                                                         }
154                                                 });                                                                     
155                                                 jsonArray.push(jsonArrayTd);                                                                    
156                                                 
157                                         });
158                                         
159                                         var jsonExportArray =[];
160                                         jsonExportArray.push({header:jsonHeaderArray,data:jsonArray});
161                                         
162                                         //Return as JSON
163                                         //console.log(JSON.stringify(jsonExportArray));
164                                         
165                                         //Return as Array
166                                         //console.log(jsonExportArray);
167                                         if(defaults.consoleLog == 'true'){
168                                                 console.log(JSON.stringify(jsonExportArray));
169                                         }
170                                         var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray));
171                                         window.open('data:application/json;filename=exportData;' + base64data);
172                                 }else if(defaults.type == 'xml'){
173                                 
174                                         var xml = '<?xml version="1.0" encoding="utf-8"?>';
175                                         xml += '<tabledata><fields>';
176
177                                         // Header
178                                         $(el).find('thead').find('tr').each(function() {
179                                                 $(this).filter(':visible').find('th').each(function(index,data) {
180                                                         if ($(this).css('display') != 'none'){                                  
181                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
182                                                                         xml += "<field>" + parseString($(this)) + "</field>";
183                                                                 }
184                                                         }
185                                                 });                                                                     
186                                         });                                     
187                                         xml += '</fields><data>';
188                                         
189                                         // Row Vs Column
190                                         var rowCount=1;
191                                         $(el).find('tbody').find('tr').each(function() {
192                                                 xml += '<row id="'+rowCount+'">';
193                                                 var colCount=0;
194                                                 $(this).filter(':visible').find('td').each(function(index,data) {
195                                                         if ($(this).css('display') != 'none'){  
196                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
197                                                                         xml += "<column-"+colCount+">"+parseString($(this))+"</column-"+colCount+">";
198                                                                 }
199                                                         }
200                                                         colCount++;
201                                                 });                                                                                                                     
202                                                 rowCount++;
203                                                 xml += '</row>';
204                                         });                                     
205                                         xml += '</data></tabledata>'
206                                         
207                                         if(defaults.consoleLog == 'true'){
208                                                 console.log(xml);
209                                         }
210                                         
211                                         var base64data = "base64," + $.base64.encode(xml);
212                                         window.open('data:application/xml;filename=exportData;' + base64data);
213
214                                 }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint'  ){
215                                         //console.log($(this).html());
216                                         var excel="<table>";
217                                         // Header
218                                         $(el).find('thead').find('tr').each(function() {
219                                                 excel += "<tr>";
220                                                 $(this).filter(':visible').find('th').each(function(index,data) {
221                                                         if ($(this).css('display') != 'none'){                                  
222                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
223                                                                         excel += "<td>" + parseString($(this))+ "</td>";
224                                                                 }
225                                                         }
226                                                 });     
227                                                 excel += '</tr>';                                               
228                                                 
229                                         });                                     
230                                         
231                                         
232                                         // Row Vs Column
233                                         var rowCount=1;
234                                         $(el).find('tbody').find('tr').each(function() {
235                                                 excel += "<tr>";
236                                                 var colCount=0;
237                                                 $(this).filter(':visible').find('td').each(function(index,data) {
238                                                         if ($(this).css('display') != 'none'){  
239                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
240                                                                         excel += "<td>"+parseString($(this))+"</td>";
241                                                                 }
242                                                         }
243                                                         colCount++;
244                                                 });                                                                                                                     
245                                                 rowCount++;
246                                                 excel += '</tr>';
247                                         });                                     
248                                         excel += '</table>'
249                                         
250                                         if(defaults.consoleLog == 'true'){
251                                                 console.log(excel);
252                                         }
253                                         
254                                         var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";
255                                         excelFile += "<head>";
256                                         excelFile += "<!--[if gte mso 9]>";
257                                         excelFile += "<xml>";
258                                         excelFile += "<x:ExcelWorkbook>";
259                                         excelFile += "<x:ExcelWorksheets>";
260                                         excelFile += "<x:ExcelWorksheet>";
261                                         excelFile += "<x:Name>";
262                                         excelFile += "{worksheet}";
263                                         excelFile += "</x:Name>";
264                                         excelFile += "<x:WorksheetOptions>";
265                                         excelFile += "<x:DisplayGridlines/>";
266                                         excelFile += "</x:WorksheetOptions>";
267                                         excelFile += "</x:ExcelWorksheet>";
268                                         excelFile += "</x:ExcelWorksheets>";
269                                         excelFile += "</x:ExcelWorkbook>";
270                                         excelFile += "</xml>";
271                                         excelFile += "<![endif]-->";
272                                         excelFile += "</head>";
273                                         excelFile += "<body>";
274                                         excelFile += excel;
275                                         excelFile += "</body>";
276                                         excelFile += "</html>";
277
278                                         var base64data = "base64," + $.base64.encode(excelFile);
279                                         window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data);
280                                         
281                                 }else if(defaults.type == 'png'){
282                                         html2canvas($(el), {
283                                                 onrendered: function(canvas) {                                                                          
284                                                         var img = canvas.toDataURL("image/png");
285                                                         window.open(img);
286                                                         
287                                                         
288                                                 }
289                                         });             
290                                 }else if(defaults.type == 'pdf'){
291         
292                                         var doc = new jsPDF('p','pt', 'a4', true);
293                                         doc.setFontSize(defaults.pdfFontSize);
294                                         
295                                         // Header
296                                         var startColPosition=defaults.pdfLeftMargin;
297                                         $(el).find('thead').find('tr').each(function() {
298                                                 $(this).filter(':visible').find('th').each(function(index,data) {
299                                                         if ($(this).css('display') != 'none'){                                  
300                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
301                                                                         var colPosition = startColPosition+ (index * 50);                                                                       
302                                                                         doc.text(colPosition,20, parseString($(this)));
303                                                                 }
304                                                         }
305                                                 });                                                                     
306                                         });                                     
307                                 
308                                 
309                                         // Row Vs Column
310                                         var startRowPosition = 20; var page =1;var rowPosition=0;
311                                         $(el).find('tbody').find('tr').each(function(index,data) {
312                                                 rowCalc = index+1;
313                                                 
314                                         if (rowCalc % 26 == 0){
315                                                 doc.addPage();
316                                                 page++;
317                                                 startRowPosition=startRowPosition+10;
318                                         }
319                                         rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280);
320                                                 
321                                                 $(this).filter(':visible').find('td').each(function(index,data) {
322                                                         if ($(this).css('display') != 'none'){  
323                                                                 if(defaults.ignoreColumn.indexOf(index) == -1){
324                                                                         var colPosition = startColPosition+ (index * 50);                                                                       
325                                                                         doc.text(colPosition,rowPosition, parseString($(this)));
326                                                                 }
327                                                         }
328                                                         
329                                                 });                                                                                                                     
330                                                 
331                                         });                                     
332                                                                                 
333                                         // Output as Data URI
334                                         doc.output('datauri');
335         
336                                 }
337                                 
338                                 
339                                 function parseString(data){
340                                 
341                                         if(defaults.htmlContent == 'true'){
342                                                 content_data = data.html().trim();
343                                         }else{
344                                                 content_data = data.text().trim();
345                                         }
346                                         
347                                         if(defaults.escape == 'true'){
348                                                 content_data = escape(content_data);
349                                         }
350                                         
351                                         
352                                         
353                                         return content_data;
354                                 }
355                         
356                         }
357         });
358     })(jQuery);
359