[graph][s]: add and remove series now working on flot graph view.

This commit is contained in:
Rufus Pollock
2012-01-09 18:52:11 +00:00
parent 1ae8985b00
commit 07a324cc2d

View File

@@ -580,7 +580,6 @@ my.FlotGraph = Backbone.View.extend({
tagName: "div", tagName: "div",
className: "data-graph-container", className: "data-graph-container",
// TODO: normalize css
template: ' \ template: ' \
<div class="editor"> \ <div class="editor"> \
<div class="editor-info editor-hide-info"> \ <div class="editor-info editor-hide-info"> \
@@ -607,13 +606,17 @@ my.FlotGraph = Backbone.View.extend({
{{/headers}} \ {{/headers}} \
</select> \ </select> \
</div> \ </div> \
<label>Series <span>A (y-axis)</span></label> \ <div class="editor-series-group"> \
<div class="input editor-series"> \ <div class="editor-series"> \
<select> \ <label>Series <span>A (y-axis)</span></label> \
{{#headers}} \ <div class="input"> \
<option value="{{.}}">{{.}}</option> \ <select> \
{{/headers}} \ {{#headers}} \
</select> \ <option value="{{.}}">{{.}}</option> \
{{/headers}} \
</select> \
</div> \
</div> \
</div> \ </div> \
</div> \ </div> \
<div class="editor-buttons"> \ <div class="editor-buttons"> \
@@ -631,6 +634,8 @@ my.FlotGraph = Backbone.View.extend({
events: { events: {
'change form select': 'onEditorSubmit' 'change form select': 'onEditorSubmit'
, 'click .editor-add': 'addSeries'
, 'click .action-remove-series': 'removeSeries'
}, },
initialize: function(options, chart) { initialize: function(options, chart) {
@@ -656,10 +661,10 @@ my.FlotGraph = Backbone.View.extend({
$(this.el).html(htmls); $(this.el).html(htmls);
// now set a load of stuff up // now set a load of stuff up
this.$graph = this.el.find('.panel.graph'); this.$graph = this.el.find('.panel.graph');
// event approach did not seem to work // for use later when adding additional series
this.$series = this.el.find('.editor-series select'); // could be simpler just to have a common template!
this.$seriesClone = this.$series.parent().clone(); this.$seriesClone = this.el.find('.editor-series').clone();
var self = this; this._updateSeries();
return this; return this;
}, },
@@ -711,19 +716,47 @@ my.FlotGraph = Backbone.View.extend({
return series; return series;
}, },
// TODO: finish porting this function // Public: Adds a new empty series select box to the editor.
addSeries: function () { //
var element = this.seriesClone.clone(), // All but the first select box will have a remove button that allows them
// to be removed.
//
// Returns itself.
addSeries: function (e) {
e.preventDefault();
var element = this.$seriesClone.clone(),
label = element.find('label'), label = element.find('label'),
index = this.series.length; index = this.$series.length;
this.el.$series.parent().find('ul').append(element);
this.updateSeries();
label.append('<a href="#remove">Remove</a>');
label.find('span').text(String.fromCharCode(this.series.length + 64));
this.el.find('.editor-series-group').append(element);
this._updateSeries();
label.append(' [<a href="#remove" class="action-remove-series">Remove</a>]');
label.find('span').text(String.fromCharCode(this.$series.length + 64));
return this; return this;
},
// Public: Removes a series list item from the editor.
//
// Also updates the labels of the remaining series elements.
removeSeries: function (e) {
e.preventDefault();
var $el = $(e.target);
$el.parent().parent().remove();
this._updateSeries();
this.$series.each(function (index) {
if (index > 0) {
var labelSpan = $(this).prev().find('span');
labelSpan.text(String.fromCharCode(index + 65));
}
});
this.onEditorSubmit();
},
// Private: Resets the series property to reference the select elements.
//
// Returns itself.
_updateSeries: function () {
this.$series = this.el.find('.editor-series select');
} }
}); });