prototyping recursive transform UI with traverse-js
This commit is contained in:
@@ -1,37 +1,29 @@
|
||||
// adapted from https://github.com/harthur/costco. heather rules
|
||||
|
||||
var costco = function() {
|
||||
|
||||
function handleEditorChange(e) {
|
||||
var editFunc = evalFunction(e.target.value);
|
||||
var errors = $('.expression-preview-parsing-status');
|
||||
if (_.isFunction(editFunc)) {
|
||||
errors.text('No syntax error.');
|
||||
} else {
|
||||
errors.text(editFunc);
|
||||
return;
|
||||
}
|
||||
previewTransform(app.cache, editFunc);
|
||||
}
|
||||
|
||||
function evalFunction(funcString) {
|
||||
try {
|
||||
eval("var editFunc = " + funcString);
|
||||
return editFunc;
|
||||
} catch(e) {
|
||||
return e+"";
|
||||
return {errorMessage: e+""};
|
||||
}
|
||||
return editFunc;
|
||||
}
|
||||
|
||||
function previewTransform(docs, editFunc) {
|
||||
function previewTransform(docs, editFunc, currentColumn) {
|
||||
var preview = [];
|
||||
var updated = mapDocs(docs, editFunc);
|
||||
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 = {};
|
||||
preview.push({before: JSON.stringify(before[app.currentColumn]), after: JSON.stringify(after[app.currentColumn])});
|
||||
if (currentColumn) {
|
||||
preview.push({before: JSON.stringify(before[currentColumn]), after: JSON.stringify(after[currentColumn])});
|
||||
} else {
|
||||
preview.push({before: JSON.stringify(before), after: JSON.stringify(after)});
|
||||
}
|
||||
}
|
||||
util.render('editPreview', 'expression-preview-container', {rows: preview});
|
||||
}
|
||||
@@ -108,7 +100,6 @@ var costco = function() {
|
||||
}
|
||||
|
||||
return {
|
||||
handleEditorChange: handleEditorChange,
|
||||
evalFunction: evalFunction,
|
||||
previewTransform: previewTransform,
|
||||
mapDocs: mapDocs,
|
||||
|
||||
@@ -8,7 +8,6 @@ var recline = function() {
|
||||
if (!data) data = {};
|
||||
util.show('dialog');
|
||||
util.render(template, 'dialog-content', data);
|
||||
util.hide('menu');
|
||||
util.observeExit($('.dialog-content'), function() {
|
||||
util.hide('dialog');
|
||||
})
|
||||
@@ -19,6 +18,7 @@ var recline = function() {
|
||||
$( '.menu li' ).click(function(e) {
|
||||
var actions = {
|
||||
bulkEdit: function() { showDialog('bulkEdit', {name: app.currentColumn}) },
|
||||
transform: function() { showDialog('transform') },
|
||||
csv: function() { window.location.href = app.csvUrl },
|
||||
json: function() { window.location.href = "_rewrite/api/json" },
|
||||
urlImport: function() { showDialog('urlImport') },
|
||||
@@ -27,10 +27,10 @@ var recline = function() {
|
||||
deleteColumn: function() {
|
||||
var msg = "Are you sure? This will delete '" + app.currentColumn + "' from all documents.";
|
||||
if (confirm(msg)) costco.deleteColumn(app.currentColumn);
|
||||
util.hide('menu');
|
||||
}
|
||||
}
|
||||
|
||||
util.hide('menu');
|
||||
actions[$(e.target).attr('data-action')]();
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
@@ -65,6 +65,7 @@ app.after = {
|
||||
var action = $(e.target).attr('data-action');
|
||||
util.position('menu', e, {left: -60, top: 5});
|
||||
util.render(action + 'Actions', 'menu');
|
||||
recline.handleMenuClick();
|
||||
});
|
||||
},
|
||||
controls: function() {
|
||||
@@ -80,9 +81,6 @@ app.after = {
|
||||
}
|
||||
});
|
||||
},
|
||||
exportActions: recline.handleMenuClick,
|
||||
importActions: recline.handleMenuClick,
|
||||
columnActions: recline.handleMenuClick,
|
||||
signIn: function() {
|
||||
|
||||
$('.dialog-content #username-input').focus();
|
||||
@@ -110,12 +108,12 @@ app.after = {
|
||||
|
||||
},
|
||||
bulkEdit: function() {
|
||||
|
||||
$('.dialog-content .okButton').click(function(e) {
|
||||
var funcText = $('.expression-preview-code').val();
|
||||
var editFunc = costco.evalFunction(funcText);
|
||||
if (!_.isFunction(editFunc)) {
|
||||
util.notify("Error with function! " + editFunc);
|
||||
;
|
||||
if (editFunc.errorMessage) {
|
||||
util.notify("Error with function! " + editFunc.errorMessage);
|
||||
return;
|
||||
}
|
||||
util.hide('dialog');
|
||||
@@ -127,7 +125,44 @@ app.after = {
|
||||
editor.focus().get(0).setSelectionRange(18, 18);
|
||||
editor.keydown(function(e) {
|
||||
// if you don't setTimeout it won't grab the latest character if you call e.target.value
|
||||
window.setTimeout(function(){costco.handleEditorChange(e)}, 1, true);
|
||||
window.setTimeout( function() {
|
||||
var errors = $('.expression-preview-parsing-status');
|
||||
var editFunc = costco.evalFunction(e.target.value);
|
||||
if (!editFunc.errorMessage) {
|
||||
errors.text('No syntax error.');
|
||||
costco.previewTransform(app.cache, editFunc, app.currentColumn);
|
||||
} else {
|
||||
errors.text(editFunc.errorMessage);
|
||||
}
|
||||
}, 1, true);
|
||||
});
|
||||
editor.keydown();
|
||||
},
|
||||
transform: function() {
|
||||
$('.dialog-content .okButton').click(function(e) {
|
||||
util.notify("Not implemented yet, sorry! :D");
|
||||
util.hide('dialog');
|
||||
})
|
||||
|
||||
var editor = $('.expression-preview-code');
|
||||
editor.val("function(val) {\n if(_.isString(val)) this.update(\"pizza\")\n}");
|
||||
editor.focus().get(0).setSelectionRange(62,62);
|
||||
editor.keydown(function(e) {
|
||||
// if you don't setTimeout it won't grab the latest character if you call e.target.value
|
||||
window.setTimeout( function() {
|
||||
var errors = $('.expression-preview-parsing-status');
|
||||
var editFunc = costco.evalFunction(e.target.value);
|
||||
if (!editFunc.errorMessage) {
|
||||
errors.text('No syntax error.');
|
||||
var traverseFunc = function(doc) {
|
||||
util.traverse(doc).forEach(editFunc);
|
||||
return doc;
|
||||
}
|
||||
costco.previewTransform(app.cache, traverseFunc);
|
||||
} else {
|
||||
errors.text(editFunc.errorMessage);
|
||||
}
|
||||
}, 1, true);
|
||||
});
|
||||
editor.keydown();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user