home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / toolkit.jar / content / global / nsWidgetStateManager.js < prev    next >
Encoding:
JavaScript  |  2001-06-19  |  12.1 KB  |  358 lines

  1. /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  *   Ben Goodger <ben@netscape.com> (Original Author)
  20.  */
  21.  
  22. /** Presenting widgetStateManager the Third.
  23.  *   a production of ye olde bard Ben Goodger and his Merry XUL Widget Crewe.
  24.  *  =>> MODIFICATIONS MUST BE REVIEWED BY ben@netscape.com!!! <<=
  25.  **/
  26.  
  27. var wsm;
  28.  
  29. function nsWidgetStateManager ( aFrameID )
  30.   {
  31.  
  32.     this.dataManager =
  33.       {
  34.         /** Persisted Data Hash Table
  35.          *  Page_ID -> Element_ID -> Property -> Value
  36.          **/
  37.         pageData: [],
  38.  
  39.         setPageData:
  40.           function ( aPageTag, aDataObject )
  41.             {
  42.               this.pageData[aPageTag] = aDataObject;
  43.             },
  44.  
  45.         getPageData:
  46.           function ( aPageTag )
  47.             {
  48.               if( !(aPageTag in this.pageData) )
  49.                 this.pageData[aPageTag] = [];
  50.               return this.pageData[aPageTag];
  51.             },
  52.  
  53.         setItemData:
  54.           function ( aPageTag, aItemID, aDataObject )
  55.             {
  56.               if( !(aPageTag in this.pageData) )
  57.                 this.pageData[aPageTag] = [];
  58.               this.pageData[aPageTag][aItemID] = aDataObject;
  59.             },
  60.  
  61.         getItemData:
  62.           function ( aPageTag, aItemID )
  63.             {
  64.               if( !(aItemID in this.pageData[aPageTag]) )
  65.                 this.pageData[aPageTag][aItemID] = [];
  66.               return this.pageData[aPageTag][aItemID];
  67.             }
  68.       }
  69.  
  70.     this.contentID    = aFrameID;
  71.  
  72.     wsm               = this;
  73.  
  74.     /** Element Handlers
  75.      *  Provides default get and set handler functions for supported
  76.      *  widgets. Clients can override or add new widgets.
  77.      **/
  78.     this.handlers     =
  79.       {
  80.         menulist:
  81.           {  get: wsm.get_Menulist,    set: wsm.set_Menulist      },
  82.         radiogroup:
  83.           {  get: wsm.get_Radiogroup,  set: wsm.set_Radiogroup    },
  84.         checkbox:
  85.           {  get: wsm.get_Checkbox,    set: wsm.set_Checkbox      },
  86.         textbox:
  87.           {  get: wsm.get_Textbox,     set: wsm.set_Textbox},
  88.         default_handler:
  89.           {  get: wsm.get_Default,     set: wsm.set_Default       }
  90.       }
  91.  
  92.     // extra attributes to scan and save.
  93.     this.attributes   = [];
  94.   }
  95.  
  96. nsWidgetStateManager.prototype =
  97.   {
  98.     get contentArea()
  99.       {
  100.         return window.frames[ this.contentID ];
  101.       },
  102.  
  103.     savePageData:
  104.       function ( aPageTag )
  105.         {
  106.           if (!(aPageTag in this.dataManager.pageData))
  107.             return;
  108.  
  109.           if( 'GetFields' in this.contentArea)
  110.             {
  111.               // save page data based on user supplied function in content area
  112.               var dataObject = this.contentArea.GetFields();
  113.               if (dataObject)
  114.                 this.dataManager.setPageData( aPageTag, dataObject );
  115.             }
  116.  
  117.             // Automatic element retrieval. This is done in two ways.
  118.             // 1) if an element id array is present in the document, this is
  119.             //    used to build a list of elements to persist. <-- performant
  120.             // 2) otherwise, all elements with 'wsm_persist' set to true
  121.             //    are persisted <-- non-performant.
  122.            var elements;
  123.            if( '_elementIDs' in this.contentArea )
  124.               {
  125.                 elements = [];
  126.                 for( var i = 0; i < this.contentArea._elementIDs.length; i++ )
  127.                   {
  128.                     var elt = this.contentArea.document.getElementById( this.contentArea._elementIDs[i] );
  129.                     if (elt) {
  130.                       elements[elements.length] = elt;
  131.                     } else {
  132.                       // see bug #40329. People forget this too often, and it breaks Prefs
  133.                       dump("*** FIX ME: '_elementIDs' in '" + this.contentArea.location.href.split('/').pop() +
  134.                            "' contains a reference to a non-existent element ID '" +
  135.                            this.contentArea._elementIDs[i] + "'.\n");
  136.                     }
  137.                   }
  138.               }
  139.             else
  140.               {
  141.                 elements = this.contentArea.document.getElementsByAttribute( "wsm_persist", "true" );
  142.               }
  143.             for( var ii = 0; ii < elements.length; ii++ )
  144.               {
  145.                 var elementID   = elements[ii].id;
  146.                 var elementType = elements[ii].localName;
  147.                 if (!(aPageTag in this.dataManager.pageData) )
  148.                     this.dataManager.pageData[aPageTag] = [];
  149.                 this.dataManager.pageData[aPageTag][elementID] = [];
  150.                 // persist element Type
  151.                 this.dataManager.pageData[aPageTag][elementID].localName = elementType;
  152.                 // persist attributes
  153.                 var get_Func = (elementType in this.handlers) ?
  154.                                 this.handlers[elementType].get :
  155.                                 this.handlers.default_handler.get;
  156.                 this.dataManager.setItemData( aPageTag, elementID, get_Func( elementID ) );
  157.               }
  158.         },
  159.  
  160.     setPageData:
  161.       function ( aPageTag )
  162.         {
  163.           var pageData = this.dataManager.getPageData( aPageTag );
  164.           if( 'SetFields' in this.contentArea )
  165.             {
  166.               this.contentArea.SetFields( pageData );
  167.               return;
  168.             }
  169.  
  170.           for( var elementID in pageData )
  171.             {
  172.               var element = this.contentArea.document.getElementById( elementID );
  173.               if( element )
  174.                 {
  175.                   var elementType = element.localName;
  176.                   var set_Func = (elementType in this.handlers) ?
  177.                                   this.handlers[elementType].set :
  178.                                   this.handlers.default_handler.set;
  179.                   set_Func( elementID, pageData[elementID] );
  180.                 }
  181.             }
  182.         },
  183.  
  184.  
  185.     /** Widget Get/Set Function Implementations
  186.      *  These can be overridden by the client.
  187.      **/
  188.     generic_Set:
  189.       function ( aElement, aDataObject )
  190.         {
  191.           if( aElement )
  192.             {
  193.               for( var property in aDataObject )
  194.                 {
  195.                   aElement.setAttribute( property, aDataObject[property] );
  196.                 }
  197.               if ( !aElement.getAttribute("disabled","true") )
  198.                 aElement.removeAttribute("disabled");
  199.             }
  200.         },
  201.  
  202.     generic_Get:
  203.       function ( aElement )
  204.         {
  205.           if( aElement )
  206.             {
  207.               var dataObject = [];
  208.               var wsmAttributes = aElement.getAttribute( "wsm_attributes" );
  209.               var attributes = wsm.attributes;              // make a copy
  210.               if( wsmAttributes != "" )
  211.                 {
  212.                   attributes.push( wsmAttributes.split(" ") );  // modify the copy
  213.                 }
  214.               for( var i = 0; i < attributes.length; i++ )
  215.                 {
  216.                   dataObject[attributes[i]] = aElement.getAttribute( attributes[i] );
  217.                 }
  218.               return dataObject;
  219.             }
  220.             return null;
  221.         },
  222.  
  223.     // <menulist>
  224.     set_Menulist:
  225.       function ( aElementID, aDataObject )
  226.         {
  227.           var element = wsm.contentArea.document.getElementById( aElementID );
  228.           // set all generic properties
  229.           wsm.generic_Set( element, aDataObject );
  230.           // set menulist specific properties
  231.           if( 'value' in aDataObject )
  232.             {
  233.               try {
  234.                 element.selectedItem = element.getElementsByAttribute( "value", aDataObject.value )[0];
  235.               }
  236.               catch (ex) {
  237.                 dump(aElementID +", ex: " + ex + "\n");
  238.               }
  239.             }
  240.         },
  241.  
  242.     get_Menulist:
  243.       function ( aElementID )
  244.         {
  245.           var element     = wsm.contentArea.document.getElementById( aElementID );
  246.           // retrieve all generic attributes
  247.           var dataObject  = wsm.generic_Get( element );
  248.           // retrieve all menulist specific attributes
  249.           if( dataObject )
  250.             {
  251.               dataObject.value = element.getAttribute( "value" );
  252.               return dataObject;
  253.             }
  254.           return null;
  255.         },
  256.  
  257.     // <radiogroup>
  258.     set_Radiogroup:
  259.       function ( aElementID, aDataObject )
  260.         {
  261.  
  262.           var element = wsm.contentArea.document.getElementById( aElementID );
  263.           wsm.generic_Set( element, aDataObject );
  264.           if( 'value' in aDataObject )
  265.             {
  266.               element.selectedItem = element.getElementsByAttribute( "value", aDataObject.value )[0];
  267.             }
  268.           if( 'disabled' in aDataObject )
  269.             {
  270.               element.disabled = aDataObject.disabled;
  271.             }
  272.         },
  273.  
  274.     get_Radiogroup:
  275.       function ( aElementID )
  276.         {
  277.           var element = wsm.contentArea.document.getElementById( aElementID );
  278.           var dataObject = wsm.generic_Get( element );
  279.           if( dataObject )
  280.             {
  281.               dataObject.value = element.getAttribute( "value" );
  282.               return dataObject;
  283.             }
  284.           return null;
  285.         },
  286.  
  287.     // <textbox>
  288.     set_Textbox:
  289.       function ( aElementID, aDataObject )
  290.         {
  291.           var element = wsm.contentArea.document.getElementById( aElementID );
  292.           wsm.generic_Set( element, aDataObject );
  293.         },
  294.  
  295.     get_Textbox:
  296.       function ( aElementID )
  297.         {
  298.           var element = wsm.contentArea.document.getElementById( aElementID );
  299.           var dataObject = wsm.generic_Get( element );
  300.           if( dataObject )
  301.             {
  302.               dataObject.value = wsm.contentArea.document.getElementById( aElementID ).value;
  303.               return dataObject;
  304.             }
  305.           return null;
  306.         },
  307.  
  308.     // <checkbox>
  309.     set_Checkbox:
  310.       function ( aElementID, aDataObject )
  311.         {
  312.           var element = wsm.contentArea.document.getElementById( aElementID );
  313.           // Set generic properites. 
  314.           wsm.generic_Set( element, aDataObject );
  315.           // Handle reversed boolean values.
  316.           if ( "checked" in aDataObject && element.getAttribute( "reversed" ) == "true" )
  317.             element.checked = !aDataObject.checked; 
  318.         },
  319.  
  320.     get_Checkbox:
  321.       function ( aElementID )
  322.         {
  323.           var element = wsm.contentArea.document.getElementById( aElementID );
  324.           var dataObject = wsm.generic_Get( element );
  325.           if( dataObject )
  326.             {
  327.               var checked = wsm.contentArea.document.getElementById( aElementID ).checked;
  328.               dataObject.checked = element.getAttribute("reversed") == "true" ? !checked : checked;
  329.               return dataObject;
  330.             }
  331.           return null;
  332.         },
  333.  
  334.     // <default>
  335.     set_Default:
  336.       function ( aElementID, aDataObject )
  337.         {
  338.           var element = wsm.contentArea.document.getElementById( aElementID );
  339.           wsm.generic_Set( element, aDataObject );
  340.         },
  341.  
  342.     get_Default:
  343.       function ( aElementID )
  344.         {
  345.           var element = wsm.contentArea.document.getElementById( aElementID );
  346.           var dataObject = wsm.generic_Get( element );
  347.           return dataObject ? dataObject : null;
  348.         }
  349.   }
  350.  
  351.  
  352. // M:tHoF Greatest Hits Section (Append one line per edit):
  353. // it will be dark soon 
  354. // MANOS MADE ME PERMANENT! 
  355. // there is no way out of here
  356. // [The Master] not dead as you know it. He is with us always.
  357.  
  358.