2013-08-28 00:07:28 +00:00
"use strict" ;
2014-09-16 18:52:32 +00:00
habitrpg . controller ( "TasksCtrl" , [ '$scope' , '$rootScope' , '$location' , 'User' , 'Notification' , '$http' , 'ApiUrlService' , '$timeout' , 'Shared' ,
function ( $scope , $rootScope , $location , User , Notification , $http , ApiUrlService , $timeout , Shared ) {
2013-10-30 02:07:39 +00:00
$scope . obj = User . user ; // used for task-lists
2013-12-16 06:17:09 +00:00
$scope . user = User . user ;
2013-10-30 02:07:39 +00:00
2013-08-29 02:51:45 +00:00
$scope . score = function ( task , direction ) {
2014-08-09 08:29:56 +00:00
switch ( task . type ) {
case 'reward' :
$rootScope . playSound ( 'Reward' ) ;
break ;
case 'daily' :
$rootScope . playSound ( 'Daily' ) ;
break ;
case 'todo' :
$rootScope . playSound ( 'ToDo' ) ;
break ;
default :
if ( direction === 'down' ) $rootScope . playSound ( 'Minus_Habit' ) ;
else if ( direction === 'up' ) $rootScope . playSound ( 'Plus_Habit' ) ;
}
2013-12-11 16:30:39 +00:00
User . user . ops . score ( { params : { id : task . id , direction : direction } } )
2013-08-29 02:51:45 +00:00
} ;
2013-10-30 02:07:39 +00:00
$scope . addTask = function ( addTo , listDef ) {
2013-12-11 16:30:39 +00:00
var newTask = {
text : listDef . newTask ,
type : listDef . type ,
tags : _ . transform ( User . user . filters , function ( m , v , k ) {
if ( v ) m [ k ] = v ;
} )
}
User . user . ops . addTask ( { body : newTask } ) ;
2013-10-30 02:07:39 +00:00
delete listDef . newTask ;
2013-08-29 02:51:45 +00:00
} ;
2013-08-28 00:07:28 +00:00
2013-10-27 00:23:52 +00:00
/ * *
* Add the new task to the actions log
* /
2013-08-29 02:51:45 +00:00
$scope . clearDoneTodos = function ( ) { } ;
2013-10-27 00:23:52 +00:00
2014-09-09 07:04:13 +00:00
/ * *
* Pushes task to top or bottom of list
* /
$scope . pushTask = function ( task , index , location ) {
var to = ( location === 'bottom' ) ? - 1 : 0 ;
User . user . ops . sortTask ( { params : { id : task . id } , query : { from : index , to : to } } )
} ;
2013-10-27 00:23:52 +00:00
/ * *
* This is calculated post - change , so task . completed = true if they just checked it
* /
2013-08-29 02:51:45 +00:00
$scope . changeCheck = function ( task ) {
if ( task . completed ) {
$scope . score ( task , "up" ) ;
} else {
$scope . score ( task , "down" ) ;
2013-08-28 00:07:28 +00:00
}
2013-08-29 02:51:45 +00:00
} ;
2013-08-28 00:07:28 +00:00
2013-10-27 00:23:52 +00:00
$scope . removeTask = function ( list , $index ) {
2014-02-05 22:43:49 +00:00
if ( ! confirm ( window . env . t ( 'sureDelete' ) ) ) return ;
2013-12-11 16:30:39 +00:00
User . user . ops . deleteTask ( { params : { id : list [ $index ] . id } } )
2013-08-29 02:51:45 +00:00
} ;
2013-10-21 18:41:19 +00:00
2014-10-13 21:10:59 +00:00
$scope . saveTask = function ( task , stayOpen , isSaveAndClose ) {
2014-01-07 20:30:47 +00:00
if ( task . checklist )
2013-12-30 19:56:26 +00:00
task . checklist = _ . filter ( task . checklist , function ( i ) { return ! ! i . text } ) ;
2013-12-11 16:30:39 +00:00
User . user . ops . updateTask ( { params : { id : task . id } , body : task } ) ;
2013-12-30 19:56:26 +00:00
if ( ! stayOpen ) task . _editing = false ;
2014-10-13 21:10:59 +00:00
if ( isSaveAndClose )
$ ( "#task-" + task . id ) . parent ( ) . children ( '.popover' ) . removeClass ( 'in' ) ;
2013-10-21 18:41:19 +00:00
} ;
2013-10-27 00:23:52 +00:00
/ * *
* Reset $scope . task to $scope . originalTask
* /
$scope . cancel = function ( ) {
var key ;
for ( key in $scope . task ) {
$scope . task [ key ] = $scope . originalTask [ key ] ;
2013-08-29 02:51:45 +00:00
}
2013-10-27 00:23:52 +00:00
$scope . originalTask = null ;
$scope . editedTask = null ;
$scope . editing = false ;
2013-08-29 02:51:45 +00:00
} ;
2013-10-21 18:41:19 +00:00
2013-10-27 22:27:01 +00:00
$scope . unlink = function ( task , keep ) {
// TODO move this to userServices, turn userSerivces.user into ng-resource
2014-09-16 18:52:32 +00:00
$http . post ( ApiUrlService . get ( ) + '/api/v2/user/tasks/' + task . id + '/unlink?keep=' + keep )
2013-10-27 22:27:01 +00:00
. success ( function ( ) {
User . log ( { } ) ;
} ) ;
} ;
2013-12-30 06:49:19 +00:00
/ *
-- -- -- -- -- -- -- -- -- -- -- --
To - Dos
-- -- -- -- -- -- -- -- -- -- -- --
* /
2014-10-12 18:40:58 +00:00
$scope . _today = moment ( ) . add ( { days : 1 } ) ;
2013-12-30 06:49:19 +00:00
2013-12-30 04:10:27 +00:00
/ *
-- -- -- -- -- -- -- -- -- -- -- --
Checklists
-- -- -- -- -- -- -- -- -- -- -- --
* /
2013-12-30 20:19:42 +00:00
function focusChecklist ( task , index ) {
window . setTimeout ( function ( ) {
$ ( '#task-' + task . id + ' .checklist-form .inline-edit' ) [ index ] . focus ( ) ;
} ) ;
}
2013-12-30 04:10:27 +00:00
$scope . addChecklist = function ( task ) {
task . checklist = [ { completed : false , text : "" } ] ;
2013-12-30 20:19:42 +00:00
focusChecklist ( task , 0 ) ;
2013-12-30 04:10:27 +00:00
}
2013-12-30 19:56:26 +00:00
$scope . addChecklistItem = function ( task , $event , $index ) {
2014-01-07 19:55:41 +00:00
if ( ! task . checklist [ $index ] . text ) {
// Don't allow creation of an empty checklist item
// TODO Provide UI feedback that this item is still blank
} else if ( $index == task . checklist . length - 1 ) {
2013-12-30 19:56:26 +00:00
User . user . ops . updateTask ( { params : { id : task . id } , body : task } ) ; // don't preen the new empty item
2013-12-30 04:10:27 +00:00
task . checklist . push ( { completed : false , text : '' } ) ;
2013-12-30 20:19:42 +00:00
focusChecklist ( task , task . checklist . length - 1 ) ;
2013-12-30 19:56:26 +00:00
} else {
$scope . saveTask ( task , true ) ;
2013-12-30 20:19:42 +00:00
focusChecklist ( task , $index + 1 ) ;
2013-12-30 04:10:27 +00:00
}
}
2014-01-07 19:55:41 +00:00
$scope . removeChecklistItem = function ( task , $event , $index , force ) {
// Remove item if clicked on trash icon
if ( force ) {
2013-12-30 20:19:42 +00:00
task . checklist . splice ( $index , 1 ) ;
$scope . saveTask ( task , true ) ;
2014-01-07 19:55:41 +00:00
} else if ( ! task . checklist [ $index ] . text ) {
// User deleted all the text and is now wishing to delete the item
// saveTask will prune the empty item
$scope . saveTask ( task , true ) ;
// Move focus if the list is still non-empty
if ( $index > 0 )
focusChecklist ( task , $index - 1 ) ;
// Don't allow the backspace key to navigate back now that the field is gone
$event . preventDefault ( ) ;
}
2013-12-30 04:10:27 +00:00
}
2014-10-12 10:15:28 +00:00
$scope . swapChecklistItems = function ( task , oldIndex , newIndex ) {
var toSwap = task . checklist . splice ( oldIndex , 1 ) [ 0 ] ;
task . checklist . splice ( newIndex , 0 , toSwap ) ;
$scope . saveTask ( task , true ) ;
}
2013-12-30 20:42:19 +00:00
$scope . navigateChecklist = function ( task , $index , $event ) {
focusChecklist ( task , $event . keyCode == '40' ? $index + 1 : $index - 1 ) ;
}
2013-12-30 04:10:27 +00:00
$scope . checklistCompletion = function ( checklist ) {
return _ . reduce ( checklist , function ( m , i ) { return m + ( i . completed ? 1 : 0 ) ; } , 0 )
}
2013-12-31 21:03:16 +00:00
$scope . collapseChecklist = function ( task ) {
task . collapseChecklist = ! task . collapseChecklist ;
$scope . saveTask ( task , true ) ;
}
2013-12-30 04:10:27 +00:00
2013-08-29 02:51:45 +00:00
/ *
-- -- -- -- -- -- -- -- -- -- -- --
2013-12-17 05:38:17 +00:00
Items
2013-08-29 02:51:45 +00:00
-- -- -- -- -- -- -- -- -- -- -- --
2013-08-28 00:07:28 +00:00
* /
2013-12-16 05:38:08 +00:00
$scope . $watch ( 'user.items.gear.equipped' , function ( ) {
2014-01-12 23:05:15 +00:00
$scope . itemStore = Shared . updateStore ( User . user ) ;
2013-12-16 06:17:09 +00:00
} , true ) ;
2013-09-20 16:04:20 +00:00
2013-12-05 22:48:50 +00:00
$scope . buy = function ( item ) {
2013-12-15 21:49:22 +00:00
User . user . ops . buy ( { params : { key : item . key } } ) ;
2014-08-09 10:09:58 +00:00
$rootScope . playSound ( 'Reward' ) ;
2013-08-29 02:51:45 +00:00
} ;
2013-08-28 00:07:28 +00:00
2013-11-16 10:22:12 +00:00
/ *
-- -- -- -- -- -- -- -- -- -- -- --
Ads
-- -- -- -- -- -- -- -- -- -- -- --
* /
2013-10-27 00:23:52 +00:00
/ * *
* See conversation on http : //productforums.google.com/forum/#!topic/adsense/WYkC_VzKwbA,
* Adsense is very sensitive . It must be called once - and - only - once for every < ins > , else things break .
* Additionally , angular won 't run javascript embedded into a script template, so we can' t copy / paste
* the html provided by adsense - we need to run this function post - link
* /
$scope . initAds = function ( ) {
$ . getScript ( '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' ) ;
( window . adsbygoogle = window . adsbygoogle || [ ] ) . push ( { } ) ;
}
2014-06-08 07:05:16 +00:00
/ *
-- -- -- -- -- -- -- -- -- -- -- --
Hiding Tasks
-- -- -- -- -- -- -- -- -- -- -- --
* /
$scope . shouldShow = function ( task , list , prefs ) {
2014-10-10 12:14:43 +00:00
if ( task . _editing ) // never hide a task while being edited
return true ;
2014-10-19 08:04:28 +00:00
if ( task . type == 'todo' ) // TODO: convert To-Dos to use this new system and probably add a "Dated" column (i.e., "Incomplete" (includes dated), "Dated" (has due date and is not complete), "Complete")
2014-06-08 07:05:16 +00:00
return true ;
var shouldDo = task . type == 'daily' ? habitrpgShared . shouldDo ( new Date , task . repeat , prefs ) : true ;
switch ( list . view ) {
2014-10-19 08:04:28 +00:00
case "yellowred" : // Habits
return task . value < 1 ;
case "greenblue" : // Habits
return task . value >= 1 ;
case "remaining" : // Dailies and To-Dos
return ! task . completed && shouldDo ;
case "complete" : // Dailies and To-Dos
return task . completed || ! shouldDo ;
case "ingamerewards" : // All skills/rewards except the user's own
return false ; // Because "rewards" list includes only the user's own
case "all" :
return true ;
2014-06-08 07:05:16 +00:00
}
}
2013-10-19 23:15:48 +00:00
} ] ) ;