[#73,view/timeline][m]: many improvements to timeline - now pretty functional.
* reload data on query updates * attempting to guess start date and end date fields. * avoid initting multiple times * refactor to be cleaner
This commit is contained in:
@@ -12,16 +12,37 @@ my.Timeline = Backbone.View.extend({
|
|||||||
<div id="vmm-timeline-id"></div> \
|
<div id="vmm-timeline-id"></div> \
|
||||||
',
|
',
|
||||||
|
|
||||||
initialize: function() {
|
// These are the default (case-insensitive) names of field that are used if found.
|
||||||
|
// If not found, the user will need to define these fields on initialization
|
||||||
|
startFieldNames: ['date','startdate', 'start', 'start-date'],
|
||||||
|
endFieldNames: ['end','endDate'],
|
||||||
|
elementId: '#vmm-timeline-id',
|
||||||
|
|
||||||
|
initialize: function(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.el = $(this.el);
|
this.el = $(this.el);
|
||||||
this.render();
|
this.timeline = new VMM.Timeline();
|
||||||
|
this._timelineIsInitialized = false;
|
||||||
this.bind('view:show', function() {
|
this.bind('view:show', function() {
|
||||||
// set width explicitly o/w timeline goes wider that screen for some reason
|
if (self._timelineIsInitialized === false) {
|
||||||
self.el.find('#vmm-timeline-id').width(self.el.parent().width());
|
self._initTimeline();
|
||||||
// only call initTimeline once in DOM as Timeline uses $ internally to look up element
|
}
|
||||||
self.initTimeline();
|
|
||||||
});
|
});
|
||||||
|
this.model.fields.bind('change', function() {
|
||||||
|
self._setupTemporalField();
|
||||||
|
});
|
||||||
|
this.model.currentDocuments.bind('all', function() {
|
||||||
|
self.reloadData();
|
||||||
|
});
|
||||||
|
var stateData = _.extend({
|
||||||
|
startField: null,
|
||||||
|
endField: null
|
||||||
|
},
|
||||||
|
options.state
|
||||||
|
);
|
||||||
|
this.state = new recline.Model.ObjectState(stateData);
|
||||||
|
this._setupTemporalField();
|
||||||
|
this.render();
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
@@ -30,15 +51,21 @@ my.Timeline = Backbone.View.extend({
|
|||||||
this.el.html(htmls);
|
this.el.html(htmls);
|
||||||
},
|
},
|
||||||
|
|
||||||
initTimeline: function() {
|
_initTimeline: function() {
|
||||||
var config = {
|
// set width explicitly o/w timeline goes wider that screen for some reason
|
||||||
width: "300px",
|
this.el.find(this.elementId).width(this.el.parent().width());
|
||||||
height: "50%"
|
// only call _initTimeline once view in DOM as Timeline uses $ internally to look up element
|
||||||
};
|
var config = {};
|
||||||
var data = this._timelineJSON();
|
var data = this._timelineJSON();
|
||||||
var elementId = '#vmm-timeline-id';
|
this.timeline.init(data, this.elementId, config);
|
||||||
this.timeline = new VMM.Timeline();
|
this._timelineIsInitialized = true
|
||||||
this.timeline.init(data, elementId, config);
|
},
|
||||||
|
|
||||||
|
reloadData: function() {
|
||||||
|
if (this._timelineIsInitialized) {
|
||||||
|
var data = this._timelineJSON();
|
||||||
|
this.timeline.reload(data);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_timelineJSON: function() {
|
_timelineJSON: function() {
|
||||||
@@ -53,7 +80,8 @@ my.Timeline = Backbone.View.extend({
|
|||||||
};
|
};
|
||||||
this.model.currentDocuments.each(function(doc) {
|
this.model.currentDocuments.each(function(doc) {
|
||||||
var tlEntry = {
|
var tlEntry = {
|
||||||
"startDate": doc.get('date'),
|
"startDate": doc.get(self.state.get('startField')),
|
||||||
|
"endDate": doc.get(self.state.get('endField')) || null,
|
||||||
"headline": String(doc.get(self.model.fields.models[0].id)),
|
"headline": String(doc.get(self.model.fields.models[0].id)),
|
||||||
"text": ''
|
"text": ''
|
||||||
};
|
};
|
||||||
@@ -62,6 +90,24 @@ my.Timeline = Backbone.View.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return out;
|
return out;
|
||||||
|
},
|
||||||
|
|
||||||
|
_setupTemporalField: function() {
|
||||||
|
this.state.set({
|
||||||
|
startField: this._checkField(this.startFieldNames),
|
||||||
|
endField: this._checkField(this.endFieldNames)
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
_checkField: function(possibleFieldNames) {
|
||||||
|
var modelFieldNames = this.model.fields.pluck('id');
|
||||||
|
for (var i = 0; i < possibleFieldNames.length; i++){
|
||||||
|
for (var j = 0; j < modelFieldNames.length; j++){
|
||||||
|
if (modelFieldNames[j].toLowerCase() == possibleFieldNames[i].toLowerCase())
|
||||||
|
return modelFieldNames[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,46 @@
|
|||||||
module("View - Timeline");
|
module("View - Timeline");
|
||||||
|
|
||||||
test('basics', function () {
|
test('extract dates and timelineJSON', function () {
|
||||||
|
var dataset = recline.Backend.createDataset([
|
||||||
|
{'Date': '2012-03-20', 'title': '1'},
|
||||||
|
{'Date': '2012-03-25', 'title': '2'},
|
||||||
|
]);
|
||||||
|
var view = new recline.View.Timeline({
|
||||||
|
model: dataset
|
||||||
|
});
|
||||||
|
equal(view.state.get('startField'), 'Date');
|
||||||
|
|
||||||
|
var out = view._timelineJSON();
|
||||||
|
var exp = {
|
||||||
|
'timeline': {
|
||||||
|
'type': 'default',
|
||||||
|
'headline': '',
|
||||||
|
'date': [
|
||||||
|
{
|
||||||
|
'startDate': '2012-03-20',
|
||||||
|
'endDate': null,
|
||||||
|
'headline': '2012-03-20',
|
||||||
|
'text': ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'startDate': '2012-03-25',
|
||||||
|
'endDate': null,
|
||||||
|
'headline': '2012-03-25',
|
||||||
|
'text': ''
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
deepEqual(out, exp);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('render etc', function () {
|
||||||
var dataset = Fixture.getDataset();
|
var dataset = Fixture.getDataset();
|
||||||
var view = new recline.View.Timeline({
|
var view = new recline.View.Timeline({
|
||||||
model: dataset
|
model: dataset
|
||||||
});
|
});
|
||||||
$('.fixtures').append(view.el);
|
$('.fixtures').append(view.el);
|
||||||
view.initTimeline();
|
view._initTimeline();
|
||||||
assertPresent('.vmm-timeline', view.el);
|
assertPresent('.vmm-timeline', view.el);
|
||||||
assertPresent('.timenav', view.el);
|
assertPresent('.timenav', view.el);
|
||||||
assertPresent('.timenav', view.el);
|
assertPresent('.timenav', view.el);
|
||||||
|
|||||||
Reference in New Issue
Block a user