From 6145a938e365d8c7cbb5f79f47c8f11a1bef066c Mon Sep 17 00:00:00 2001 From: Joe Friedl Date: Mon, 19 Dec 2011 11:44:12 -0600 Subject: [PATCH] Error messages in the UI --- lib/document_handler.js | 10 +++++----- static/application.css | 23 +++++++++++++++++++++-- static/application.js | 26 +++++++++++++++++++++++--- static/application.min.js | 2 +- static/index.html | 1 + 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index f30defa..ba6ec68 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -24,7 +24,7 @@ DocumentHandler.prototype.handleGet = function(key, response, skipExpire) { else { winston.warn('document not found', { key: key }); response.writeHead(404, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'document not found' })); + response.end(JSON.stringify({ message: 'Document not found.' })); } }, skipExpire); }; @@ -40,7 +40,7 @@ DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) { else { winston.warn('raw document not found', { key: key }); response.writeHead(404, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'document not found' })); + response.end(JSON.stringify({ message: 'Document not found.' })); } }, skipExpire); }; @@ -58,7 +58,7 @@ DocumentHandler.prototype.handlePost = function(request, response) { _this.cancelled = true; winston.warn('document >maxLength', { maxLength: _this.maxLength }); response.writeHead(400, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'document exceeds maximum length' })); + response.end(JSON.stringify({ message: 'Document exceeds maximum length.' })); } }); request.on('end', function(end) { @@ -72,7 +72,7 @@ DocumentHandler.prototype.handlePost = function(request, response) { else { winston.verbose('error adding document'); response.writeHead(500, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'error adding document' })); + response.end(JSON.stringify({ message: 'Error adding document.' })); } }); }); @@ -80,7 +80,7 @@ DocumentHandler.prototype.handlePost = function(request, response) { request.on('error', function(error) { winston.error('connection error: ' + error.message); response.writeHead(500, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'connection error' })); + response.end(JSON.stringify({ message: 'Connection error.' })); }); }; diff --git a/static/application.css b/static/application.css index 9a17b10..7587eed 100644 --- a/static/application.css +++ b/static/application.css @@ -110,7 +110,7 @@ textarea { right: 0px; } -#box3 { +#box3, #messages li { background: #173e48; font-family: Helvetica, sans-serif; font-size: 12px; @@ -118,7 +118,7 @@ textarea { padding: 10px 15px; } -#box3 .label { +#box3 .label, #messages li { color: #fff; font-weight: bold; } @@ -147,3 +147,22 @@ textarea { #box2 .function.twitter { background-position: -153px top; } #box2 .function.enabled.twitter { background-position: -153px center; } #box2 .function.enabled.twitter:hover { background-position: -153px bottom; } + +#messages { + position:fixed; + top:0px; + right:138px; + margin:0; + padding:0; + width:400px; +} + +#messages li { + background:rgba(23,62,72,0.8); + margin:0 auto; + list-style:none; +} + +#messages li.error { + background:rgba(102,8,0,0.8); +} diff --git a/static/application.js b/static/application.js index bd9f446..e21f447 100644 --- a/static/application.js +++ b/static/application.js @@ -57,12 +57,20 @@ haste_document.prototype.save = function(data, callback) { _this.locked = true; _this.key = res.key; var high = hljs.highlightAuto(data); - callback({ + callback(null, { value: high.value, key: res.key, language: high.language, lineCount: data.split("\n").length }); + }, + error: function(res) { + try { + callback($.parseJSON(res.responseText)); + } + catch (e) { + callback({message: 'Something went wrong!'}); + } } }); }; @@ -91,6 +99,15 @@ haste.prototype.setTitle = function(ext) { document.title = title; }; +// Show a message box +haste.prototype.showMessage = function(msg, cls) { + var msgBox = $('
  • '+msg+'
  • '); + $('#messages').prepend(msgBox); + setTimeout(function() { + msgBox.slideUp('fast', function() { $(this).remove(); }); + }, 3000); +}; + // Show the light key haste.prototype.lightKey = function() { this.configureKey(['new', 'save']); @@ -209,8 +226,11 @@ haste.prototype.duplicateDocument = function() { // Lock the current document haste.prototype.lockDocument = function() { var _this = this; - this.doc.save(this.$textarea.val(), function(ret) { - if (ret) { + this.doc.save(this.$textarea.val(), function(err, ret) { + if (err) { + _this.showMessage(err.message, 'error'); + } + else if (ret) { _this.$code.html(ret.value); _this.setTitle(ret.key); var file = '/' + ret.key; diff --git a/static/application.min.js b/static/application.min.js index 7f3ac9b..85a3a9b 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -var haste_document=function(){this.locked=!1};haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f;c==="txt"?f={value:e.data}:c?f=hljs.highlight(c,e.data):f=hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c,lineCount:e.data.split("\n").length})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b({value:e.value,key:d.key,language:e.language,lineCount:a.split("\n").length})}})};var haste=function(a,b){this.appName=a,this.baseUrl=window.location.href,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b){if(b){a.$code.html(b.value),a.setTitle(b.key);var c="/"+b.key;b.language&&(c+="."+a.lookupExtensionByType(b.language)),window.history.pushState(null,a.appName+"-"+b.key,c),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(b.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;b'+a+"");$("#messages").prepend(c),setTimeout(function(){c.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b,c){if(b)a.showMessage(b.message,"error");else if(c){a.$code.html(c.value),a.setTitle(c.key);var d="/"+c.key;c.language&&(d+="."+a.lookupExtensionByType(c.language)),window.history.pushState(null,a.appName+"-"+c.key,d),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(c.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;b +