IconContainer.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mobile.IconContainer"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.IconContainer"] = true;
  8. dojo.provide("dojox.mobile.IconContainer");
  9. dojo.require("dojox.mobile");
  10. dojo.declare(
  11. "dojox.mobile.IconContainer",
  12. dijit._WidgetBase,
  13. {
  14. defaultIcon: "",
  15. transition: "below", // slide, flip, or below
  16. pressedIconOpacity: 0.4,
  17. iconBase: "",
  18. iconPos: "",
  19. back: "Home",
  20. label: "My Application",
  21. single: false,
  22. buildRendering: function(){
  23. this.domNode = this.containerNode = this.srcNodeRef || dojo.doc.createElement("UL");
  24. this.domNode.className = "mblIconContainer";
  25. var t = this._terminator = dojo.create("LI");
  26. t.className = "mblIconItemTerminator";
  27. t.innerHTML = " ";
  28. this.domNode.appendChild(t);
  29. },
  30. _setupSubNodes: function(ul){
  31. var len = this.domNode.childNodes.length - 1; // -1 for terminator
  32. for(i = 0; i < len; i++){
  33. child = this.domNode.childNodes[i];
  34. if(child.nodeType != 1){ continue; }
  35. w = dijit.byNode(child);
  36. if(this.single){
  37. w.subNode.firstChild.style.display = "none";
  38. }
  39. ul.appendChild(w.subNode);
  40. }
  41. },
  42. startup: function(){
  43. var ul, i, len, child, w;
  44. if(this.transition == "below"){
  45. this._setupSubNodes(this.domNode);
  46. }else{
  47. var view = new dojox.mobile.View({id:this.id+"_mblApplView"});
  48. var _this = this;
  49. view.onAfterTransitionIn = function(moveTo, dir, transition, context, method){
  50. _this._opening._open_1();
  51. };
  52. view.domNode.style.visibility = "hidden";
  53. var heading = view._heading = new dojox.mobile.Heading({back:this.back, label:this.label, moveTo:this.domNode.parentNode.id, transition:this.transition});
  54. view.addChild(heading);
  55. ul = dojo.doc.createElement("UL");
  56. ul.className = "mblIconContainer";
  57. ul.style.marginTop = "0px";
  58. this._setupSubNodes(ul);
  59. view.domNode.appendChild(ul);
  60. dojo.doc.body.appendChild(view.domNode);
  61. heading.startup();
  62. }
  63. },
  64. closeAll: function(){
  65. var len = this.domNode.childNodes.length;
  66. for(var i = 0; i < len; i++){
  67. child = this.domNode.childNodes[i];
  68. if(child.nodeType != 1){ continue; }
  69. if(child == this._terminator){ break; }
  70. w = dijit.byNode(child);
  71. w.containerNode.parentNode.style.display = "none";
  72. w.setOpacity(w.iconNode, 1);
  73. }
  74. },
  75. addChild: function(widget){
  76. this.domNode.insertBefore(widget.domNode, this._terminator);
  77. widget.transition = this.transition;
  78. if(this.transition == "below"){
  79. this.domNode.appendChild(widget.subNode);
  80. }
  81. widget.inheritParams();
  82. widget.setIcon();
  83. }
  84. });
  85. dojo.declare(
  86. "dojox.mobile.IconItem",
  87. dojox.mobile.AbstractItem,
  88. {
  89. // description:
  90. // Dynamic creation is not supported.
  91. lazy: false,
  92. requires: "",
  93. timeout: 10,
  94. templateString: '<li class="mblIconItem">'+
  95. '<div class="mblIconArea" dojoAttachPoint="iconDivNode">'+
  96. '<div><img src="${icon}" dojoAttachPoint="iconNode"></div>${label}'+
  97. '</div>'+
  98. '</li>',
  99. templateStringSub: '<li class="mblIconItemSub" lazy="${lazy}" style="display:none;" dojoAttachPoint="contentNode">'+
  100. '<h2 class="mblIconContentHeading" dojoAttachPoint="closeNode">'+
  101. '<div class="mblBlueMinusButton" style="position:absolute;left:4px;top:2px;" dojoAttachPoint="closeIconNode"><div></div></div>${label}'+
  102. '</h2>'+
  103. '<div class="mblContent" dojoAttachPoint="containerNode"></div>'+
  104. '</li>',
  105. createTemplate: function(s){
  106. dojo.forEach(["lazy","icon","label"], function(v){
  107. while(s.indexOf("${"+v+"}") != -1){
  108. s = s.replace("${"+v+"}", this[v]);
  109. }
  110. }, this);
  111. var div = dojo.doc.createElement("DIV");
  112. div.innerHTML = s;
  113. /*
  114. dojo.forEach(dojo.query("[dojoAttachPoint]", domNode), function(node){
  115. this[node.getAttribute("dojoAttachPoint")] = node;
  116. }, this);
  117. */
  118. var nodes = div.getElementsByTagName("*");
  119. var i, len, s1;
  120. len = nodes.length;
  121. for(i = 0; i < len; i++){
  122. s1 = nodes[i].getAttribute("dojoAttachPoint");
  123. if(s1){
  124. this[s1] = nodes[i];
  125. }
  126. }
  127. var domNode = div.removeChild(div.firstChild);
  128. div = null;
  129. return domNode;
  130. },
  131. buildRendering: function(){
  132. this.inheritParams();
  133. this.domNode = this.createTemplate(this.templateString);
  134. this.subNode = this.createTemplate(this.templateStringSub);
  135. this.subNode._parentNode = this.domNode; // [custom property]
  136. if(this.srcNodeRef){
  137. // reparent
  138. for(var i = 0, len = this.srcNodeRef.childNodes.length; i < len; i++){
  139. this.containerNode.appendChild(this.srcNodeRef.removeChild(this.srcNodeRef.firstChild));
  140. }
  141. this.srcNodeRef.parentNode.replaceChild(this.domNode, this.srcNodeRef);
  142. this.srcNodeRef = null;
  143. }
  144. this.setIcon();
  145. },
  146. setIcon: function(){
  147. this.iconNode.src = this.icon;
  148. dojox.mobile.setupIcon(this.iconNode, this.iconPos);
  149. },
  150. postCreate: function(){
  151. this.connect(this.iconNode, "onmousedown", "onMouseDownIcon");
  152. this.connect(this.iconNode, "onclick", "iconClicked");
  153. this.connect(this.closeIconNode, "onclick", "closeIconClicked");
  154. this.connect(this.iconNode, "onerror", "onError");
  155. },
  156. highlight: function(){
  157. dojo.addClass(this.iconDivNode, "mblVibrate");
  158. if(this.timeout > 0){
  159. var _this = this;
  160. setTimeout(function(){
  161. _this.unhighlight();
  162. }, this.timeout*1000);
  163. }
  164. },
  165. unhighlight: function(){
  166. dojo.removeClass(this.iconDivNode, "mblVibrate");
  167. },
  168. setOpacity: function(node, val){
  169. node.style.opacity = val;
  170. node.style.mozOpacity = val;
  171. node.style.khtmlOpacity = val;
  172. node.style.webkitOpacity = val;
  173. },
  174. instantiateWidget: function(e){
  175. // avoid use of dojo.query
  176. /*
  177. var list = dojo.query('[dojoType]', this.containerNode);
  178. for(var i = 0, len = list.length; i < len; i++){
  179. dojo["require"](list[i].getAttribute("dojoType"));
  180. }
  181. */
  182. var nodes = this.containerNode.getElementsByTagName("*");
  183. var len = nodes.length;
  184. var s;
  185. for(var i = 0; i < len; i++){
  186. s = nodes[i].getAttribute("dojoType");
  187. if(s){
  188. dojo["require"](s);
  189. }
  190. }
  191. if(len > 0){
  192. (dojox.mobile.parser || dojo.parser).parse(this.containerNode);
  193. }
  194. this.lazy = false;
  195. },
  196. isOpen: function(e){
  197. return this.containerNode.style.display != "none";
  198. },
  199. onMouseDownIcon: function (e){
  200. this.setOpacity(this.iconNode, this.getParentWidget().pressedIconOpacity);
  201. },
  202. iconClicked: function(e){
  203. if(e){
  204. setTimeout(dojo.hitch(this, function(d){ this.iconClicked(); }), 0);
  205. return;
  206. }
  207. if(this.moveTo || this.href || this.url){
  208. this.transitionTo(this.moveTo, this.href, this.url);
  209. setTimeout(dojo.hitch(this, function(d){
  210. this.setOpacity(this.iconNode, 1);
  211. }), 1500);
  212. }else{
  213. this.open();
  214. }
  215. },
  216. closeIconClicked: function(e){
  217. if(e){
  218. setTimeout(dojo.hitch(this, function(d){ this.closeIconClicked(); }), 0);
  219. return;
  220. }
  221. this.close();
  222. },
  223. open: function(){
  224. var parent = this.getParentWidget(); // IconContainer
  225. if(this.transition == "below"){
  226. if(parent.single){
  227. parent.closeAll();
  228. this.setOpacity(this.iconNode, this.getParentWidget().pressedIconOpacity);
  229. }
  230. this._open_1();
  231. }else{
  232. parent._opening = this;
  233. if(parent.single){
  234. parent.closeAll();
  235. var view = dijit.byId(parent.id+"_mblApplView");
  236. view._heading.setLabel(this.label);
  237. }
  238. this.transitionTo(parent.id+"_mblApplView");
  239. }
  240. },
  241. _open_1: function(){
  242. this.contentNode.style.display = "";
  243. this.unhighlight();
  244. if(this.lazy){
  245. if(this.requires){
  246. dojo.forEach(this.requires.split(/,/), function(c){
  247. dojo["require"](c);
  248. });
  249. }
  250. this.instantiateWidget();
  251. }
  252. this.contentNode.scrollIntoView();
  253. this.onOpen();
  254. },
  255. close: function(){
  256. if(dojo.isWebKit){
  257. var t = this.domNode.parentNode.offsetWidth/8;
  258. var y = this.iconNode.offsetLeft;
  259. var pos = 0;
  260. for(var i = 1; i <= 3; i++){
  261. if(t*(2*i-1) < y && y <= t*(2*(i+1)-1)){
  262. pos = i;
  263. break;
  264. }
  265. }
  266. dojo.addClass(this.containerNode.parentNode, "mblCloseContent mblShrink"+pos);
  267. }else{
  268. this.containerNode.parentNode.style.display = "none";
  269. }
  270. this.setOpacity(this.iconNode, 1);
  271. this.onClose();
  272. },
  273. onOpen: function(){
  274. // stub method to allow the application to connect to.
  275. },
  276. onClose: function(){
  277. // stub method to allow the application to connect to.
  278. },
  279. onError: function(){
  280. this.iconNode.src = this.getParentWidget().defaultIcon;
  281. }
  282. });
  283. }