Implemented basic error handling for CRUD operations.
h3rald h3rald@h3rald.com
Sun, 15 Mar 2015 12:58:50 +0100
7 files changed,
21 insertions(+),
155 deletions(-)
M
app/index.html
→
app/index.html
@@ -35,6 +35,7 @@ <script src="js/models.js"> </script>
<script src="js/components.js"> </script> <script src="js/navbar.js"> </script> <script src="js/modules.js"> </script> + <script src="js/modules/document.js"> </script> <script src="js/app.js"> </script> </body> </html>
M
app/js/components.js
→
app/js/components.js
@@ -35,4 +35,4 @@ app.editor.view = function(obj) {
return m(".editor.panel.panal-default", {config: app.editor.config(obj)}, obj.content); }; -}());+}());));
M
app/js/modules.js
→
app/js/modules.js
@@ -16,6 +16,12 @@ m("dt", "Version"),
m("dd", info.version), m("dt", "Size"), m("dd", info.size), + m("dt", "Loaded directory"), + m("dd", info.directory), + m("dt", "Mirroring"), + m("dd", info.mirror), + m("dt", "Read-only"), + m("dd", info.read_only), m("dt", "Total Documents"), m("dd", info.total_documents), m("dt", "Total Tags"),@@ -69,155 +75,6 @@ table
]); }; - // Document module - app.document = {vm: {}}; - app.document.vm.init = function() { - var vm = this; - vm.id = m.prop(m.route.param("id")); - vm.action = m.route.param("action"); - vm.readOnly = true; - vm.contentType = m.prop(""); - vm.existingId = m.prop(); - vm.existingContentType = m.prop(); - vm.existingContent = m.prop(); - try { - vm.ext = vm.id().match(/\.(.+)$/)[1]; - } catch(e) { - vm.ext = ""; - } - vm.getDoc = function(cb){ - vm.doc = Doc.get(vm.id()); - vm.doc.then(function(doc){ - vm.content = doc.data; - vm.tags = doc.tags; - }); - }; - vm.tags = []; - switch (vm.action) { - case 'create': - vm.readOnly = false; - if (vm.existingId()) { - vm.id(vm.existingId()); - vm.contentType(vm.existingContentType()); - vm.content(vm.existingContent()); - vm.existingId = m.prop(); - vm.existingContentType = m.prop(); - vm.existingContent = m.prop(); - } - break; - case 'edit': - vm.getDoc(); - vm.readOnly = false; - break; - case 'view': - vm.getDoc(); - break; - } - switch (vm.ext){ - case 'js': - vm.mode = "javascript"; - break; - case 'css': - vm.mode = "css"; - break; - case 'html': - vm.mode = "html"; - break; - case 'json': - vm.mode = "json"; - break; - case 'md': - vm.mode = "markdown"; - break; - default: - vm.mode = "text"; - } - vm.edit = function(){ - m.route("/document/edit/"+vm.id()); - }; - vm.save = function(){ - var doc = {}; - doc.id = vm.id(); - doc.data = vm.editor.getValue(); - doc.tags = vm.tags; - var put = function(){ - Doc.put(doc, vm.contentType()).then(function(){ - LS.flash({type: "success", content: "Document saved successfully."}); - m.route("/document/view/"+vm.id()); - }); - }; - if (vm.action === "create") { - Doc.get(vm.id()) - .then(function(){ - LS.flash({type: "danger", content: "Document '"+vm.id()+"' already exists."}); - vm.existingContent(doc.data); - vm.existingContentType(vm.contentType()); - vm.existingId(vm.id()); - m.route(m.route()); - }, function(){put();}); - } else { - put(); - } - }; - vm.delete = function(){ - var msg = "Do you want to delete document '"+vm.id()+"'?"; - if (confirm(msg)) { - Doc.delete(vm.id()).then(function(){ - LS.flash({type: "success", content: "Document '"+vm.id()+"' deleted successfully."}); - m.route("/info"); - }); - } else { - m.route("/document/view/"+vm.id()); - } - }; - vm.cancel = function(){ - if (vm.action === "create"){ - m.route("/info"); - } else { - m.route("/document/view/"+vm.id()); - } - }; - vm.tools = function(){ - switch (vm.action){ - case "view": - return [ - {title: "Edit", icon: "edit", action: vm.edit}, - {title: "Delete", icon: "trash", action: vm.delete} - ]; - default: - return [ - {title: "Save", icon: "save", action: vm.save}, - {title: "Cancel", icon: "times-circle", action: vm.cancel} - ]; - } - }; - }; - app.document.main = function(){ - var vm = app.document.vm; - var titleLeft = vm.id(); - var titleRight = m("span.pull-right", vm.tags.map(function(t){return u.taglink(t);})); - if (vm.action === "create"){ - titleLeft = m("input", { - placeholder: "Specify document ID...", - onchange: m.withAttr("value", vm.id), - size: 35, - value: vm.id() - }); - titleRight = m("span.pull-right", [m("input", { - placeholder: "Specify content type...", - onchange: m.withAttr("value", vm.contentType), - size: 20, - value: vm.contentType() - })]); - } - var title = m("span",[titleLeft, titleRight]); - return m("div", [ - m(".row", [u.toolbar({links: vm.tools()})]), - m(".row", [u.panel({title: title, content:app.editor.view(vm)})]) - ]); - }; - - // Guide Module app.guide = {vm: {}}; app.guide.vm.init = function() {@@ -232,9 +89,7 @@
u.layout(app.guide); u.layout(app.info); u.layout(app.tags); - u.layout(app.document); -}());tags); - u.layout(app.document); +}());yout(app.document); -}());));+}());
M
app/js/utils.js
→
app/js/utils.js
@@ -13,8 +13,14 @@
mod.controller = mod.controller || function(){ this.navbar = new app.navbar.controller(); mod.vm.init(); + // Display flash if set on previous route mod.vm.flash = m.prop(u.flash()); LS.flash = m.prop(); + mod.vm.showFlash = function(obj){ + LS.flash(obj); + mod.vm.flash(u.flash()); + LS.flash = m.prop(); + }; }; mod.view = function(ctrl){
M
lib/api_v1.nim
→
lib/api_v1.nim
@@ -181,6 +181,9 @@ let tags = LS.store.retrieveTagsWithTotals()
var content = newJObject() content["version"] = %(LS.appname & " v" & LS.appversion) content["size"] = %($((LS.file.getFileSize().float/(1024*1024)).formatFloat(ffDecimal, 2)) & " MB") + content["read_only"] = %LS.readonly + content["directory"] = %LS.directory + content["mirror"] = %LS.mirror content["total_documents"] = %total_docs content["total_tags"] = %total_tags content["tags"] = tags
M
lib/core.nim
→
lib/core.nim
@@ -126,7 +126,8 @@ # Add dir tag
store.createTag("$dir:"&store.mirror, id, true) var filename = id.unixToNativePath if not fileExists(filename): - var file = filename.open(fmWrite) + filename.parentDir.createDir + var file = filename.open(fmReadWrite) file.write(rawdata) else: raise newException(EFileExists, "File already exists: $1" % filename)