2daaf5cc6d1dbc71d1f004ab16b057b9538e90e6
[ccsdk/features.git] /
1 ( function( $, app, joey ) {
2
3         var ui = app.ns("ui");
4
5         var CELL_SEPARATOR = ",";
6         var CELL_QUOTE = '"';
7         var LINE_SEPARATOR = "\r\n";
8
9         ui.CSVTable = ui.AbstractWidget.extend({
10                 defaults: {
11                         results: null
12                 },
13                 _baseCls: "uiCSVTable",
14                 init: function( parent ) {
15                         this._super();
16                         var results = this.config.results.hits.hits;
17                         var columns = this._parseResults( results );
18                         this._downloadButton = new ui.Button({
19                                 label: "Generate Download Link",
20                                 onclick: this._downloadLinkGenerator_handler
21                         });
22                         this._downloadLink = $.joey( { tag: "A", text: "download", });
23                         this._downloadLink.hide();
24                         this._csvText = this._csv_template( columns, results );
25                         this.el = $.joey( this._main_template() );
26                         this.attach( parent );
27                 },
28                 _downloadLinkGenerator_handler: function() {
29                         var csvData = new Blob( [ this._csvText ], { type: 'text/csv' });
30                         var csvURL = URL.createObjectURL( csvData );
31                         this._downloadLink.attr( "href", csvURL );
32                         this._downloadLink.show();
33                 },
34                 _parseResults: function( results ) {
35                         var columnPaths = {};
36                         (function parse( path, obj ) {
37                                 if( obj instanceof Array ) {
38                                         for( var i = 0; i < obj.length; i++ ) {
39                                                 parse( path, obj[i] );
40                                         }
41                                 } else if( typeof obj === "object" ) {
42                                         for( var prop in obj ) {
43                                                 parse( path + "." + prop, obj[ prop ] );
44                                         }
45                                 } else {
46                                         columnPaths[ path ] = true;
47                                 }
48                         })( "root", results );
49                         var columns = [];
50                         for( var column in columnPaths ) {
51                                 columns.push( column.split(".").slice(1) );
52                         }
53                         return columns;
54                 },
55                 _main_template: function() { return (
56                         { tag: "DIV", cls: this._baseCls, id: this.id(), children: [
57                                 this._downloadButton,
58                                 this._downloadLink,
59                                 { tag: "PRE", text: this._csvText }
60                         ] }
61                 ); },
62                 _csv_template: function( columns, results ) {
63                         return this._header_template( columns ) + LINE_SEPARATOR + this._results_template( columns, results );
64                 },
65                 _header_template: function( columns ) {
66                         return columns.map( function( column ) {
67                                 return column.join(".");
68                         }).join( CELL_SEPARATOR );
69                 },
70                 _results_template: function( columns, results ) {
71                         return results.map( function( result ) {
72                                 return columns.map( function( column ) {
73                                         var l = 0,
74                                                 ptr = result;
75                                         while( l !== column.length && ptr != null ) {
76                                                 ptr = ptr[ column[ l++ ] ];
77                                         }
78                                         return ( ptr == null ) ? "" : ( CELL_QUOTE + ptr.toString().replace(/"/g, '""') + CELL_QUOTE );
79                                 }).join( CELL_SEPARATOR );
80                         }).join( LINE_SEPARATOR );
81                 }
82         });
83
84 })( this.jQuery, this.app, this.joey );