Source Code

for file /controls/ViewSelector.js

// ViewSelector.js
// Javascript Behaviour for the View Selector Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// -----
// 21.08.2005 created by Matthias Hertel.
// 21.05.2006 using visual effects - buttons.
// 16.09.2006 context on event-methods is now set to the bound object.
// 21.10.2006 persisting properties removed. use the the PropPersist AJAX Control instead.
// 18.11.2006 correct reload when using the browser back.
// 21.12.2007 fixing Firefox
// 18.12.2007 Simplifications and documentation.
// 01.10.2010 IE9 compatibility avoiding browser detection.

jcl.ViewSelectorBehaviour = {

  // Properties
  eventname: "view", /// <summary>The local or complete event name that is used for publishing OpenAjax events.</summary>
  currentView: "",
  views: "",

  init: function () {
    var n, objs;

    this.eventname = jcl.BuildFullEventname(this);
    OpenAjax.hub.subscribe(this.eventname, this._handleEvent, this);

    if (this.attributes != null) { // IE9 compatible
      objs = this.getElementsByTagName("A");
      for (n = 0; n < objs.length; n++)
        if (objs[n].attributes["view"] != null)
          objs[n].view = objs[n].attributes["view"].value;

      objs = this.getElementsByTagName("BUTTON");
      for (n = 0; n < objs.length; n++)
        if (objs[n].attributes["view"] != null)
          objs[n].view = objs[n].attributes["view"].value;
    } // if        
  }, // init


  afterinit: function () {
    // publish an initial event if none was already publish.
    if (this.currentView == "") {
      OpenAjax.hub.publish(this.eventname, this.views.split(';')[0].split(':')[0]);
    } // if
  }, // afterinit


  // Events
  onclick: function (evt) {
    evt = evt || window.event;

    var src = evt.target || evt.srcElement;
    if ((src.tagName == "A") && (src.href.indexOf('#') > 0)) {
      this.select(src.href.split('#')[1]);

      evt.cancelBubble = true;
      evt.returnValue = false;

    } else if ((src.tagName == "BUTTON") && (src.getAttribute != null) && (src.getAttribute("view") != null)) { // IE9 compatible
      this.select(src.getAttribute("view"));
      evt.cancelBubble = true;
      evt.returnValue = false;

    } else if ((src.tagName == "BUTTON") && (src.view != null)) {
      this.select(src.view);
      evt.cancelBubble = true;
      evt.returnValue = false;

    } // if
  }, // onclick


  // Methods
  select: function (viewName) {
    OpenAjax.hub.publish(this.eventname, viewName);
  }, // select


  _handleEvent: function (prop, value) {
    var objs, n;
    if (value != this.currentView) {
      this.currentView = value;
      objs = this.getElementsByTagName("BUTTON");
      for (n = 0; n < objs.length; n++)
        objs[n].className = ((objs[n].view == value) ? "selected" : "");

    } // if
  } // _handleEvent

}; // jcl.ViewSelectorBehaviour



This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.