date.js magic
[hyena.git] / app / hyena.js
blob6654ea31296569bf72ba5495b7fef964172cadf7
1 /*
2  * Hyena - personal portal system
3  */
5 $(document).ready(function(){
7   function syncTo(data){
8         var json = JSON.stringify(data);
9         $.post('/', {json: json}, function(data, textStatus){
10           console.debug('sync %o', data);
11         });
12   }
14   function removeAction(data, text){
15         data.actions = $.grep(data.actions, function(action){
16           return (action.text != text);
17         });
18   }
20   function addAction(data, action){
21         data.actions.push(action);
22   } 
24   function render(data){
25         gdata = data; // debug
26         
27         var html = TrimPath.processDOMTemplate('t-home', data);
28         $('#site').html(html);
30         $('.na.delete').click(function(){
31           console.debug('ui: delete clicked');
32           var div = $(this).parent('div.na');
33           var na  = div.find('.text');
34           removeAction(data, na.text());
35           div.fadeOut(function(){$(this).remove();});
36           syncTo(data);
37           $('#na-input').focus();
38           return false;
39         });
41         $('#na-input').keypress(function (e){
42           if (e.which == 13){
43                 console.log('enter');
44                 console.debug(this);
45                 addAction(data, {
46                   text: this.value,
47                   type: 'errand',
48                   date: Date.parse('next thursday')
49                 });
50                 syncTo(data);
51                 render(data);
52                 $('#na-input').focus();
53           }
54         });
55   };
56   
57   $.getJSON('/', render);
58 });