topical media & game development

talk show tell print

professional-web-01-v3-controller.js / js



  
  function initController() {
      
      YAHOO.buzzWatch.controller = new BuzzWatchController(); 
      
  }
  
  function BuzzWatchController () {
  
    this.callback = {
        success: this.handleSuccess,
        failure: this.handleFailure,
        scope: this
    };
    this.saveCallback = {
        success: this.handleSaveSuccess,
        failure: this.handleFailure,
        scope: this
    };
   this.xsltCallback = {
        success: this.handleXSLTSuccess,
        failure: this.handleFailure,
        scope: this
    };
    this.callbackList = {
        success: this.handleListSuccess,
        failure: this.handleListFailure,
        scope: this
    };
  
    YAHOO.util.Event.addListener(
      "menubar.file.save", 
      "click", this.save, 
      this, 
      true);
    YAHOO.util.Event.addListener(
      "menubar.file.new", 
      "click", 
      this._new, 
      this, 
      true);
    //this.loadList();
    this.initMenuGo();
    this.setTimeout(30);
    if (YAHOO.buzzWatch.config.isEmpty())
      YAHOO.buzzWatch.menuGo.cfg.getProperty("submenu").show();
    YAHOO.util.Connect.asyncRequest('GET', "format.xsl", this.xsltCallback);
  }
  
  BuzzWatchController.prototype.loadList = function() {
    YAHOO.util.Connect.asyncRequest('GET', <watch.php>, this.callbackList);
  }
  
  BuzzWatchController.prototype.getUrlForSymbol = function(symbol) {
    return location.pathname + "?name="+symbol;
  }
  
  BuzzWatchController.prototype.load = function(symbol) {
  
    if (symbol != undefined)
      this.symbol = symbol;
    if (this.symbol != undefined) {
      YAHOO.util.Connect.asyncRequest('GET', "watch.php?name="+this.symbol, this.callback);
    }
  }
  
  BuzzWatchController.prototype.loadURL = function(e, o, url) {
    //YAHOO.buzzWatch.menuGo.cfg.getProperty("submenu").hide();
    //YAHOO.buzzWatch.controller.load(symbol);
    location = url;
  }
  
  BuzzWatchController.prototype.handleXSLTSuccess = function(o) {
    if(o.responseText !== undefined){
      this.xsltDom = o.responseXML;
      //alert ("XSLT received " + this.xsltDom.documentElement.nodeName);
    }
  }
  
  BuzzWatchController.prototype.getXSLTProcessor = function() {
    if (this.freeXSLTprocessors == undefined || this.freeXSLTprocessors.length == 0) {
      //alert("creating a new processor with "+this.xsltDom.documentElement.nodeName);
      var processor = new XSLTProcessor();
      processor.importStylesheet(this.xsltDom);
      return processor;
    }
    return this.freeXSLTprocessors.pop();
  }
  BuzzWatchController.prototype.returnXSLTProcessor = function(processor) {
    if (this.freeXSLTprocessors == undefined)
      this.freeXSLTprocessors = new Array();
    this.freeXSLTprocessors.push(processor);
  }
    
  
  BuzzWatchController.prototype.handleSuccess = function(o) {
    if(o.responseText !== undefined){
      var xotree = new XML.ObjTree();
      var tree = xotree.parseDOM( o.responseXML.documentElement );
      YAHOO.buzzWatch.config.set(tree.watch.symbol, tree.watch.tag);
      refreshPanels();
      YAHOO.buzzWatch.editInPlace.set('textTitle', tree.watch.title);
      YAHOO.buzzWatch.editInPlace.set('textDescription', tree.watch.description);
      
    }
  }
  
  BuzzWatchController.prototype.initMenuGo = function() {
    // I have no idea why this is necessary :-(
    var menu = YAHOO.buzzWatch.menuGo.cfg.getProperty("submenu");
    var items = menu.getItemGroups();
    var newItems = new Array();
    while (menu.getItemGroups().length > 0) {
      var menuItem = menu.removeItem(0);
      var li = menuItem.element;
      var a = li.getElementsByTagName("a")[0];
      menuItem.destroy();
      var href = a.getAttribute('href');
      var link = this.getUrlForSymbol(href.substring(href.indexOf('=')+1));
      menuItem = new YAHOO.widget.MenuItem(
        a.firstChild.nodeValue, 
        { url: link }
      );
      newItems.push(menuItem);
       menuItem.clickEvent.subscribe(
        this.loadURL, 
        link, 
        false
      );
     }
    for (var i=0; i< newItems.length; i++) {
      menu.addItem(newItems[i]);    
    }
    menu.render();
  
  }
  
  BuzzWatchController.prototype.handleListSuccess = function(o) {
    if(o.responseText !== undefined){
      this.setTimeout(getMaxAge(o));
      var xotree = new XML.ObjTree();
      xotree.force_array = ["watch"];
      var tree = xotree.parseDOM( o.responseXML.documentElement );
      var menu = YAHOO.buzzWatch.menuGo.cfg.getProperty("submenu");
      while (menu.getItemGroups().length > 0) {
        var menuItem = menu.removeItem(0);
        menuItem.destroy();
      }
      var watches = tree.watches.watch;
      for (var i=0; i< watches.length; i++) {
        var watch = watches[i];
        var menuItem = new YAHOO.widget.MenuItem(
          watch.title + ' (' + watch.symbol+')', 
          {url: this.getUrlForSymbol(watch.symbol)}
        );
        menu.addItem(menuItem);
        menuItem.clickEvent.subscribe(
          this.loadURL, 
          this.getUrlForSymbol(watch.symbol), 
          false
        );
      }
      menu.render();
    }
  }
  BuzzWatchController.prototype.handleSaveSuccess = function(o) {
    alert ("saved");
  }
  BuzzWatchController.prototype.handleFailure = function(o) {
      alert("failure");
  }
  BuzzWatchController.prototype.handleListFailure = function(o) {
      this.setTimeout(30);
      alert("failure");
  }
  BuzzWatchController.prototype.clearTimeout = function() {
    if (this.timeout!=undefined) {
      clearTimeout(this.timer);
      this.timeout = undefined;
    }
    //alert ("clearing timeout controller");
  }
  BuzzWatchController.prototype.setTimeout = function(duration) {
    this.clearTimeout;
    duration = Number(duration);
    if (duration)
      this.period = duration;
    if (this.period == undefined)
      this.period = 60;
    this.timeout = setTimeout('YAHOO.buzzWatch.controller.loadList()' , this.period * 1000);
    //alert ("seting timeout controller to "+ this.period);
  }
  BuzzWatchController.prototype.save = function() {
    var xotree = new XML.ObjTree();
    var tree = {
      watch: {
        symbol: YAHOO.buzzWatch.config.symbol,
        tag: YAHOO.buzzWatch.config.tag,
        title: YAHOO.buzzWatch.editInPlace.get('textTitle'),
        description: YAHOO.buzzWatch.editInPlace.get('textDescription')
      }
    };
    alert("save: " + xotree.writeXML(tree));
    var o = YAHOO.util.Connect.getConnectionObject();
    o.conn.open("POST", <watch.php>, true);
    YAHOO.util.Connect.initHeader('Content-Type','application/xml');
    YAHOO.util.Connect.setHeader(o);
    YAHOO.util.Connect.handleReadyState(o, this.saveCallback);
    o.conn.send(xotree.writeXML(tree));
  }
  BuzzWatchController.prototype._new = function() {
    YAHOO.buzzWatch.editInPlace.set('textTitle', "[title]");
    YAHOO.buzzWatch.editInPlace.set('textDescription', "[description]");
    YAHOO.buzzWatch.config.set("", "");
    YAHOO.buzzWatch.config.displayConfig();
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.