From bbab8eb73c6514a15a9cbc4293826f3e23398594 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 28 Aug 2013 14:04:54 -0400 Subject: [PATCH] rewrite: fix sortables. we need to change the way this api route handles sorting, it's really inconsistent - especially for the batch-update routec --- assets/js/directives/directives.js | 11 ++++++----- src/controllers/api.coffee | 9 ++++----- views/index.jade | 7 +++++++ views/tasks/index.jade | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/assets/js/directives/directives.js b/assets/js/directives/directives.js index 8888249233..82ece6a016 100644 --- a/assets/js/directives/directives.js +++ b/assets/js/directives/directives.js @@ -54,19 +54,20 @@ habitrpg.directive('whenScrolled', function() { /** * Add sortable */ -habitrpg.directive('sort', function (User) { - return ['$scope', 'element', 'attrs', 'ngModel', function($scope, element, attrs, ngModel) { +habitrpg.directive('habitrpgSortable', function (User) { + return function($scope, element, attrs, ngModel) { $(element).sortable({ axis: "y", start: function (event, ui) { ui.item.data('startIndex', ui.item.index()); }, stop: function (event, ui) { - var taskType = angular.element(ui.item[0]).scope().task.type + 's' + var taskType = angular.element(ui.item[0]).scope().task.type + 's'; var startIndex = ui.item.data('startIndex'); var task = User.user[taskType][startIndex]; - User.log({op: 'sortTask', data: task, from: startIndex, to: ui.item.index()}); + // FIXME - this is a really inconsistent way of API handling. we need to fix the batch-update route + User.log({op: 'sortTask', data: _.defaults({from: startIndex, to: ui.item.index()}, task)}); } }); - }] + } }); diff --git a/src/controllers/api.coffee b/src/controllers/api.coffee index 080ca76771..8178d23f22 100644 --- a/src/controllers/api.coffee +++ b/src/controllers/api.coffee @@ -204,13 +204,13 @@ api.createTask = (req, res, next) -> api.sortTask = (req, res, next) -> {id} = req.params - {to, from, type} = res.locals.task + {to, from, type} = req.body {user} = res.locals path = "#{type}Ids" user[path].splice(to, 0, user[path].splice(from, 1)[0]) - user.save (err) -> + user.save (err, saved) -> return res.json(500,{err}) if err - res.json 200, user[path] + res.json 200, saved.toJSON()[path] ### ------------------------------------------------------------------------ @@ -377,7 +377,6 @@ api.revive = (req, res, next) -> ### api.batchUpdate = (req, res, next) -> {user} = res.locals - #console.log {user} oldSend = res.send oldJson = res.json @@ -402,7 +401,7 @@ api.batchUpdate = (req, res, next) -> when "buy" api.buy(req, res) when "sortTask" - api.verifyTaskExists (req, res) -> + api.verifyTaskExists req, res, -> api.sortTask(req, res) when "addTask" api.createTask(req, res) diff --git a/views/index.jade b/views/index.jade index 860d16ff07..1672e6dbca 100644 --- a/views/index.jade +++ b/views/index.jade @@ -31,6 +31,13 @@ html script(type='text/javascript', src='/bower_components/angular-bootstrap/ui-bootstrap.js') script(type='text/javascript', src='/bower_components/angular-bootstrap/ui-bootstrap-tpls.js') + // Sortable + script(type='text/javascript', src='/bower_components/jquery-ui/ui/minified/jquery.ui.core.min.js') + script(type='text/javascript', src='/bower_components/jquery-ui/ui/minified/jquery.ui.widget.min.js') + script(type='text/javascript', src='/bower_components/jquery-ui/ui/minified/jquery.ui.mouse.min.js') + script(type='text/javascript', src='/bower_components/jquery-ui/ui/minified/jquery.ui.sortable.min.js') + + script(type='text/javascript', src='/bower_components/habitrpg-shared/dist/habitrpg-shared.js') != js('app') diff --git a/views/tasks/index.jade b/views/tasks/index.jade index 5b6e985efc..8501e46e55 100644 --- a/views/tasks/index.jade +++ b/views/tasks/index.jade @@ -33,7 +33,7 @@ div(ng-controller='TasksCtrl') hr // Actual List - ul(class='{{list.type}}s', ng-show='user[list.type + "s"]') + ul(class='{{list.type}}s', ng-show='user[list.type + "s"]', habitrpg-sortable) include ./task // Static Rewards