rewrite: fix sortables. we need to change the way this api route handles

sorting, it's really inconsistent - especially for the batch-update routec
This commit is contained in:
Tyler Renelle 2013-08-28 14:04:54 -04:00
parent 1b22928c0a
commit bbab8eb73c
4 changed files with 18 additions and 11 deletions

View file

@ -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)});
}
});
}]
}
});

View file

@ -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)

View file

@ -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')

View file

@ -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