Re-uploading www directory: 06 Oct 2010 21:15 GTM+1
[dtn] / www / DTN_Technical_Details / Scripts / Widgets / HeaderControls / HeaderControls.js
1 //
2 //  iWeb - HeaderControls.js
3 //  Copyright (c) 2007-2008 Apple Inc. All rights reserved.
4 //
5
6 var HeaderControls=Class.create(Widget,{widgetIdentifier:"com-apple-iweb-widget-headercontrols",initialize:function($super,instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp)
7 {if(instanceID!=null)
8 {$super(instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp);NotificationCenter.addObserver(this,HeaderControls.prototype.p_prevPage,"PreviousPage",this.p_mediaGridID());NotificationCenter.addObserver(this,HeaderControls.prototype.p_nextPage,"NextPage",this.p_mediaGridID());NotificationCenter.addObserver(this,HeaderControls.prototype.p_setPage,"SetPage",this.p_mediaGridID());this.mRange=new IWPageRange(0,5);this.p_updateRange();}},onload:function()
9 {var defaults={showBackToIndex:true,showSubscribe:true,showSlideshow:true,mediaIndex:false,entriesPerPage:99,entryCount:0};this.initializeDefaultPreferences(defaults);this.setPage(0);this.updateFromPreferences();if(this.preferences&&this.preferences.postNotification)
10 {this.preferences.postNotification("BLWidgetIsSafeToDrawNotification",1);}},onunload:function()
11 {},startup:function()
12 {this.p_updateCanvasControls();this.p_updateBackToIndex();this.p_updatePaginationControls();this.p_updateSubscribe();this.p_updateSlideshow();if(this.p_mediaIndex())
13 {this.getElementById("media_index_only").show();}
14 else
15 {this.getElementById("album_only").show();}},changedPreferenceForKey:function(key)
16 {if(key=="entriesPerPage"||key=="entryCount"||key=="x-currentPage")
17 {this.p_updateRange();this.p_updatePaginationControls();}
18 else if(key=="showBackToIndex")
19 {this.p_updateBackToIndex();}
20 else if(key=="showSubscribe")
21 {this.p_updateSubscribe();}
22 else if(key=="showSlideshow")
23 {this.p_updateSlideshow();}
24 else if(key=="canvas controls")
25 {this.p_updateCanvasControls();}
26 else if(this.runningInApp)
27 {if(key=="x-nextPage")
28 {this.nextPage();}
29 else if(key=="x-previousPage")
30 {this.prevPage();}
31 else if(key=="x-setPage")
32 {this.setPage(this.p_setPagePreference());}}},updateFromPreferences:function()
33 {this.startup();},prevPage:function()
34 {NotificationCenter.postNotification(new IWNotification("PreviousPage",this.p_mediaGridID(),null));},nextPage:function()
35 {NotificationCenter.postNotification(new IWNotification("NextPage",this.p_mediaGridID(),null));},setPage:function(pageIndex)
36 {NotificationCenter.postNotification(new IWNotification("SetPage",this.p_mediaGridID(),{pageIndex:pageIndex}));},playSlideshow:function()
37 {if(this.mPlaySlideshowFunction)
38 {this.mPlaySlideshowFunction();}},setPlaySlideshowFunction:function(playSlideshow)
39 {this.mPlaySlideshowFunction=playSlideshow;},p_canNavigateToPrev:function()
40 {return(this.p_currentPage()>0);},p_prevPage:function(notification)
41 {if(this.p_canNavigateToPrev())
42 {this.setPage(this.p_currentPage()-1);}},p_canNavigateToNext:function()
43 {return(this.p_currentPage()<this.p_pageCount()-1);},p_nextPage:function(notification)
44 {if(this.p_canNavigateToNext())
45 {this.setPage(this.p_currentPage()+1);}},p_setPage:function(notification)
46 {var pageIndex=notification.userInfo().pageIndex;this.setPreferenceForKey(pageIndex,"x-currentPage");if(!this.runningInApp)
47 {var entriesPerPage=this.p_entriesPerPage();var location=pageIndex*entriesPerPage;var length=Math.min(this.p_entryCount()-location,entriesPerPage);var userInfo={"range":new IWRange(location,length)};NotificationCenter.postNotification(new IWNotification("RangeChanged",this.p_mediaGridID(),userInfo));}},p_showBackToIndex:function()
48 {var show=this.preferenceForKey("showBackToIndex");(function(){return show!==undefined}).assert();return show;},p_showSubscribe:function()
49 {var show=this.preferenceForKey("showSubscribe");(function(){return show!==undefined}).assert();return show;},p_showSlideshow:function()
50 {var show=this.preferenceForKey("showSlideshow");(function(){return show!==undefined}).assert();return show;},p_mediaGridID:function()
51 {var mediaGridID=null;if(this.preferences)
52 {mediaGridID=this.preferenceForKey("gridID");}
53 if(mediaGridID===undefined)
54 {mediaGridID=null;}
55 return mediaGridID;},p_setPagePreference:function()
56 {var setPagePreference=null;if(this.preferences)
57 {setPagePreference=this.preferenceForKey("x-setPage");}
58 if(setPagePreference===undefined)
59 {setPagePreference=null;}
60 return setPagePreference;},p_updatePaginationControls:function()
61 {var widgetDiv=this.div();var currentPage=this.p_currentPage();var controls="";if(this.p_isPaginated())
62 {var canvasControlURLs=this.preferenceForKey("canvas controls");if(this.p_canNavigateToPrev())
63 {var leftArrowSrc=canvasControlURLs['canvas_arrow-left'];controls+="<a href='javascript:#{WIDGET_ID}.prevPage()'>";controls+=imgMarkup(leftArrowSrc,'','','');controls+="</a> ";}
64 else
65 {var leftArrowSrc=canvasControlURLs['canvas_arrow-left-D'];controls+=imgMarkup(leftArrowSrc,'','','')+" ";}
66 for(var i=this.mRange.min();i<this.mRange.max();i++)
67 {if(i==currentPage)
68 {controls+="<span class='current_page'>"+(i+1)+"</span> ";}
69 else
70 {controls+="<a href='javascript:#{WIDGET_ID}.setPage("+i+")'>"+(i+1)+"</a> ";}}
71 if(this.p_canNavigateToNext())
72 {var rightArrowSrc=canvasControlURLs['canvas_arrow-right'];controls+="<a href='javascript:#{WIDGET_ID}.nextPage()'>";controls+=imgMarkup(rightArrowSrc,'','','');controls+="</a>";}
73 else
74 {var rightArrowSrc=canvasControlURLs['canvas_arrow-right-D'];controls+=imgMarkup(rightArrowSrc,'','','');}}
75 var template=new Template(controls);var myControls=template.evaluate({WIDGET_ID:this.instanceID});this.getElementById("pagination_controls").update(myControls);widgetDiv.select(".paginated_only").invoke(this.p_isPaginated()?'show':'hide');widgetDiv.select(".non_paginated_only").invoke(this.p_isPaginated()?'hide':'show');if(this.runningInApp)
76 {this.setPreferenceForKey(controls,"x-paginationSpanContents");}
77 else
78 {NotificationCenter.postNotification(new IWNotification("paginationSpanContents",this.p_mediaGridID(),{controls:controls}));}},p_setAnchorsUnderElementToHREF:function(element,href)
79 {$(element).select('a').map(function(anchor){anchor.href=href;});},p_updateCanvasControls:function()
80 {var canvasControlURLs=this.preferenceForKey("canvas controls");this.div().select('.canvas').each(function(img)
81 {var canvasControlName="canvas_"+img.classNames().toArray()[1];setImgSrc(img,canvasControlURLs[canvasControlName]);});},p_updateBackToIndex:function()
82 {var element=this.getElementById("back_to_index");this.p_showBackToIndex()?element.show():element.hide();if(!this.runningInApp)
83 {this.p_setAnchorsUnderElementToHREF(element,this.p_indexURL());}},p_updateSubscribe:function()
84 {this.div().select(".subscribe").invoke(this.p_showSubscribe()?'show':'hide');if(!this.runningInApp)
85 {var feedURL="javascript:"+this.instanceID+(this.p_mediaIndex()?".mediaIndexSubscribe()":".photocastSubscribe()")
86 var self=this;this.div().select(".subscribe").each(function(element)
87 {self.p_setAnchorsUnderElementToHREF(element,feedURL);});}},mediaIndexSubscribe:function()
88 {window.location=this.p_feedURL();},photocastSubscribe:function()
89 {photocastHelper(this.p_feedURL());},p_updateSlideshow:function()
90 {this.div().select(".play_slideshow").invoke(this.p_showSlideshow()?'show':'hide');},p_mediaIndex:function()
91 {var mediaIndex=null;if(this.preferences)
92 {mediaIndex=this.preferenceForKey("mediaIndex");}
93 if(mediaIndex===undefined)
94 {mediaIndex=false;}
95 return mediaIndex;},p_currentPage:function()
96 {var currentPage=0;if(this.preferences)
97 {currentPage=this.preferenceForKey("x-currentPage");}
98 if(!currentPage)
99 {currentPage=0;}
100 return currentPage;},p_entriesPerPage:function()
101 {var entriesPerPage=null;if(this.preferences)
102 {entriesPerPage=this.preferenceForKey("entriesPerPage");}
103 if(entriesPerPage==undefined)
104 {entriesPerPage=99;}
105 return entriesPerPage;},p_entryCount:function()
106 {var entryCount=null;if(this.preferences)
107 {entryCount=this.preferenceForKey("entryCount");}
108 if(entryCount==undefined)
109 {entryCount=0;}
110 return entryCount;},p_indexURL:function()
111 {return this.preferenceForKey("indexURL");},p_feedURL:function()
112 {return this.preferenceForKey("feedURL");},p_isPaginated:function()
113 {return(this.p_entryCount()>this.p_entriesPerPage());},p_pageCount:function()
114 {return Math.ceil(this.p_entryCount()/this.p_entriesPerPage());},p_updateRange:function()
115 {var pageCount=this.p_pageCount();var currentPage=this.p_currentPage();if(currentPage>=pageCount)
116 {currentPage=pageCount-1;this.setPreferenceForKey(currentPage,"x-currentPage");}
117 if(pageCount<=5||this.mRange.length()<3||this.mRange.max()>pageCount)
118 {this.mRange.setMax(Math.min(5,pageCount));}
119 if(currentPage<this.mRange.min())
120 {this.mRange.shift(currentPage-this.mRange.min());}
121 else if(currentPage>=this.mRange.max())
122 {this.mRange.shift(currentPage-this.mRange.max()+1);}}});