-
-
Save morenoh149/9537447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('todo', ['ionic', 'firebase']) | |
/** | |
* The Projects factory handles saving and loading projects | |
* from localStorage, and also lets us save and load the | |
* last active project index. | |
*/ | |
.factory('Projects', function() { | |
return { | |
all: function () { | |
var projectString = window.localStorage['projects']; | |
if(projectString) { | |
return angular.fromJson(projectString); | |
} | |
return []; | |
}, | |
// just saves all the projects everytime | |
save: function(projects) { | |
window.localStorage['projects'] = angular.toJson(projects); | |
}, | |
newProject: function(projectTitle) { | |
// Add a new project | |
return { | |
title: projectTitle, | |
tasks: [] | |
}; | |
}, | |
getLastActiveIndex: function () { | |
return parseInt(window.localStorage['lastActiveProject']) || 0; | |
}, | |
setLastActiveIndex: function (index) { | |
window.localStorage['lastActiveProject'] = index; | |
} | |
} | |
}) | |
.controller('TodoCtrl', function($scope, $timeout, $ionicModal, Projects, $firebase) { | |
// Load or initialize projects | |
//$scope.projects = Projects.all(); | |
var projectsUrl = "https://ionic-guide-harry.firebaseio.com/projects"; | |
var projectRef = new Firebase(projectsUrl); | |
$scope.projects = $firebase(projectRef); | |
$scope.projects.$on("loaded", function() { | |
var keys = $scope.projects.$getIndex(); | |
console.log($scope.projects.$child('-JGTmBu4aeToOSGmgCo1')); | |
// Grab the last active, or the first project | |
$scope.activeProject = $scope.projects.$child("" + keys[0]); | |
}); | |
// A utility function for creating a new project | |
// with the given projectTitle | |
var createProject = function(projectTitle) { | |
var newProject = Projects.newProject(projectTitle); | |
$scope.projects.$add(newProject); | |
Projects.save($scope.projects); | |
$scope.selectProject(newProject, $scope.projects.length-1); | |
}; | |
// Called to create a new project | |
$scope.newProject = function() { | |
var projectTitle = prompt('Project name'); | |
if(projectTitle) { | |
createProject(projectTitle); | |
} | |
}; | |
// Called to select the given project | |
$scope.selectProject = function(project, index) { | |
$scope.activeProject = project; | |
Projects.setLastActiveIndex(index); | |
$scope.sideMenuController.close(); | |
}; | |
// Create our modal | |
$ionicModal.fromTemplateUrl('new-task.html', function(modal) { | |
$scope.taskModal = modal; | |
}, { | |
scope: $scope | |
}); | |
$scope.createTask = function(task) { | |
if(!$scope.activeProject || !task) { | |
return; | |
} | |
console.log($scope.activeProject.task); | |
$scope.activeProject.task.$add({ | |
title: task.title | |
}); | |
$scope.taskModal.hide(); | |
// Inefficient, but save all the projects | |
Projects.save($scope.projects); | |
task.title = ""; | |
}; | |
$scope.newTask = function() { | |
$scope.taskModal.show(); | |
}; | |
$scope.closeNewTask = function() { | |
$scope.taskModal.hide(); | |
}; | |
$scope.toggleProjects = function() { | |
$scope.sideMenuController.toggleLeft(); | |
}; | |
// Try to create the first project, make sure to defer | |
// this by using $timeout so everything is initialized | |
// properly | |
$timeout(function() { | |
if($scope.projects.length == 0) { | |
while(true) { | |
var projectTitle = prompt('Your first project title:'); | |
if(projectTitle) { | |
createProject(projectTitle); | |
break; | |
} | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment