Created
March 25, 2014 13:16
-
-
Save trengrj/9761604 to your computer and use it in GitHub Desktop.
really simple task manager internals
This file contains hidden or 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
var autofocus = autofocus || {}; | |
autofocus.tasks = []; | |
autofocus.colours = {}; | |
autofocus.save = function() { | |
localStorage.tasks = JSON.stringify(autofocus.tasks); | |
} | |
autofocus.load = function() { | |
if (localStorage.tasks) { | |
autofocus.tasks = JSON.parse(localStorage.tasks); | |
for (var i = 0; i < tasks.length; i++) { | |
autofocus.displayTask(tasks[i]); | |
} | |
} | |
} | |
autofocus.createTask = function(raw) { | |
var task = {}; | |
task.id = Date.now(); | |
task.raw = raw; | |
task.text = raw.replace(/#\S*/g, ''); | |
task.tags = raw.match(/#\S*/g); | |
task.status = "active"; | |
autofocus.tasks.push(task); | |
autofocus.save(); | |
return task; | |
} | |
autofocus.generateTags = function(tags) { | |
var text = ''; | |
if (!tags) { | |
return text; | |
} | |
for (var i = 0; i < tags.length; i++) { | |
var colour = (autofocus.colours[tags[i]] !== undefined) ? autofocus.colours[tags[i]] : Math.floor(Math.random()*4); | |
autofocus.colours[tags[i]] = colour; | |
text += ' <span class="label label-'+colour+'">'+h(tags[i].replace('#',''))+'</span>'; | |
} | |
return text; | |
} | |
autofocus.displayTask = function(task) { | |
var html = $("<li>"+task.text+"</li>"); | |
html.append(autofocus.generateTags(task.tags)); | |
if (task.status == "active") { | |
html.click(function() { | |
$(".tasks-completed").prepend($(this)); | |
autofocus.completeTask(task.id); | |
$(this).unbind(); | |
}); | |
$(".tasks").append(html); | |
} | |
else { | |
$(".tasks-completed").append(html); | |
} | |
} | |
autofocus.completeTask = function(id) { | |
for (var i = 0; i < tasks.length; i++) { | |
if (tasks[i].id == id) { | |
var task = tasks[i]; | |
task.status = "complete"; | |
tasks.splice(i,1); | |
tasks.unshift(task); | |
autofocus.save(); | |
} | |
} | |
} | |
autofocus.addTask = function() { | |
var task = autofocus.createTask($("#add-input").val()); | |
autofocus.displayTask(task); | |
$("#add-input").val('') | |
} | |
autofocus.init = function() { | |
$('#add-input').keydown(function (event) { | |
var keypressed = event.keyCode || event.which; | |
if (keypressed == 13) { | |
autofocus.addTask(); | |
} | |
}); | |
$("#add-button").click(function() { | |
autofocus.addTask(); | |
}) | |
} | |
$(function() { | |
autofocus.load(); | |
autofocus.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment