From c22c91ca59467aa8505b5d03847cf4dc6c99ce2c Mon Sep 17 00:00:00 2001 From: floydpraveen Date: Sat, 17 Aug 2013 13:17:27 +0530 Subject: [PATCH] clean up --- public/javascripts/views/list.js | 1 - public/javascripts/views/task.js | 39 ++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/public/javascripts/views/list.js b/public/javascripts/views/list.js index bd8521c..e0842a1 100644 --- a/public/javascripts/views/list.js +++ b/public/javascripts/views/list.js @@ -41,7 +41,6 @@ TaskView a.$el.insertAfter($('*[data-id="'+a.model.get('parent_id')+'"]').parents('li:first')); else this.$el.append(a.el); - console.log(a.$input.prop('selectionStart')); } }); diff --git a/public/javascripts/views/task.js b/public/javascripts/views/task.js index a02c1d1..55f508e 100644 --- a/public/javascripts/views/task.js +++ b/public/javascripts/views/task.js @@ -14,20 +14,20 @@ constants var TaskView = Backbone.View.extend({ - tagName: 'li', + tagName: 'li', template: $('#taskTemplate').html(), events: { 'click .task': 'edit', 'blur .edit': 'close', - 'keyup .edit': 'broadcast', + 'keyup .edit': 'handleKeyup', 'keypress .edit': 'update' }, initialize: function() { this.listenTo(this.model, 'change', this.render); this.listenTo(this.model, 'destroy', this.remove); - this.socket = io.connect(); + this.socket = io.connect(); }, render: function() { @@ -57,7 +57,29 @@ constants this.$input.focus(); }, - broadcast: function() { + handleKeyup: function(e) { + if (e.keyCode == 40) + this.$el.next('li').find('input').focus(); + else if (e.keyCode == 38) + this.$el.prev('li').find('input').focus(); + + if (e.shiftKey && e.keyCode == 9) { + var model = this.$el.next('li').find('input').data('id'); + model = Tasks.get(model); + var old_parent = model.get('parent_id'); + old_parent = Tasks.get(old_parent); + var new_parent = old_parent.get('parent_id'); + if (new_parent == null) new_parent = 0; + model.set('parent_id',new_parent); + model.save({content: model.get('content'), parent_id: model.get('parent_id')}); + } + else if (e.keyCode == 9) { + var parent = this.$el.prev('li').prev('li').find('input').data('id'); + var current = this.$el.prev('li').find('input').data('id'); + var model = Tasks.get(current); + model.set('parent_id',parent); + model.save({content: model.get('content'), parent_id: model.get('parent_id')}); + } this.socket.emit('task', { id: this.model.id, parent_id: this.model.parent_id, @@ -66,22 +88,21 @@ constants }, update: function(e) { - console.log("update here"); if ( e.which === constants.ENTER_KEY ) { Tasks.add({content:'', parent_id: this.model.get('parent_id')}); this.$input.blur(); - this.$el.prev('li').find('input').focus(); + this.$el.next('li').find('input').focus(); } }, close: function() { var value = this.$input.val().trim(); - console.log(this.model); if (value === '') { this.model.destroy(); } - else - this.model.save({content: value, parent_id: this.model.get('parent_id')}); + else { + this.model.save({content: value, parent_id: this.model.attributes.parent_id}); + } this.$el.removeClass('editing'); }