home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / Class / TabControlClass.js < prev   
Encoding:
Text File  |  1999-12-01  |  11.2 KB  |  329 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //TabControl Class
  4.  
  5. //EVENT MODEL
  6. //
  7. //The TabControl class distributes events to the page classes by calling
  8. //methods of the page class if they exist.
  9. //The supported events are:
  10. //
  11. //  page.getTabLabel()  returns the tab label text for this page
  12. //  page.canLoad()      returns true if this page can be loaded
  13. //  page.load()         called when page visited
  14. //  page.loaded         true if 1st call for wizard session, else false
  15. //  page.update()       called when any UI element is changed
  16. //  page.unload()       return true if this page can be unloaded
  17. //  page.lastUnload()   called when finish() is called
  18.  
  19.  
  20. //INTERFACE
  21. //
  22. //TabControl methods:
  23. //
  24. //  getPageNum(thePageName) returns integer
  25. //    - returns the index in pageList of the given page (-1 if not found)
  26. //
  27. //  start(startPage)
  28. //    - initializes the tabs (startPage is optional page name to start on)
  29. //
  30. //  finish() returns string
  31. //    - unloads the current page and returns its name
  32. //    - sends the lastUnload event to each of the pages in the current group
  33. //      
  34. //  addGroup(theGroupName, theGroupArray) returns boolean
  35. //    - add a group of pages to the list of possible groups
  36. //    - (theGroupArray is an array of page names)
  37. //
  38. //  showGroup(theGroupName) returns boolean
  39. //    - select a group to display (call refresh or showPage to view the new group)
  40. //
  41. //  addPage(thePageName, thePageObject) returns boolean
  42. //    - add a page and its corresponding object to the list of possible pages
  43. //    - (thePageName is the div id of the page)
  44. //
  45. //  showPage(thePageName) returns boolean
  46. //    - show the page with the given name in the current group
  47. //    - (calls the unload() method of the current page,
  48. //    -  calls the canLoad() and load() methods of the new page)
  49. //
  50. //  showPageNum(thePageNum) return boolean
  51. //    - show the page with the given index in the current group
  52. //
  53. //  nextPage() returns boolean
  54. //    - show the next page in the current group
  55. //
  56. //  previousPage() returns boolean
  57. //    - show the previous page in the current group
  58. //
  59. //  refresh() returns boolean
  60. //    - refresh the tab display
  61. //
  62. //  insertPage(thePageName, insertBeforeName, allowDuplicates) returns boolean
  63. //    - dynamically insert a page in the current group
  64. //    - insertBeforeName specifies the page to insert after, null = insert at end
  65. //    - (call refresh or showPage to view the new page)
  66. //
  67. //  removePage(thePageName) returns boolean
  68. //    - dynamically remove a page from the current group
  69. //    - (call refresh or showPage to remove the page from the display)
  70. //
  71. //  update(theItemName)       
  72. //    - Calls the update() method in the page object for the current page
  73.  
  74.  
  75. function TabControl(thePrefix) {
  76.   // properties
  77.   this.prefix = (thePrefix != null) ? thePrefix : 'Tab';  //tab layers prefix
  78.   this.tabs = '';              //the array of tab objects
  79.   this.p = new Array();        //page objects array  
  80.   this.groups = new Array();   //page collection array
  81.   
  82.   this.group = '';             //the current group
  83.   this.pageList = '';          //the current page list
  84.   this.page = 0;               //the current page number
  85.   this.pageName = '';          //the current page name
  86.   this.oldPage = '';           //the previously displayed page
  87.   this.obj = '';               //the current layer object
  88.   this.pageListChanged = true; //the page list state
  89.   
  90.   this.groups['default'] = new Array();  // create the default group
  91. }
  92.  
  93. // methods
  94. TabControl.prototype.getPageNum = TabControl_getPageNum;
  95. TabControl.prototype.start = TabControl_start;
  96. TabControl.prototype.finish = TabControl_finish;
  97. TabControl.prototype.addPage = TabControl_addPage;
  98. TabControl.prototype.addGroup = TabControl_addGroup;
  99. TabControl.prototype.showGroup = TabControl_showGroup;
  100. TabControl.prototype.showPage = TabControl_showPage;
  101. TabControl.prototype.showPageNum = TabControl_showPageNum;
  102. TabControl.prototype.nextPage = TabControl_nextPage;
  103. TabControl.prototype.previousPage = TabControl_previousPage;
  104. TabControl.prototype.refresh = TabControl_refresh;
  105. TabControl.prototype.insertPage = TabControl_insertPage;
  106. TabControl.prototype.removePage = TabControl_removePage;
  107. TabControl.prototype.update = TabControl_update;
  108. TabControl.prototype.newTabs = TabControl_newTabs;
  109.   
  110. //****************************************************************************************
  111.  
  112. function TabControl_getPageNum(thePageName) {
  113.   var i, retVal = -1;
  114.   for (i=0; i < this.pageList.length; i++) {
  115.     if (this.pageList[i] == thePageName) {
  116.       retVal = i;
  117.       break;
  118.   } }
  119.   return retVal;
  120. }
  121.  
  122. function TabControl_start(startPage) {
  123.   var i;
  124.  
  125.   with (this) {
  126.     if (!group) showGroup('default');
  127.     
  128.     for (i in p) p[i].loaded = false; //mark pages as not loaded
  129.     
  130.     pageListChanged = true;
  131.     
  132.     // show the last page, if it exists
  133.     page = 0;
  134.     pageName = '';
  135.     if (startPage) {
  136.       page = getPageNum(startPage);
  137.       if (page == -1) page = 0;  // show first page on error
  138.     }
  139.     showPage(pageList[page],true); //show prior page or first page
  140.   }
  141. }
  142.  
  143. function TabControl_finish() {
  144.   var i;
  145.   with (this) {
  146.     if (p[pageList[page]].unload != null) p[pageList[page]].unload(); //unload current page
  147.     for (i=0; i < pageList.length; i++) { //give each class a last chance
  148.       if (pageList[i] && (p[pageList[i]].lastUnload != null)) {
  149.         p[pageList[i]].lastUnload();
  150.   } } }
  151.   return this.pageList[this.page];
  152. }
  153.  
  154. function TabControl_addGroup(theGroupName, theGroupArray) {
  155.   var retVal = false;
  156.   if (this.groups[theGroupName] == null) {
  157.     this.groups[theGroupName] = theGroupArray;
  158.     retVal = true;
  159.   }
  160.   return retVal;
  161. }
  162.  
  163. function TabControl_showGroup(theGroupName) {
  164.   var i, retVal = false;
  165.   if (this.groups[theGroupName] != null) with (this) { //if group is known
  166.     group = theGroupName;
  167.     pageList = new Array();
  168.     for (i=0; i < groups[group].length; i++) //copy the group array to pageList
  169.       pageList[i] = groups[group][i];        // so dynamic adds won't change the group
  170.     pageListChanged = true;
  171.     retVal = true;
  172.   }
  173.   return retVal;
  174. }
  175.  
  176. function TabControl_addPage(thePageName, thePageObject) {
  177.   var retVal = false;
  178.   if (this.p[thePageName] == null) with (this) {
  179.     p[thePageName] = thePageObject;
  180.     groups['default'].push(thePageName);
  181.     retVal = true;
  182.   }
  183.   return retVal;
  184. }
  185.  
  186. function TabControl_showPage(thePageName, dontUnloadCurPg) {
  187.   var i, loadOk=true, unloadOk=true, retVal=false;
  188.   if (thePageName && this.p[thePageName] != null && 
  189.       this.getPageNum(thePageName) != -1) with (this) {
  190.     if (thePageName == pageName) {  // if same page
  191.       oldPage = page;  //remember previous for faster tabbing
  192.       page = getPageNum(thePageName);  //get number in case page was inserted
  193.       if (oldPage == page) oldPage = null;
  194.       newTabs();  
  195.       retVal = true;      
  196.     } else {
  197.       if (p[thePageName].canLoad != null)  //call canLoad() method if it exists
  198.         loadOk = p[thePageName].canLoad();
  199.       if (loadOk) {
  200.         if (!dontUnloadCurPg) { //if not silently visiting
  201.           if (pageName && p[pageName].unload != null) //tell prior pg to unload
  202.             unloadOk = p[pageName].unload();
  203.         }
  204.         if (unloadOk) { //if prior page agreed to go down (or didn't answer), continue
  205.           oldPage = page;  //remember previous for faster tabbing
  206.           page = getPageNum(thePageName);
  207.           pageName = thePageName; //change pageName
  208.           if (obj) obj.visibility = "hidden"; //hide old pg
  209.           obj = findObject(pageName);
  210.           if (obj) obj.visibility = "visible"; //show new page
  211.           if (p[pageName].load != null) {
  212.             p[pageName].load(); //tell page to load itself
  213.             p[pageName].loaded = true; //mark page as having been loaded this wizard session
  214.           }
  215.           newTabs();  
  216.           retVal = true;
  217.   } } } }
  218.   return retVal;
  219. }
  220.  
  221. function TabControl_showPageNum(pageNumber) {
  222.   var retVal = false;
  223.   if (pageNumber >= 0 && pageNumber < this.pageList.length)
  224.     retVal = this.showPage(this.pageList[pageNumber]);
  225.   return retVal;
  226. }
  227.  
  228. function TabControl_nextPage() {
  229.   var newPage=this.page+1, retVal = false;;
  230.   if (newPage < this.pageList.length)
  231.     retVal = this.showPage(this.pageList[newPage]);
  232.   return retVal;
  233. }
  234.  
  235. function TabControl_previousPage() {
  236.   var newPage=this.page-1, retVal = false;
  237.   if (newPage >= 0)
  238.     retVal = this.showPage(this.pageList[newPage]);
  239.   return retVal;
  240. }
  241.  
  242. function TabControl_refresh() {
  243.   var curPage, retVal = false;
  244.   with (this) {
  245.     curPage = getPageNum(pageName);
  246.     if (curPage > -1) retVal = showPageNum(curPage);
  247.     else if (page < pageList.length) retVal = showPageNum(page);
  248.     else if (pageList.length > 0) retVal = showPageNum(pageList.length-1);
  249.   }
  250.   return retVal;
  251. }
  252.  
  253. function TabControl_insertPage(thePageName, insertBeforeName, allowDuplicate) {
  254.   var i, retVal = false, insertItem = this.pageList.length;
  255.   with (this) {
  256.     if (insertBeforeName)
  257.       insertItem = this.getPageNum(insertBeforeName);
  258.     if (insertItem != -1 && (allowDuplicate || this.getPageNum(thePageName) == -1)) {
  259.       pageList.splice(insertItem, 0, thePageName);
  260.       pageListChanged = true;
  261.       retVal = true;
  262.   } }
  263.   return retVal;
  264. }
  265.  
  266. function TabControl_removePage(thePageName) {
  267.   var i , retVal = false, removeItem = -1;
  268.   with (this) {
  269.     removeItem = getPageNum(thePageName);
  270.     if (removeItem != -1) {
  271.       pageList.splice(removeItem, 1);
  272.       pageListChanged = true;
  273.       retVal = true;
  274.   } }
  275.   return retVal;
  276. }
  277.  
  278. function TabControl_update(theItemName) {
  279.   if (this.p[this.pageName] != null && this.p[this.pageName].update != null)
  280.     this.p[this.pageName].update(theItemName);
  281. }
  282.  
  283. function TabControl_newTabs() {
  284.   var i, tabObj, tabLabel;
  285.   with (this) {
  286.     //Create tab data structure
  287.     if (!tabs) {
  288.       tabs = new Array();
  289.       for(i=0,tabObj=findObject(prefix+i); tabObj; i++,tabObj=findObject(prefix+i)) {
  290.         tabs[i] = new Array();
  291.         tabs[i].tab      = tabObj;
  292.         tabs[i].tabSel   = findObject(prefix+i+"Sel");    //find bg object
  293.         tabs[i].tabLabel = findObject(prefix+i+"Label"); //find Label object
  294.     } }
  295.  
  296.     if (!pageListChanged) { //if only switching tabs
  297.       if (oldPage != null) //hide old selection
  298.         tabs[oldPage].tabSel.visibility = "hidden";
  299.       tabs[page].tabSel.visibility = "inherit"; //show new selection
  300.  
  301.     } else for (i=0; i < tabs.length; i++) { //refresh pagelist and all tabs
  302.       if (i < pageList.length && pageList[i]) {
  303.  
  304.         //show the tab before setting the src: bug in DW2
  305.         if (tabs[i].tab.visibility == "hidden")  //show tab if hidden
  306.           tabs[i].tab.visibility = "visible";
  307.  
  308.         //set tab label
  309.         tabLabel = "";
  310.         if (p[pageList[i]] != null && p[pageList[i]].getTabLabel != null)
  311.           tabLabel = p[pageList[i]].getTabLabel();
  312.         if (tabs[i].tabLabel.innerHTML != tabLabel) //change label if diff
  313.           tabs[i].tabLabel.innerHTML = tabLabel;
  314.  
  315.         //set selected
  316.         if (page == i) { //show selected tab
  317.           if (tabs[i].tabSel.visibility == "hidden") //show selected tab
  318.             tabs[i].tabSel.visibility = "inherit";
  319.         } else if (tabs[i].tabSel.visibility != "hidden") {//hide unselected tab
  320.             tabs[i].tabSel.visibility = "hidden";
  321.         }
  322.  
  323.       } else if (tabs[i].tab.visibility != "hidden") //hide tab if not hidden
  324.         tabs[i].tab.visibility = "hidden";
  325.     }
  326.     pageListChanged = false;
  327.   }
  328. }
  329.