[#349,transform][s]: remove transform view and associated code - fixes #349.
This commit is contained in:
@@ -230,18 +230,6 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
||||
});
|
||||
return facetResults;
|
||||
};
|
||||
|
||||
this.transform = function(editFunc) {
|
||||
var dfd = new Deferred();
|
||||
// TODO: should we clone before mapping? Do not see the point atm.
|
||||
self.records = _.map(self.records, editFunc);
|
||||
// now deal with deletes (i.e. nulls)
|
||||
self.records = _.filter(self.records, function(record) {
|
||||
return record != null;
|
||||
});
|
||||
dfd.resolve();
|
||||
return dfd.promise();
|
||||
};
|
||||
};
|
||||
|
||||
}(this.recline.Backend.Memory));
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
this.recline = this.recline || {};
|
||||
this.recline.Data = this.recline.Data || {};
|
||||
|
||||
(function(my) {
|
||||
// adapted from https://github.com/harthur/costco. heather rules
|
||||
|
||||
my.Transform = {};
|
||||
|
||||
my.Transform.evalFunction = function(funcString) {
|
||||
try {
|
||||
eval("var editFunc = " + funcString);
|
||||
} catch(e) {
|
||||
return {errorMessage: e+""};
|
||||
}
|
||||
return editFunc;
|
||||
};
|
||||
|
||||
my.Transform.previewTransform = function(docs, editFunc, currentColumn) {
|
||||
var preview = [];
|
||||
var updated = my.Transform.mapDocs($.extend(true, {}, docs), editFunc);
|
||||
for (var i = 0; i < updated.docs.length; i++) {
|
||||
var before = docs[i]
|
||||
, after = updated.docs[i]
|
||||
;
|
||||
if (!after) after = {};
|
||||
if (currentColumn) {
|
||||
preview.push({before: before[currentColumn], after: after[currentColumn]});
|
||||
} else {
|
||||
preview.push({before: before, after: after});
|
||||
}
|
||||
}
|
||||
return preview;
|
||||
};
|
||||
|
||||
my.Transform.mapDocs = function(docs, editFunc) {
|
||||
var edited = []
|
||||
, deleted = []
|
||||
, failed = []
|
||||
;
|
||||
|
||||
var updatedDocs = _.map(docs, function(doc) {
|
||||
try {
|
||||
var updated = editFunc(_.clone(doc));
|
||||
} catch(e) {
|
||||
failed.push(doc);
|
||||
return;
|
||||
}
|
||||
if(updated === null) {
|
||||
updated = {_deleted: true};
|
||||
edited.push(updated);
|
||||
deleted.push(doc);
|
||||
}
|
||||
else if(updated && !_.isEqual(updated, doc)) {
|
||||
edited.push(updated);
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
|
||||
return {
|
||||
updates: edited,
|
||||
docs: updatedDocs,
|
||||
deletes: deleted,
|
||||
failed: failed
|
||||
};
|
||||
};
|
||||
|
||||
}(this.recline.Data))
|
||||
14
src/model.js
14
src/model.js
@@ -159,20 +159,6 @@ my.Dataset = Backbone.Model.extend({
|
||||
return this._store.save(this._changes, this.toJSON());
|
||||
},
|
||||
|
||||
transform: function(editFunc) {
|
||||
var self = this;
|
||||
if (!this._store.transform) {
|
||||
alert('Transform is not supported with this backend: ' + this.get('backend'));
|
||||
return;
|
||||
}
|
||||
this.trigger('recline:flash', {message: "Updating all visible docs. This could take a while...", persist: true, loader: true});
|
||||
this._store.transform(editFunc).done(function() {
|
||||
// reload data as records have changed
|
||||
self.query();
|
||||
self.trigger('recline:flash', {message: "Records updated successfully"});
|
||||
});
|
||||
},
|
||||
|
||||
// ### query
|
||||
//
|
||||
// AJAX method with promise API to get records from the backend.
|
||||
|
||||
@@ -164,12 +164,6 @@ my.MultiView = Backbone.View.extend({
|
||||
model: this.model,
|
||||
state: this.state.get('view-timeline')
|
||||
})
|
||||
}, {
|
||||
id: 'transform',
|
||||
label: 'Transform',
|
||||
view: new my.Transform({
|
||||
model: this.model
|
||||
})
|
||||
}];
|
||||
}
|
||||
// Hashes of sidebar elements
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
/*jshint multistr:true */
|
||||
|
||||
this.recline = this.recline || {};
|
||||
this.recline.View = this.recline.View || {};
|
||||
|
||||
// Views module following classic module pattern
|
||||
(function($, my) {
|
||||
|
||||
// ## ColumnTransform
|
||||
//
|
||||
// View (Dialog) for doing data transformations
|
||||
my.Transform = Backbone.View.extend({
|
||||
template: ' \
|
||||
<div class="recline-transform"> \
|
||||
<div class="script"> \
|
||||
<h2> \
|
||||
Transform Script \
|
||||
<button class="okButton btn btn-primary">Run on all records</button> \
|
||||
</h2> \
|
||||
<textarea class="expression-preview-code"></textarea> \
|
||||
</div> \
|
||||
<div class="expression-preview-parsing-status"> \
|
||||
No syntax error. \
|
||||
</div> \
|
||||
<div class="preview"> \
|
||||
<h3>Preview</h3> \
|
||||
<div class="expression-preview-container"></div> \
|
||||
</div> \
|
||||
</div> \
|
||||
',
|
||||
|
||||
events: {
|
||||
'click .okButton': 'onSubmit',
|
||||
'keydown .expression-preview-code': 'onEditorKeydown'
|
||||
},
|
||||
|
||||
initialize: function(options) {
|
||||
this.el = $(this.el);
|
||||
},
|
||||
|
||||
render: function() {
|
||||
var htmls = Mustache.render(this.template);
|
||||
this.el.html(htmls);
|
||||
// Put in the basic (identity) transform script
|
||||
// TODO: put this into the template?
|
||||
var editor = this.el.find('.expression-preview-code');
|
||||
if (this.model.fields.length > 0) {
|
||||
var col = this.model.fields.models[0].id;
|
||||
} else {
|
||||
var col = 'unknown';
|
||||
}
|
||||
editor.val("function(doc) {\n doc['"+ col +"'] = doc['"+ col +"'];\n return doc;\n}");
|
||||
editor.keydown();
|
||||
},
|
||||
|
||||
onSubmit: function(e) {
|
||||
var self = this;
|
||||
var funcText = this.el.find('.expression-preview-code').val();
|
||||
var editFunc = recline.Data.Transform.evalFunction(funcText);
|
||||
if (editFunc.errorMessage) {
|
||||
this.trigger('recline:flash', {message: "Error with function! " + editFunc.errorMessage});
|
||||
return;
|
||||
}
|
||||
this.model.transform(editFunc);
|
||||
},
|
||||
|
||||
editPreviewTemplate: ' \
|
||||
<table class="table table-condensed table-bordered before-after"> \
|
||||
<thead> \
|
||||
<tr> \
|
||||
<th>Field</th> \
|
||||
<th>Before</th> \
|
||||
<th>After</th> \
|
||||
</tr> \
|
||||
</thead> \
|
||||
<tbody> \
|
||||
{{#row}} \
|
||||
<tr> \
|
||||
<td> \
|
||||
{{field}} \
|
||||
</td> \
|
||||
<td class="before {{#different}}different{{/different}}"> \
|
||||
{{before}} \
|
||||
</td> \
|
||||
<td class="after {{#different}}different{{/different}}"> \
|
||||
{{after}} \
|
||||
</td> \
|
||||
</tr> \
|
||||
{{/row}} \
|
||||
</tbody> \
|
||||
</table> \
|
||||
',
|
||||
|
||||
onEditorKeydown: function(e) {
|
||||
var self = this;
|
||||
// if you don't setTimeout it won't grab the latest character if you call e.target.value
|
||||
window.setTimeout( function() {
|
||||
var errors = self.el.find('.expression-preview-parsing-status');
|
||||
var editFunc = recline.Data.Transform.evalFunction(e.target.value);
|
||||
if (!editFunc.errorMessage) {
|
||||
errors.text('No syntax error.');
|
||||
var docs = self.model.records.map(function(doc) {
|
||||
return doc.toJSON();
|
||||
});
|
||||
var previewData = recline.Data.Transform.previewTransform(docs, editFunc);
|
||||
var $el = self.el.find('.expression-preview-container');
|
||||
var fields = self.model.fields.toJSON();
|
||||
var rows = _.map(previewData.slice(0,4), function(row) {
|
||||
return _.map(fields, function(field) {
|
||||
return {
|
||||
field: field.id,
|
||||
before: row.before[field.id],
|
||||
after: row.after[field.id],
|
||||
different: !_.isEqual(row.before[field.id], row.after[field.id])
|
||||
}
|
||||
});
|
||||
});
|
||||
$el.html('');
|
||||
_.each(rows, function(row) {
|
||||
var templated = Mustache.render(self.editPreviewTemplate, {
|
||||
row: row
|
||||
});
|
||||
$el.append(templated);
|
||||
});
|
||||
} else {
|
||||
errors.text(editFunc.errorMessage);
|
||||
}
|
||||
}, 1, true);
|
||||
}
|
||||
});
|
||||
|
||||
})(jQuery, recline.View);
|
||||
Reference in New Issue
Block a user