From 03f425b63d1ba8c2ef8dc77ccebc6e2a0c254b6f Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Wed, 3 Jun 2015 16:34:24 -0700 Subject: [PATCH] getSiblingOffset from vimflowy --- dist/flowy.js | 19 ++++++++++--------- dist/flowy.unwrapped.js | 19 ++++++++++--------- src/models/todo.js | 19 ++++++++++--------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/dist/flowy.js b/dist/flowy.js index 52463c0..32af476 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -744,20 +744,21 @@ var TodoModel = Backbone.Model.extend({ if (parent && parent.isRoot() && !options.rootAllowed) return undefined; return parent; }, - getPreviousSibling: function() { + getSiblingOffset: function(offset) { var parent = this.getParent(); if (!parent) return undefined; var index = parent.findChild(this.id); - if (index < 0 || index === 0) return undefined; - return parent.getChild(index - 1); + if (index < 0) return undefined; + var numChildren = parent.getChildrenCount(); + var siblingIndex = index + offset; + if (siblingIndex < 0 || siblingIndex >= numChildren) return undefined; + return parent.getChild(siblingIndex); + }, + getPreviousSibling: function() { + return this.getSiblingOffset(-1); }, getNextSibling: function() { - var parent = this.getParent(); - if (!parent) return undefined; - var index = parent.findChild(this.id); - var numChildren = parent.getChildrenCount(); - if (index < 0 || index === numChildren - 1) return undefined; - return parent.getChild(index + 1); + return this.getSiblingOffset(1); }, nextNode: function(options) { options = _.defaults({}, options, {childrenAllowed: true}); diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index e6babb8..7e9d913 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -743,20 +743,21 @@ var TodoModel = Backbone.Model.extend({ if (parent && parent.isRoot() && !options.rootAllowed) return undefined; return parent; }, - getPreviousSibling: function() { + getSiblingOffset: function(offset) { var parent = this.getParent(); if (!parent) return undefined; var index = parent.findChild(this.id); - if (index < 0 || index === 0) return undefined; - return parent.getChild(index - 1); + if (index < 0) return undefined; + var numChildren = parent.getChildrenCount(); + var siblingIndex = index + offset; + if (siblingIndex < 0 || siblingIndex >= numChildren) return undefined; + return parent.getChild(siblingIndex); + }, + getPreviousSibling: function() { + return this.getSiblingOffset(-1); }, getNextSibling: function() { - var parent = this.getParent(); - if (!parent) return undefined; - var index = parent.findChild(this.id); - var numChildren = parent.getChildrenCount(); - if (index < 0 || index === numChildren - 1) return undefined; - return parent.getChild(index + 1); + return this.getSiblingOffset(1); }, nextNode: function(options) { options = _.defaults({}, options, {childrenAllowed: true}); diff --git a/src/models/todo.js b/src/models/todo.js index ab6a810..11f9206 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -40,20 +40,21 @@ var TodoModel = Backbone.Model.extend({ if (parent && parent.isRoot() && !options.rootAllowed) return undefined; return parent; }, - getPreviousSibling: function() { + getSiblingOffset: function(offset) { var parent = this.getParent(); if (!parent) return undefined; var index = parent.findChild(this.id); - if (index < 0 || index === 0) return undefined; - return parent.getChild(index - 1); + if (index < 0) return undefined; + var numChildren = parent.getChildrenCount(); + var siblingIndex = index + offset; + if (siblingIndex < 0 || siblingIndex >= numChildren) return undefined; + return parent.getChild(siblingIndex); + }, + getPreviousSibling: function() { + return this.getSiblingOffset(-1); }, getNextSibling: function() { - var parent = this.getParent(); - if (!parent) return undefined; - var index = parent.findChild(this.id); - var numChildren = parent.getChildrenCount(); - if (index < 0 || index === numChildren - 1) return undefined; - return parent.getChild(index + 1); + return this.getSiblingOffset(1); }, nextNode: function(options) { options = _.defaults({}, options, {childrenAllowed: true}); -- 2.47.3