5482b96296e114565341963e1ec83a95013fa840
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 ( function( $, app, joey ) {
17
18         var ui = app.ns("ui");
19
20         var CELL_SEPARATOR = ",";
21         var CELL_QUOTE = '"';
22         var LINE_SEPARATOR = "\r\n";
23
24         ui.CSVTable = ui.AbstractWidget.extend({
25                 defaults: {
26                         results: null
27                 },
28                 _baseCls: "uiCSVTable",
29                 init: function( parent ) {
30                         this._super();
31                         var results = this.config.results.hits.hits;
32                         var columns = this._parseResults( results );
33                         this._downloadButton = new ui.Button({
34                                 label: "Generate Download Link",
35                                 onclick: this._downloadLinkGenerator_handler
36                         });
37                         this._downloadLink = $.joey( { tag: "A", text: "download", });
38                         this._downloadLink.hide();
39                         this._csvText = this._csv_template( columns, results );
40                         this.el = $.joey( this._main_template() );
41                         this.attach( parent );
42                 },
43                 _downloadLinkGenerator_handler: function() {
44                         var csvData = new Blob( [ this._csvText ], { type: 'text/csv' });
45                         var csvURL = URL.createObjectURL( csvData );
46                         this._downloadLink.attr( "href", csvURL );
47                         this._downloadLink.show();
48                 },
49                 _parseResults: function( results ) {
50                         var columnPaths = {};
51                         (function parse( path, obj ) {
52                                 if( obj instanceof Array ) {
53                                         for( var i = 0; i < obj.length; i++ ) {
54                                                 parse( path, obj[i] );
55                                         }
56                                 } else if( typeof obj === "object" ) {
57                                         for( var prop in obj ) {
58                                                 parse( path + "." + prop, obj[ prop ] );
59                                         }
60                                 } else {
61                                         columnPaths[ path ] = true;
62                                 }
63                         })( "root", results );
64                         var columns = [];
65                         for( var column in columnPaths ) {
66                                 columns.push( column.split(".").slice(1) );
67                         }
68                         return columns;
69                 },
70                 _main_template: function() { return (
71                         { tag: "DIV", cls: this._baseCls, id: this.id(), children: [
72                                 this._downloadButton,
73                                 this._downloadLink,
74                                 { tag: "PRE", text: this._csvText }
75                         ] }
76                 ); },
77                 _csv_template: function( columns, results ) {
78                         return this._header_template( columns ) + LINE_SEPARATOR + this._results_template( columns, results );
79                 },
80                 _header_template: function( columns ) {
81                         return columns.map( function( column ) {
82                                 return column.join(".");
83                         }).join( CELL_SEPARATOR );
84                 },
85                 _results_template: function( columns, results ) {
86                         return results.map( function( result ) {
87                                 return columns.map( function( column ) {
88                                         var l = 0,
89                                                 ptr = result;
90                                         while( l !== column.length && ptr != null ) {
91                                                 ptr = ptr[ column[ l++ ] ];
92                                         }
93                                         return ( ptr == null ) ? "" : ( CELL_QUOTE + ptr.toString().replace(/"/g, '""') + CELL_QUOTE );
94                                 }).join( CELL_SEPARATOR );
95                         }).join( LINE_SEPARATOR );
96                 }
97         });
98
99 })( this.jQuery, this.app, this.joey );