templates.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*!
  2. * Nokia Mobile Web Templates v0.5
  3. * http://forumnokia.com
  4. *
  5. * Copyright (c) 2009 Forum Nokia
  6. *
  7. */
  8. /*
  9. * Slideshow(string:id, int:index, boolean:wrap)
  10. * usage: mySlideshow = new Slideshow("coming-soon", 0, true);
  11. *
  12. */
  13. function Slideshow(_id, _index, _wrap) {
  14. var slideshow = document.getElementById(_id);
  15. var index, wrap, preview, caption, link;
  16. var images = new Array();
  17. var context = this;
  18. // if no index was set assume we're starting at the beginning
  19. (_index)? index = _index : index = 0;
  20. // does the slideshow wrap around at the beginning and end?
  21. (_wrap)? wrap = _wrap : wrap = false;
  22. // if the slideshow id isn't found do nothing (false)...
  23. (slideshow)?init():false;
  24. function init() {
  25. // find all <img>'s along with the <a class="preview>
  26. // and <img /> elements within the slideshow
  27. var _images = slideshow.getElementsByTagName("img");
  28. for (var i=0; i < _images.length; i++) {
  29. var isPreview = _images[i].parentNode.className.search(/preview/)+1;
  30. if (isPreview) {
  31. // set the preview image reference
  32. preview = _images[i];
  33. link = _images[i].parentNode;
  34. } else {
  35. // push all other images into the images array
  36. images.push(_images[i]);
  37. }
  38. }
  39. // find the preview <p> element and reference it for caption
  40. var _span = slideshow.getElementsByTagName("span");
  41. for (var i=0; i < _span.length; i++) {
  42. var isPreview = _images[i].parentNode.className.search(/preview/)+1;
  43. if (isPreview) {
  44. caption = _span[i];
  45. }
  46. }
  47. // find and enable the previous and next controls
  48. var _ul = slideshow.getElementsByTagName("ul");
  49. for (var i=0; i < _ul.length; i++) {
  50. var isControls = _ul[i].className.search(/controls/)+1;
  51. if (isControls) {
  52. var _a = _ul[i].getElementsByTagName("a");
  53. for (var j=0; j < _a.length; j++) {
  54. var isPrevious = _a[j].className.search(/previous/)+1;
  55. var isNext = _a[j].className.search(/next/)+1;
  56. // wire up previous button
  57. if (isPrevious) {
  58. _a[j].onclick = function() {
  59. context.previous();
  60. }
  61. }
  62. // wire up next button
  63. if (isNext) {
  64. _a[j].onclick = function() {
  65. context.next();
  66. }
  67. }
  68. }
  69. }
  70. }
  71. // kick things off with an initial update
  72. update();
  73. }
  74. function update() {
  75. // tweak index if at start or end based on wrap property
  76. (index<0)?((wrap)?index=images.length-1:index=0):false;
  77. (index>images.length-1)?((wrap)?index=0:index=images.length-1):false;
  78. // update the view
  79. preview.setAttribute("src", images[index].getAttribute("src"));
  80. caption.innerHTML = images[index].getAttribute("alt");
  81. link.setAttribute("href", images[index].parentNode.getAttribute("href"));
  82. }
  83. this.previous = function () {
  84. // select the previous image by index and update the view
  85. index--;update();
  86. }
  87. this.next = function () {
  88. // select the next image by index and update the view
  89. index++;update();
  90. }
  91. }
  92. /*
  93. * AccordionList(string:id, callback:function)
  94. * usage: myAccordianList = new AccordianList(id, callback);
  95. * - id 'foo' can also be an array such as ids['foo','bar']
  96. * - callback (optional) function is triggered when a <dt> within the list is clicked
  97. * and passes a reference to itself to the defined callback function.
  98. */
  99. function AccordionList(_id, _callback) {
  100. var id = new Array();
  101. var callback;
  102. (!_isArray(_id))?id.push(_id):id=_id;
  103. (typeof _callback=="function")?callback=_callback:callback=function(){};
  104. for (var x=0;x<id.length;x++) {
  105. var dl = document.getElementById(id[x]);
  106. var dt = dl.getElementsByTagName("dt");
  107. for (var j=0; j < dt.length; j++) {
  108. var state = dt[j].getAttribute("class");
  109. // no classes defined, add class attribute with value 'collapsed'
  110. if (state == null) {
  111. dt[j].setAttribute("class", "collapsed");
  112. state = dt[j].getAttribute("class");
  113. }
  114. var expanded = state.search(/expanded/)+1;
  115. // find corresponding dd element
  116. var dd = dt[j];
  117. do dd = dd.nextSibling;
  118. while (dd && dd.nodeType != 1);
  119. (expanded)? dd.style['display'] = "block" : dd.style['display'] = "none" ;
  120. dt[j].onclick = function() {
  121. var dd = this;
  122. var state = this.getAttribute("class");
  123. var expanded = state.search(/expanded/)+1;
  124. var toggle;
  125. (expanded) ? toggle = state.replace(/expanded/, "collapsed") : toggle = state.replace(/collapsed/, "expanded") ;
  126. this.setAttribute("class", toggle);
  127. do dd = dd.nextSibling;
  128. while (dd && dd.nodeType != 1);
  129. (dd.style['display'] == "none")? dd.style['display'] = "block" : dd.style['display'] = "none" ;
  130. callback(this);
  131. }
  132. }
  133. }
  134. }
  135. /*
  136. * toggleSwitch()
  137. * usage: mySwitch = new toggleSwitch(id, function);
  138. * id can also be an array such as ids['foo','bar'…]
  139. *
  140. */
  141. function ToggleSwitch(_id, _callback) {
  142. var id = new Array();
  143. var callback;
  144. (!_isArray(_id))?id.push(_id):id=_id;
  145. (typeof _callback=="function")?callback=_callback:callback=function(){};
  146. for (var x=0;x<id.length;x++) {
  147. var toggle = document.getElementById(id[x]);
  148. toggle.style['display'] = "none";
  149. // now let's build the toggle switch dynamically...
  150. var ol = document.createElement("ol");
  151. // set the class based on the state of the toggle (checkbox)
  152. var toggleClass = "toggle-switch ";
  153. (toggle.checked)?toggleClass += "on":toggleClass += "off";
  154. ol.setAttribute("class", toggleClass);
  155. // create the <li class="label-on"> element
  156. var lion = document.createElement("li");
  157. lion.setAttribute("class", "label-on");
  158. // create the <li class="label-off"> element
  159. var lioff = document.createElement("li");
  160. lioff.setAttribute("class", "label-off");
  161. // create the 'on' <a> element
  162. var aon = document.createElement("a");
  163. aon.setAttribute("href", "#on");
  164. aon.appendChild(document.createTextNode("on"));
  165. // create the 'off' <a> element
  166. var aoff = document.createElement("a");
  167. aoff.setAttribute("href", "#off");
  168. aoff.appendChild(document.createTextNode("off"));
  169. // assemble all of the various elements
  170. lioff.appendChild(aoff);
  171. lion.appendChild(aon);
  172. ol.appendChild(lion);
  173. ol.appendChild(lioff);
  174. // clone and add the original (and hidden) checkbox to the toggle swithc
  175. ol.appendChild(toggle.cloneNode(true));
  176. // add the click event
  177. ol.onclick = function() {
  178. var state = this.getAttribute("class");
  179. var on = state.search(/on/)+1;
  180. var toggle;
  181. var checkbox = this.getElementsByTagName("input");
  182. if (on) {
  183. toggle = state.replace(/on/, "off");
  184. checkbox[0].removeAttribute("checked");
  185. } else {
  186. toggle = state.replace(/off/, "on");
  187. checkbox[0].setAttribute("checked", "true");
  188. }
  189. this.setAttribute("class", toggle);
  190. callback(this);
  191. }
  192. // replace the original 'toggle' element with the new one
  193. toggle.parentNode.replaceChild(ol, toggle);
  194. }
  195. }
  196. /*
  197. * styleTweaker()
  198. * usage: myStyleTweaker = new styleTweaker();
  199. * id can also be an array such as ids['foo','bar'…]
  200. *
  201. */
  202. function StyleTweaker() {
  203. this.ua = navigator.userAgent;
  204. this.tweaks = new Object();
  205. }
  206. StyleTweaker.prototype.add = function(_string, _stylesheet) {
  207. this.tweaks[_string] = _stylesheet;
  208. }
  209. StyleTweaker.prototype.remove = function(_term) {
  210. for (var _string in this.tweaks) {
  211. var exists = false;
  212. (_string == _term)?exists=true:false;
  213. (this.tweaks[_string])?exists=true:false;
  214. (exists)?delete this.tweaks[_string]:false;
  215. }
  216. }
  217. StyleTweaker.prototype.tweak = function() {
  218. for (var _string in this.tweaks) {
  219. if (this.ua.match(_string)) {
  220. loadStylesheet(this.tweaks[_string]);
  221. }
  222. }
  223. }
  224. StyleTweaker.prototype.untweak = function() {
  225. for (var _string in this.tweaks) {
  226. if (this.ua.match(_string)) {
  227. removeStylesheet(this.tweaks[_string]);
  228. }
  229. }
  230. }
  231. /*
  232. * _isArray()
  233. * usage: _isArray(object);
  234. *
  235. */
  236. function _isArray(x){
  237. return ((typeof x == "object") && (x.constructor == Array));
  238. }
  239. /*
  240. * addEvent()
  241. * usage: addEvent(event, function);
  242. * note: only targets window events!
  243. *
  244. */
  245. function addEvent(_event, _function) {
  246. var _current_event = window[_event];
  247. if (typeof window[_event] != 'function') {
  248. window[_event] = _function;
  249. } else {
  250. window[_event] = function() {
  251. _current_event();
  252. _function();
  253. }
  254. }
  255. }
  256. /*
  257. * include(file)
  258. * usage: include(filename.js);
  259. *
  260. */
  261. function include(filename) {
  262. var head = document.getElementsByTagName("head")[0];
  263. var script = document.createElement("script");
  264. script.setAttribute("type", "text/javascript");
  265. script.setAttribute("src", filename);
  266. head.appendChild(script);
  267. }
  268. /*
  269. * loadStylesheet(file)
  270. * usage: loadStylesheet(filename.css);
  271. *
  272. */
  273. function loadStylesheet(filename) {
  274. var head = document.getElementsByTagName('head')[0];
  275. var link = document.createElement("link");
  276. link.setAttribute("rel", "stylesheet");
  277. link.setAttribute("type", "text/css");
  278. link.setAttribute("href", filename);
  279. head.appendChild(link);
  280. }
  281. /*
  282. * removeStylesheet(file)
  283. * usage: removeStylesheet(filename.css);
  284. *
  285. */
  286. function removeStylesheet(filename) {
  287. var stylesheets=document.getElementsByTagName("link");
  288. for (var i=stylesheets.length; i>=0; i--) {
  289. if (stylesheets[i] && stylesheets[i].getAttribute("href")!=null && stylesheets[i].getAttribute("href").indexOf(filename)!=-1) {
  290. stylesheets[i].parentNode.removeChild(stylesheets[i]);
  291. }
  292. }
  293. }