Resources/Web/js/ohm_web.js
author moel.mich
Sun, 27 May 2012 20:15:32 +0000
changeset 348 d8fa1e55acfa
permissions -rw-r--r--
Added the remote web enhancement developed by Prince Samuel.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7 	Copyright (C) 2012 Prince Samuel <prince.samuel@gmail.com>
     8 
     9 */
    10 
    11 ko.bindingHandlers.treeTable = {
    12   update: function(element, valueAccessor, allBindingsAccessor) {
    13     var dependency = ko.utils.unwrapObservable(valueAccessor()),
    14     options = ko.toJS(allBindingsAccessor().treeOptions || {});
    15 
    16     setTimeout(function() { $(element).treeTable(options); }, 0);
    17   } 
    18 };
    19 
    20 var node = function(config, parent) {
    21   this.parent = parent;
    22   var _this = this;
    23 
    24   var mappingOptions = {
    25     Children : {
    26       create: function(args) {
    27         return new node(args.data, _this);
    28       }
    29       ,
    30       key: function(data) {
    31         return ko.utils.unwrapObservable(data.id);
    32       }
    33     }
    34   };
    35 
    36   ko.mapping.fromJS(config, mappingOptions, this);
    37 }
    38 
    39 $(function(){
    40   $.getJSON('data.json', function(data) {
    41     viewModel = new node(data, undefined);
    42 
    43     (function() {
    44       function flattenChildren(children, result) {
    45         ko.utils.arrayForEach(children(), function(child) {
    46           result.push(child);
    47           if (child.Children) {
    48             flattenChildren(child.Children, result);
    49           }
    50         });
    51       }
    52 
    53       viewModel.flattened = ko.dependentObservable(function() {
    54         var result = []; //root node
    55 
    56         if (viewModel.Children) {
    57           flattenChildren(viewModel.Children, result);   
    58         }
    59 
    60         return result;
    61       });
    62 
    63       viewModel.update = function() {
    64         $.getJSON('data.json', function(data) {
    65           ko.mapping.fromJS(data, {}, viewModel);
    66         });
    67       }
    68 
    69       viewModel.rate = 3000; //milliseconds
    70       viewModel.timer = {};
    71 
    72       viewModel.startAuto = function (){
    73         viewModel.timer = setInterval(viewModel.update, viewModel.rate);
    74       }
    75 
    76       viewModel.stopAuto = function (){
    77         clearInterval(viewModel.timer);
    78       }
    79 
    80       viewModel.auto_refresh = ko.observable(false);
    81       viewModel.toggleAuto = ko.dependentObservable(function() {
    82         if (viewModel.auto_refresh())
    83           viewModel.startAuto();
    84         else
    85           viewModel.stopAuto();
    86       }, viewModel);
    87 
    88     })();
    89 
    90     ko.applyBindings(viewModel);
    91     $("#tree").treeTable({
    92       initialState: "expanded",
    93       clickableNodeNames: true
    94     });
    95   });
    96   $( "#refresh" ).button();
    97   $( "#auto_refresh" ).button();
    98   $( "#slider" ).slider({
    99     value:3,
   100     min: 1,
   101     max: 10,
   102     slide: function( event, ui ) {
   103       viewModel.rate = ui.value * 1000;
   104       if (viewModel.auto_refresh()) {
   105         //reset the timer
   106         viewModel.stopAuto();
   107         viewModel.startAuto();
   108       }
   109       $( "#lbl" ).text( ui.value + "s");
   110     }
   111   });
   112   $( "#lbl" ).text( $( "#slider" ).slider( "value" ) + "s");
   113 
   114 });
   115 
   116