Difference between revisions of "BZtodo"
(Created page with "running on http://cascada.sitebuilt.net/BZtodo/client/ <source lang="javascript"> $( document ).ready(function() { ctrl.loadList(); console.log(ctrl.priorityOptions()); ...") |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
running on http://cascada.sitebuilt.net/BZtodo/client/ | running on http://cascada.sitebuilt.net/BZtodo/client/ | ||
:by Timothy S. McKenna | |||
:mckenna.tim@gmail.com | |||
:(857)498-2574 | |||
:http://mckennatim.github.io/mckennatim/ | |||
:https://github.com/mckennatim/BZtodo | |||
====[https://github.com/mckennatim/BZtodo/blob/master/client/js/main.js main.js]==== | |||
<source lang="javascript"> | |||
/*----------------------------Events-------------------------*/ | |||
$( document ).ready(function() { | $( document ).ready(function() { | ||
model.loadList(); | |||
$('form').submit(function(){ | $('form').submit(function(){ | ||
ctrl.add2list(); | |||
}); | }); | ||
$('input[type="checkbox"]').live('click',function(){ | $('input[type="checkbox"]').live('click',function(){ | ||
ctrl.updDone($(this)); | ctrl.updDone($(this)); | ||
}) | }) | ||
$('.priority').live('change', function(){ | |||
$(' | |||
ctrl.updPriority($(this)); | ctrl.updPriority($(this)); | ||
}) | }) | ||
$('a').live('click', function(){ | $('a').live('click', function(){ | ||
ctrl.deleteItem($(this)); | ctrl.deleteItem($(this)); | ||
}) | }) | ||
$('#sortDoneBut').click(function(){ | $('#sortDoneBut').click(function(){ | ||
ctrl.sortOnDone(); | ctrl.sortOnDone(); | ||
}) | }) | ||
$('#sortPriorBut').click(function(){ | $('#sortPriorBut').click(function(){ | ||
ctrl.sortOnPriority(); | ctrl.sortOnPriority(); | ||
}) | }) | ||
}); | |||
/*----------------------------Model-------------------------*/ | |||
var | var model = { | ||
list: {BZtodo:[]}, | list: {BZtodo:[]}, | ||
priority: ['high', 'normal', 'low'], | priority: ['high', 'normal', 'low'], | ||
defaultPriority: 'normal', | defaultPriority: 'normal', | ||
urlqstr: 'list=BZtodo', | |||
defaultPriorityIdx: 1, | |||
saveList: function(){ | saveList: function(){ | ||
var listJ=JSON.stringify(this.list); | var listJ=JSON.stringify(this.list); | ||
localStorage.setItem('BZtodo', listJ); | localStorage.setItem('BZtodo', listJ); | ||
itemsJ = JSON.stringify(this.list.BZtodo) | |||
$.post("../server/post.php?"+this.urlqstr, {data: itemsJ}).done(function(data){}); | |||
}, | }, | ||
loadList: function(){ | loadList: function(){ | ||
$.get('../server/get.php', this.urlqstr, function(dataJ) { | |||
if (!(dataJ==false)){ | |||
$. | var data =JSON.parse(dataJ); | ||
var | var items = JSON.parse(data.items); | ||
localStorage.setItem('BZtodo',JSON.stringify( | model.list.BZtodo = items; | ||
}); | view.refreshList(model.list.BZtodo); | ||
localStorage.setItem('BZtodo',JSON.stringify(model.list)); | |||
if( | } | ||
}); | |||
} | |||
} | } | ||
/*----------------------------Controller-------------------------*/ | |||
var ctrl ={ | |||
add2list:function(item){ | |||
event.preventDefault(); | |||
console.log($('#inpTxt').val()) | |||
var newItem = $('input').val(); | |||
if(newItem.length>1){ | |||
var itemObject ={ | |||
item: newItem, | |||
done: false, | |||
priority: model.defaultPriorityIdx | |||
} | |||
view.appendLi(itemObject); | |||
model.list.BZtodo.push(itemObject); | |||
model.saveList(); | |||
} | |||
$('#inpTxt').val(''); | |||
}, | }, | ||
updDone: function(el){ | updDone: function(el){ | ||
Line 96: | Line 84: | ||
done=false | done=false | ||
} | } | ||
model.list.BZtodo[el.parent().index()].done=done; | |||
model.saveList(); | |||
}, | }, | ||
updPriority: function(el){ | updPriority: function(el){ | ||
model.list.BZtodo[el.parent().parent().index()].priority=el.val()*1; | |||
model.saveList(); | |||
}, | }, | ||
deleteItem: function(el){ | deleteItem: function(el){ | ||
var idx = el.parent().index(); | event.preventDefault(); | ||
var idx = el.parent().parent().index(); | |||
if (idx > -1) { | if (idx > -1) { | ||
model.list.BZtodo.splice(idx, 1); | |||
} | } | ||
view.refreshList( | view.refreshList(model.list.BZtodo); | ||
model.saveList(); | |||
}, | }, | ||
sortOnDone: function(){ | sortOnDone: function(){ | ||
event.preventDefault(); | |||
model.list.BZtodo.sort(function(a,b){ | |||
return a.done-b.done | return a.done-b.done | ||
}) | }) | ||
view.refreshList(model.list.BZtodo); | |||
view.refreshList( | model.saveList(); | ||
}, | }, | ||
sortOnPriority: function(){ | sortOnPriority: function(){ | ||
event.preventDefault(); | |||
model.list.BZtodo.sort(function(a,b){ | |||
return a.priority-b.priority | return a.priority-b.priority | ||
}) | }) | ||
view.refreshList(model.list.BZtodo); | |||
view.refreshList( | model.saveList(); | ||
} | } | ||
} | } | ||
/*----------------------------View-------------------------*/ | |||
var view ={ | var view ={ | ||
Line 137: | Line 127: | ||
var checked=''; | var checked=''; | ||
if(itemObj.done){checked='checked="checked"'} | if(itemObj.done){checked='checked="checked"'} | ||
$('ul').append('<li>' | $('ul').append('<li class="list-group-item">' | ||
+'<input type="checkbox"'+checked+'/>' | +'<input type="checkbox"'+checked+'/>' | ||
+'<span class="item"> '+ item +'</span>' | +'<span class="item"> '+ item +'</span>' | ||
+ | +'<span class="list-right">' | ||
+'<a href=""> | + this.priorityOptions(priority) | ||
+'<a href="">' | |||
+'<span class="glyphicon glyphicon-remove-sign"></span>' | |||
+'</a></span>' | |||
+'</li>'); | +'</li>'); | ||
}, | }, | ||
Line 149: | Line 142: | ||
view.appendLi(itemObj); | view.appendLi(itemObj); | ||
}); | }); | ||
} | }, | ||
priorityOptions: function(pridx){ | |||
var ostr= '<select class="priority">'; | |||
$.each(model.priority, function(index, opt){ | |||
if(index==pridx){ | |||
ostr+= '<option value='+index+' selected="selected">'+opt+'</option>'; | |||
}else{ | |||
ostr+= '<option value='+index+'>'+opt+'</option>'; | |||
} | |||
}) | |||
ostr+='</select>'; | |||
return ostr; | |||
} | |||
} | |||
</source> | |||
====[https://github.com/mckennatim/BZtodo/blob/master/client/index.html index.html]==== | |||
<source lang="html5"> | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>BZtodo</title> | |||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> | |||
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> | |||
<link rel="stylesheet" type="text/css" href="css/main.css"> | |||
<script type="text/javascript" src="js/main.js"></script> | |||
</head> | |||
<body> | |||
<div class="outer"> | |||
<div> | |||
<div> | |||
<form> | |||
<input type="text" id="inpTxt" placeholder="new todo item"/> | |||
<input type="submit" id="addBtn" value="Add" /> | |||
</form> | |||
</div> | |||
<div class="button-group"> | |||
<button id="sortDoneBut">sort by complete</button> | |||
<button id="sortPriorBut">sort by priority</button> | |||
</div> | |||
<div> | |||
<ul class="list-group"> | |||
<!--produced by view | |||
` <li class="list-group-item"> | |||
<input class="ckbox" type="checkbox"'+checked+'/> | |||
<span class="item">the default task</span> | |||
<span class="list-right"> | |||
<select class="priority"> | |||
<option value="0">low</option> | |||
<option value="0">medium</option> | |||
<option value="0">high</option> | |||
</select> | |||
<a href=""> | |||
<span class="glyphicon glyphicon-remove-sign"></span> | |||
</a> | |||
</span> | |||
</li> | |||
--> | |||
</ul> | |||
</div> | |||
</div> | |||
<div id="htest"></div> | |||
</body> | |||
</html> | |||
</source> | |||
====[https://github.com/mckennatim/BZtodo/blob/master/server/post.php post.php]==== | |||
<source lang="php"> | |||
<?php | |||
include('/usr/local/lib/tm/ChromePhp.php'); | |||
include '/usr/local/lib/tm/db.php'; | |||
$list=$_GET['list']; | |||
if(isset($_POST['data'])){ | |||
$blob=$_POST['data']; | |||
ChromePhp::log($blob); | |||
}else{ | |||
$blob='a blob'; | |||
} | |||
ChromePhp::log($list); | |||
$db = new Db('test'); | |||
$dbh = new PDO("mysql:host=$db->host; dbname=$db->database",$db->user, $db->pass); | |||
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |||
try { | |||
$dsql = "DELETE FROM `todos` WHERE `list`= ?"; | |||
$stmt = $dbh->prepare($dsql); | |||
$stmt->execute(array($list)); | |||
} catch(PDOException $e) { | |||
echo '{"error":{"text":'. $e->getMessage() .$dsql.'}}'; | |||
} | |||
try { | |||
$sql = "INSERT INTO `todos` (`list`, `items`) VALUES (?, ?)"; | |||
$stmt = $dbh->prepare($sql); | |||
$stmt->execute(array($list, $blob)); | |||
} catch(PDOException $e) { | |||
echo '{"error":{"text":'. $e->getMessage() .$sql.'}}'; | |||
} | |||
?> | |||
</source> | |||
====[https://github.com/mckennatim/BZtodo/blob/master/server/get.php get.php]==== | |||
<source lang="php"> | |||
<?php | |||
include('/usr/local/lib/tm/ChromePhp.php'); | |||
include '/usr/local/lib/tm/db.php'; | |||
$list=mysql_real_escape_string($_GET['list']); | |||
ChromePhp::log($list); | |||
$db= new Db('test'); | |||
try { | |||
$dbh = new PDO("mysql:host=$db->host; dbname=$db->database",$db->user, $db->pass); | |||
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |||
$sql = 'SELECT `items` FROM `todos` WHERE list=?'; | |||
$stmt = $dbh->prepare($sql); | |||
$stmt->execute(array($list)); | |||
$result = $stmt->fetch(PDO::FETCH_OBJ); | |||
//print_r($result); | |||
} catch(PDOException $e) { | |||
echo '{"error":{"text":'. $e->getMessage() .$sql.'}}'; | |||
} | } | ||
$retJSON= json_encode($result); | |||
echo $retJSON; | |||
?> | |||
</source> | </source> |
Latest revision as of 15:07, 5 April 2014
running on http://cascada.sitebuilt.net/BZtodo/client/
- by Timothy S. McKenna
- mckenna.tim@gmail.com
- (857)498-2574
- http://mckennatim.github.io/mckennatim/
- https://github.com/mckennatim/BZtodo
main.js
<source lang="javascript"> /*----------------------------Events-------------------------*/
$( document ).ready(function() {
model.loadList(); $('form').submit(function(){ ctrl.add2list(); }); $('input[type="checkbox"]').live('click',function(){ ctrl.updDone($(this)); }) $('.priority').live('change', function(){ ctrl.updPriority($(this)); }) $('a').live('click', function(){ ctrl.deleteItem($(this)); }) $('#sortDoneBut').click(function(){ ctrl.sortOnDone(); }) $('#sortPriorBut').click(function(){ ctrl.sortOnPriority(); })
});
/*----------------------------Model-------------------------*/
var model = {
list: {BZtodo:[]}, priority: ['high', 'normal', 'low'], defaultPriority: 'normal', urlqstr: 'list=BZtodo', defaultPriorityIdx: 1, saveList: function(){ var listJ=JSON.stringify(this.list); localStorage.setItem('BZtodo', listJ); itemsJ = JSON.stringify(this.list.BZtodo) $.post("../server/post.php?"+this.urlqstr, {data: itemsJ}).done(function(data){}); }, loadList: function(){ $.get('../server/get.php', this.urlqstr, function(dataJ) { if (!(dataJ==false)){ var data =JSON.parse(dataJ); var items = JSON.parse(data.items); model.list.BZtodo = items; view.refreshList(model.list.BZtodo); localStorage.setItem('BZtodo',JSON.stringify(model.list)); } }); }
}
/*----------------------------Controller-------------------------*/
var ctrl ={
add2list:function(item){ event.preventDefault(); console.log($('#inpTxt').val()) var newItem = $('input').val(); if(newItem.length>1){ var itemObject ={ item: newItem, done: false, priority: model.defaultPriorityIdx } view.appendLi(itemObject); model.list.BZtodo.push(itemObject); model.saveList(); } $('#inpTxt').val(); }, updDone: function(el){ var done; if(el.prop('checked')){ done=true; }else{ done=false } model.list.BZtodo[el.parent().index()].done=done; model.saveList(); }, updPriority: function(el){ model.list.BZtodo[el.parent().parent().index()].priority=el.val()*1; model.saveList(); }, deleteItem: function(el){ event.preventDefault(); var idx = el.parent().parent().index(); if (idx > -1) { model.list.BZtodo.splice(idx, 1); } view.refreshList(model.list.BZtodo); model.saveList(); }, sortOnDone: function(){ event.preventDefault(); model.list.BZtodo.sort(function(a,b){ return a.done-b.done }) view.refreshList(model.list.BZtodo); model.saveList(); }, sortOnPriority: function(){ event.preventDefault(); model.list.BZtodo.sort(function(a,b){ return a.priority-b.priority }) view.refreshList(model.list.BZtodo); model.saveList(); }
}
/*----------------------------View-------------------------*/
var view ={
appendLi: function(itemObj){ var item = itemObj.item; var priority = itemObj.priority; var checked=; if(itemObj.done){checked='checked="checked"'}
$('ul').append('
'); }, refreshList: function(list){ $('ul').empty(); $.each(list, function(index, itemObj) { view.appendLi(itemObj); }); }, priorityOptions: function(pridx){ var ostr= '<select class="priority">'; $.each(model.priority, function(index, opt){ if(index==pridx){ ostr+= '<option value='+index+' selected="selected">'+opt+'</option>'; }else{ ostr+= '<option value='+index+'>'+opt+'</option>'; } }) ostr+='</select>'; return ostr; } } </source>
index.html
<source lang="html5"> <!DOCTYPE html>
post.php
<source lang="php"> <?php include('/usr/local/lib/tm/ChromePhp.php'); include '/usr/local/lib/tm/db.php'; $list=$_GET['list']; if(isset($_POST['data'])){
$blob=$_POST['data']; ChromePhp::log($blob);
}else{
$blob='a blob';
}
ChromePhp::log($list);
$db = new Db('test');
$dbh = new PDO("mysql:host=$db->host; dbname=$db->database",$db->user, $db->pass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$dsql = "DELETE FROM `todos` WHERE `list`= ?"; $stmt = $dbh->prepare($dsql); $stmt->execute(array($list));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .$dsql.'}}';
}
try {
$sql = "INSERT INTO `todos` (`list`, `items`) VALUES (?, ?)"; $stmt = $dbh->prepare($sql); $stmt->execute(array($list, $blob));
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .$sql.'}}';
} ?> </source>
get.php
<source lang="php"> <?php include('/usr/local/lib/tm/ChromePhp.php'); include '/usr/local/lib/tm/db.php'; $list=mysql_real_escape_string($_GET['list']); ChromePhp::log($list);
$db= new Db('test');
try {
$dbh = new PDO("mysql:host=$db->host; dbname=$db->database",$db->user, $db->pass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = 'SELECT `items` FROM `todos` WHERE list=?'; $stmt = $dbh->prepare($sql); $stmt->execute(array($list)); $result = $stmt->fetch(PDO::FETCH_OBJ); //print_r($result);
} catch(PDOException $e) { echo '{"error":{"text":'. $e->getMessage() .$sql.'}}'; }
$retJSON= json_encode($result); echo $retJSON; ?> </source>