Skip to content

Commit 9964356

Browse files
committed
Extracted table data extractor from grid.js into a custom datasource.
1 parent c84438d commit 9964356

File tree

5 files changed

+87
-46
lines changed

5 files changed

+87
-46
lines changed

grid-datamodel/data.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<script src="../ui/jquery.ui.widget.js"></script>
1212
<script src="dataitem.js"></script>
1313
<script src="datasource.js"></script>
14+
<script src="extractor-datasource.js"></script>
1415
<script src="datastore.js"></script>
1516
<script src="grid.js"></script>
1617
<script>

grid-datamodel/datasort.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<script src="../ui/jquery.ui.widget.js"></script>
1515
<script src="dataitem.js"></script>
1616
<script src="datasource.js"></script>
17+
<script src="extractor-datasource.js"></script>
1718
<script src="datastore.js"></script>
1819
<script src="grid.js"></script>
1920
<script>

grid-datamodel/datastore.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
// TODO rename or remove this (call datasource.get(store) directly instead)
3333
populate: function( type ) {
34-
console.log("never called?")
3534
// TODO or rename datasource.get
3635
$.ui.datasource.types[ type ].get( this );
3736
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Table Extractor Datasource
3+
*
4+
* Depends on:
5+
* datasource
6+
*/
7+
(function( $ ) {
8+
9+
$.widget( "ui.extractingDatasource", $.ui.datasource, {
10+
options: {
11+
table: null
12+
},
13+
_create: function() {
14+
var type = "generated" + $.now();
15+
this.options.type = type;
16+
17+
var fieldDescriptions = {};
18+
var fields = this.options.table.find( "th" ).map(function() {
19+
var th = $( this ),
20+
field = th.data( "field" );
21+
if ( !field ) {
22+
// generate field name if missing
23+
field = th.text().toLowerCase().replace(/\s|[^a-z0-9]/g, "_");
24+
}
25+
26+
fieldDescriptions[ field ] = {
27+
type: th.data( "type" ),
28+
culture: th.data( "culture" ),
29+
format: th.data( "format" ),
30+
sortOrder: th.data( "sort-order" ) || 1
31+
};
32+
33+
return field;
34+
}).get();
35+
36+
$.ui.dataitem.extend( type, {
37+
fields: fieldDescriptions
38+
});
39+
40+
var indexedGuid = 1;
41+
var items = this.options.source = this.options.table.find( "tbody" ).children().map(function() {
42+
var item = { guid: $( this ).data( "guid" ) };
43+
// generate guid if missing
44+
if ( !item.guid ) {
45+
item.guid = indexedGuid++;
46+
}
47+
$( this ).children().each(function( i ) {
48+
item[ fields[ i ] ] = $( this ).text();
49+
});
50+
return item;
51+
}).get();
52+
53+
this._super("_create");
54+
},
55+
type: function() {
56+
return this.options.type;
57+
}
58+
});
59+
60+
})( jQuery );

grid-datamodel/grid.js

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
* Depends on:
55
* tmpl
66
* datastore
7+
*
8+
* Optional:
9+
* extractingDatasource
710
*/
811
(function( $ ) {
912

1013
$.widget( "ui.grid", {
1114
options: {
15+
columns: null,
1216
type: null,
1317
rowTemplate: null
1418
},
1519
_create: function() {
16-
if ( !this.options.type ) {
17-
this._parseData();
18-
}
20+
this._type();
21+
this._columns();
1922
this._rowTemplate();
2023
var that = this;
2124
this.element.addClass( "ui-widget" );
@@ -46,53 +49,30 @@ $.widget( "ui.grid", {
4649
});
4750
tbody.find( "td" ).addClass( "ui-widget-content" );
4851
},
49-
// TODO: data extraction should live on its own
50-
// and grid should delegate the work to the data extractor
51-
_parseData: function() {
52-
var type = "generated" + $.now();
53-
this.options.type = type;
54-
55-
var fieldDescriptions = {};
56-
// TODO seperate column extraction from data extraction to make columns option actually optional
57-
// TODO if columns is specified but not table header exist, generate it
58-
var fields = this.options.columns = this.element.find( "th" ).map(function() {
59-
var th = $( this ),
60-
field = $( this ).data( "field" );
52+
53+
_type: function() {
54+
if ( !this.options.type ) {
55+
// doesn't cover generationg the columns option or generating headers when option is specified
56+
this.options.type = new $.ui.extractingDatasource( {
57+
// TODO generate columns first, and pass along?
58+
table: this.element
59+
}).type();
60+
}
61+
},
62+
63+
_columns: function() {
64+
if ( this.options.columns ) {
65+
// TODO check if table headers exist, generate if not
66+
return;
67+
}
68+
this.options.columns = this.element.find( "th" ).map(function() {
69+
var field = $( this ).data( "field" );
6170
if ( !field ) {
6271
// generate field name if missing
6372
field = $( this ).text().toLowerCase().replace(/\s|[^a-z0-9]/g, "_");
6473
}
65-
66-
fieldDescriptions[ field ] = {
67-
type: th.data( "type" ),
68-
culture: th.data( "culture" ),
69-
format: th.data( "format" ),
70-
sortOrder: th.data( "sort-order" ) || 1
71-
};
72-
7374
return field;
74-
}).get();
75-
76-
var indexedGuid = 1;
77-
var items = this.element.find( "tbody" ).children().map(function() {
78-
var item = { guid: $( this ).data( "guid" ) };
79-
// generate guid if missing
80-
if ( !item.guid ) {
81-
item.guid = indexedGuid++;
82-
}
83-
$( this ).children().each(function( i ) {
84-
item[ fields[ i ] ] = $( this ).text();
85-
});
86-
return item;
87-
}).get();
88-
89-
$.ui.dataitem.extend( type, {
90-
fields: fieldDescriptions
91-
});
92-
$.ui.datasource({
93-
type: type,
94-
source: items
95-
});
75+
}).get()
9676
},
9777

9878
_rowTemplate: function() {

0 commit comments

Comments
 (0)