Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created March 19, 2014 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/9635235 to your computer and use it in GitHub Desktop.
Save morenoh149/9635235 to your computer and use it in GitHub Desktop.
'use strict';
app.controller('TodoCtrl', function($scope, $timeout, $ionicModal, Projects, $firebase, $ionicLoading) {
// Load projects
var projectsUrl = "https://ionic-guide-harry.firebaseio.com/projects/";
var projectRef = new Firebase(projectsUrl);
// query firebase for keys to projects
projectRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
console.log('projects location not accessible');
} else {
$scope.projectKeys = Object.keys(snapshot.val());
// grab the last active or the first project
var firebaseKeyRegEx = /^-[A-Za-z0-9]{19}$/;
var lastActiveProjectKey = Projects.getLastActiveKey();
if (firebaseKeyRegEx.test(lastActiveProjectKey)) {
// if the stored key matchs firebase format
$scope.activeProject = $scope.projects.$child(lastActiveProjectKey);
} else {
// else pick the first project available
$scope.activeProject = $scope.projects.$child($scope.projectKeys[0]);
}
}
});
// load activeProject tasks
var loadTasksFromKey = function(projectKey) {
var activeProjectTasksUrl = projectsUrl + projectKey + "/tasks/";
var tasksRef = new Firebase(activeProjectTasksUrl);
tasksRef.on('value', function(snapshot) {
if(snapshot.val() === null) {
console.log('tasks location not accessible');
} else {
$scope.taskKeys = Object.keys(snapshot.val());
$scope.activeTasks = $firebase(tasksRef);
}
});
};
loadTasksFromKey(Projects.getLastActiveKey());
// 3-way bind projects variable
$scope.projects = $firebase(projectRef);
$scope.loading = $ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 500
});
$scope.projects.$on("loaded", function() {
// tasks that need to be run once projects are loaded
$scope.loading.hide();
});
// utility function for creating a new project with the given projectTitle
var createProject = function(projectTitle) {
var newProject = Projects.newProject(projectTitle);
// add project to firebase projects, upon success select it by key
$scope.projects.$add(newProject).then( function(ref) {
$scope.selectProject(ref.name());
});
};
// Creates a new project
$scope.newProject = function() {
var projectTitle = prompt('Project name');
if(projectTitle) {
createProject(projectTitle);
}
};
// Selects the given project by it's firebase key
$scope.selectProject = function(key) {
$scope.activeProject = $scope.projects.$child(key);
loadTasksFromKey(key);
Projects.setLastActiveKey(key);
$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;
}
$scope.activeProject.$child('tasks').$add({
title: task.title,
finished: false
});
$scope.taskModal.hide();
// reset task title
task.title = "";
};
$scope.newTask = function() {
$scope.taskModal.show();
};
$scope.closeNewTask = function() {
$scope.taskModal.hide();
};
$scope.toggleProjects = function() {
$scope.sideMenuController.toggleLeft();
};
$scope.toggleTask = function(key) {
console.log($scope.activeTasks.$child(key));
if($scope.activeTasks.$child(key).finished) {
$scope.activeTasks.$child(key).finished = !$scope.activeTasks.$child(key).finished;
} else {
$scope.activeTasks.$child(key).finished = 'true';
}
console.log($scope.activeTasks.$child(key)['finished']);
};
// 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