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