~linuxjedi/drizzle.org/bug-737782

« back to all changes in this revision

Viewing changes to Scripts/Widgets/Navbar/navbar.js

  • Committer: kalebral at gmail
  • Date: 2011-02-13 17:21:36 UTC
  • mfrom: (13.1.2 trunk-seo-fixes)
  • Revision ID: kalebral@gmail.com-20110213172136-kv0coqo74cu7m2ps
Merge Andrew - lots of clean up

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//  iWeb - navbar.js
3
 
//  Copyright (c) 2007-2008 Apple Inc. All rights reserved.
4
 
//
5
 
 
6
 
var NavBar=Class.create(Widget,{widgetIdentifier:"com-apple-iweb-widget-NavBar",initialize:function($super,instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp)
7
 
{if(instanceID)
8
 
{$super(instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp);if(!this.preferenceForKey("useStaticFeed")&&this.preferenceForKey("dotMacAccount"))
9
 
{var depthPrefix=this.preferenceForKey("path-to-root");if(!depthPrefix||depthPrefix=="")
10
 
depthPrefix="./";this.xml_feed=depthPrefix+"?webdav-method=truthget&depth=infinity&ns=iweb&filterby=in-navbar";}
11
 
else
12
 
{this.xml_feed="feed.xml";if(this.sitePath)
13
 
{this.xml_feed=this.sitePath+"/"+this.xml_feed;}}
14
 
this.changedPreferenceForKey("navbar-css");this.regenerate();}},regenerate:function()
15
 
{new Ajax.Request(this.xml_feed,{method:'get',onSuccess:this.populateNavItems.bind(this)});return true;},getStyleElement:function(key)
16
 
{if(!this.styleElement)
17
 
{var head=document.getElementsByTagName("head")[0];if(head)
18
 
{var newElement=document.createElement("style");newElement.type="text/css";head.appendChild(newElement);this.styleElement=newElement;}}
19
 
return this.styleElement;},substWidgetPath:function(text)
20
 
{var result=text.replace(/\$WIDGET_PATH/gm,this.widgetPath);return result;},addCSSSelectorPrefix:function(text)
21
 
{var prefix="div#"+this.instanceID+" ";text=text.replace(/\/\*[^*]*\*+([^/][^*]*\*+)*\//gm,"");text=text.replace(/(^\s*|\}\s*)([^{]+)({[^}]*})/gm,function(match,beforeSelectorList,selectorList,propertyList){var result=beforeSelectorList;var selectors=selectorList.split(",");for(var i=0;i<selectors.length;i++){result+=prefix+selectors[i];if(i+1<selectors.length)result+=",";}
22
 
result+=propertyList;return result;});return text;},changedPreferenceForKey:function(key)
23
 
{if(key=="navbar-css")
24
 
{var text=this.preferenceForKey(key);if(!text)
25
 
{text="";}
26
 
text=this.substWidgetPath(text);text=this.addCSSSelectorPrefix(text);var styleElement=this.getStyleElement();if(styleElement)
27
 
{if(!windowsInternetExplorer)
28
 
{var node=document.createTextNode(text);if(node)
29
 
{while(styleElement.hasChildNodes())
30
 
{styleElement.removeChild(styleElement.firstChild);}
31
 
styleElement.appendChild(node);}}
32
 
else
33
 
{styleElement.styleSheet.cssText=text;}}}},populateNavItems:function(req)
34
 
{var items;var feedRoot=ajaxGetDocumentElement(req);if(feedRoot){var parsedFeed=this.getAtomFeedItems(feedRoot);var items=parsedFeed.resultArray;var currentPageGUID=null;var isCollectionPage="NO";var curPagePat=null;if(this.runningInApp)
35
 
curPagePat=/\.#current#.$/;else
36
 
{currentPageGUID=this.preferenceForKey("current-page-GUID");isCollectionPage=this.preferenceForKey("isCollectionPage");}
37
 
var navDiv=this.div("navbar-list");var navBgDiv=navDiv.parentNode;$(navBgDiv).ensureHasLayoutForIE();while(navDiv.firstChild){navDiv.removeChild(navDiv.firstChild);}
38
 
var depthPrefix=this.preferenceForKey("path-to-root");if(!depthPrefix||depthPrefix=="")
39
 
depthPrefix="./";for(var x=0;x<items.length;x++){var navItem=document.createElement("li");var anchor=document.createElement("a");var title=items[x].title;var pageGUID=items[x].GUID;title=title.replace(/ /g,"\u00a0")+" ";var url=items[x].url;if(!this.runningInApp&&!url.match(/^http:/i))
40
 
url=depthPrefix+url;var inAppCurPage=this.runningInApp&&curPagePat.exec(unescape(new String(url)));if(inAppCurPage)
41
 
{url=url.replace(curPagePat,"");}
42
 
if(pageGUID==currentPageGUID||inAppCurPage){navItem.className='current-page';if(!this.runningInApp&&isCollectionPage!="YES"){url="";}}
43
 
else
44
 
navItem.className='noncurrent-page';anchor.setAttribute("href",url);anchor.appendChild(document.createTextNode(title));navItem.appendChild(anchor);navDiv.appendChild(navItem);}
45
 
if(this.preferences&&this.preferences.postNotification){this.preferences.postNotification("BLWidgetIsSafeToDrawNotification",1);}}},getAtomFeedItems:function(feedNode)
46
 
{var results=new Array;var pageOrder=new Array;if(feedNode)
47
 
{var generator="";var generatorElt=getFirstElementByTagName(feedNode,"generator");if(generatorElt&&generatorElt.firstChild){generator=allData(generatorElt);}
48
 
var pageGUIDs,pageGUIDsElt;for(var entryElt=feedNode.firstChild;entryElt;entryElt=entryElt.nextSibling){var isInNavbarElt=null;if(!pageGUIDs&&(pageGUIDsElt=findChild(entryElt,"site-navbar","urn:iweb:"))){pageGUIDs=allData(pageGUIDsElt).split(",");for(var x=0;x<pageGUIDs.length;x++){var pageGUID=pageGUIDs[x];pageOrder[""+pageGUID]=x;}}
49
 
if(entryElt.nodeName=="entry"&&(isInNavbarElt=findChild(entryElt,"in-navbar","urn:iweb:"))){if(!isInNavbarElt)
50
 
continue;var pageGUID="";if(isInNavbarElt.firstChild){pageGUID=""+allData(isInNavbarElt);}else{iWLog("no navBarElt child");}
51
 
if(pageGUID=="navbar-sort")
52
 
continue;var title="";var titleElt=findChild(entryElt,"title","urn:iweb:");if(!titleElt){iWLog("No iWeb title");titleElt=findChild(entryElt,"title");}
53
 
if(titleElt&&titleElt.firstChild){title=allData(titleElt);}
54
 
var linkElt=getFirstElementByTagName(entryElt,'link');url=linkElt.getAttribute("href");if(!url&&linkElement.firstChild){url=allData(linkElement);}
55
 
results[results.length]={title:title,url:url,GUID:pageGUID};}}}
56
 
if(pageGUIDs){results=$(results).reject(function(result){return(pageOrder[result.GUID]===undefined);});results.sort(function(lhs,rhs){return pageOrder[lhs.GUID]-pageOrder[rhs.GUID];});}
57
 
return{resultArray:results};},onload:function()
58
 
{},onunload:function()
59
 
{}});function findChild(element,nodeName,namespace)
60
 
{var child;for(child=element.firstChild;child;child=child.nextSibling){if(child.localName==nodeName||child.baseName==nodeName){if(!namespace){return child;}
61
 
var childNameSpace=child.namespaceURI;if(childNameSpace==namespace){return child;}}}
62
 
return null;}
63
 
function getFirstElementByTagName(node,tag_name){var elements=node.getElementsByTagName(tag_name);if(elements.length){return elements[0];}
64
 
else{return findChild(node,tag_name);}}
65
 
function allData(node)
66
 
{node=node.firstChild;var data=node.data;while((node=node.nextSibling)){data+=node.data;}
67
 
return data;}