[transform,refactor][s]: rename costco.js to data.transform.js and rename module to recline.Data.Transform.
This commit is contained in:
@@ -168,7 +168,7 @@ this.recline.Backend.Memory = this.recline.Backend.Memory || {};
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.transform = function(editFunc) {
|
this.transform = function(editFunc) {
|
||||||
var toUpdate = costco.mapDocs(this.data, editFunc);
|
var toUpdate = recline.Data.Transform.mapDocs(this.data, editFunc);
|
||||||
// TODO: very inefficient -- could probably just walk the documents and updates in tandem and update
|
// TODO: very inefficient -- could probably just walk the documents and updates in tandem and update
|
||||||
_.each(toUpdate.updates, function(record, idx) {
|
_.each(toUpdate.updates, function(record, idx) {
|
||||||
self.data[idx] = record;
|
self.data[idx] = record;
|
||||||
|
|||||||
@@ -1,68 +1,67 @@
|
|||||||
|
this.recline = this.recline || {};
|
||||||
|
this.recline.Data = this.recline.Data || {};
|
||||||
|
|
||||||
|
(function(my) {
|
||||||
// adapted from https://github.com/harthur/costco. heather rules
|
// adapted from https://github.com/harthur/costco. heather rules
|
||||||
|
|
||||||
var costco = function() {
|
my.Transform = {};
|
||||||
|
|
||||||
function evalFunction(funcString) {
|
|
||||||
try {
|
|
||||||
eval("var editFunc = " + funcString);
|
|
||||||
} catch(e) {
|
|
||||||
return {errorMessage: e+""};
|
|
||||||
}
|
|
||||||
return editFunc;
|
|
||||||
}
|
|
||||||
|
|
||||||
function previewTransform(docs, editFunc, currentColumn) {
|
|
||||||
var preview = [];
|
|
||||||
var updated = 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mapDocs(docs, editFunc) {
|
my.Transform.evalFunction = function(funcString) {
|
||||||
var edited = []
|
try {
|
||||||
, deleted = []
|
eval("var editFunc = " + funcString);
|
||||||
, failed = []
|
} catch(e) {
|
||||||
;
|
return {errorMessage: e+""};
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
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 {
|
return {
|
||||||
evalFunction: evalFunction,
|
updates: edited,
|
||||||
previewTransform: previewTransform,
|
docs: updatedDocs,
|
||||||
mapDocs: mapDocs
|
deletes: deleted,
|
||||||
|
failed: failed
|
||||||
};
|
};
|
||||||
}();
|
};
|
||||||
|
|
||||||
|
}(this.recline.Data))
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ my.Transform = Backbone.View.extend({
|
|||||||
onSubmit: function(e) {
|
onSubmit: function(e) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var funcText = this.el.find('.expression-preview-code').val();
|
var funcText = this.el.find('.expression-preview-code').val();
|
||||||
var editFunc = costco.evalFunction(funcText);
|
var editFunc = recline.Data.Transform.evalFunction(funcText);
|
||||||
if (editFunc.errorMessage) {
|
if (editFunc.errorMessage) {
|
||||||
this.trigger('recline:flash', {message: "Error with function! " + editFunc.errorMessage});
|
this.trigger('recline:flash', {message: "Error with function! " + editFunc.errorMessage});
|
||||||
return;
|
return;
|
||||||
@@ -97,13 +97,13 @@ my.Transform = Backbone.View.extend({
|
|||||||
// if you don't setTimeout it won't grab the latest character if you call e.target.value
|
// if you don't setTimeout it won't grab the latest character if you call e.target.value
|
||||||
window.setTimeout( function() {
|
window.setTimeout( function() {
|
||||||
var errors = self.el.find('.expression-preview-parsing-status');
|
var errors = self.el.find('.expression-preview-parsing-status');
|
||||||
var editFunc = costco.evalFunction(e.target.value);
|
var editFunc = recline.Data.Transform.evalFunction(e.target.value);
|
||||||
if (!editFunc.errorMessage) {
|
if (!editFunc.errorMessage) {
|
||||||
errors.text('No syntax error.');
|
errors.text('No syntax error.');
|
||||||
var docs = self.model.records.map(function(doc) {
|
var docs = self.model.records.map(function(doc) {
|
||||||
return doc.toJSON();
|
return doc.toJSON();
|
||||||
});
|
});
|
||||||
var previewData = costco.previewTransform(docs, editFunc);
|
var previewData = recline.Data.Transform.previewTransform(docs, editFunc);
|
||||||
var $el = self.el.find('.expression-preview-container');
|
var $el = self.el.find('.expression-preview-container');
|
||||||
var fields = self.model.fields.toJSON();
|
var fields = self.model.fields.toJSON();
|
||||||
var rows = _.map(previewData.slice(0,4), function(row) {
|
var rows = _.map(previewData.slice(0,4), function(row) {
|
||||||
|
|||||||
Reference in New Issue
Block a user