/branches/jsdoc_tk_gui/setup/workingDirectory/Webeo/gui/button/Button.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 273 lines · 187 code · 38 blank · 48 comment · 56 complexity · dfbd6f5ef2c3f883903591a296372eb0 MD5 · raw file

  1. ek.require("gui.tooltip.Tooltip");
  2. ek.register("gui.button.Button");
  3. ek.requireCSS("css.gui.button.Button", "button.Button");
  4. /**
  5. *@require "position.js"
  6. *@require "tooltip.js"
  7. **/
  8. var PICTURE_BUTTON = 0;
  9. var INPUT_BUTTON = 1;
  10. var TEXT_BUTTON = 2;
  11. function PictureButton(idName, shortName, extension, toolTipText, toggle, mainModel, clickMethod, isAjax, url){
  12. return new Button(PICTURE_BUTTON, idName, shortName, extension, toolTipText, toggle, mainModel, clickMethod, isAjax, url);
  13. }
  14. function SimpleButton(idName, shortName, extension, toolTipText, toggle, mainModel, clickMethod, isAjax, url){
  15. return new Button(INPUT_BUTTON, idName, shortName, extension, toolTipText, toggle, mainModel, clickMethod, isAjax, url);
  16. }
  17. function TextButton(idName, shortName, toolTipText, toggle, url, target, clickMethod){
  18. var b = new Button(TEXT_BUTTON, idName, shortName, null, toolTipText, toggle, null, clickMethod);
  19. b.initCSS("textButton", "toggled");
  20. b.initUrlAndTarget(url, target);
  21. return b;
  22. }
  23. /**
  24. * Construit un bouton
  25. *@param dom balise dom img de l'image utilisé comme bouton
  26. *@param shortName Nom des images sans sufixes
  27. *@param extension extension des images
  28. *@param tooltip Message d'aide affiché au survol du bouton
  29. *@param toggle true si c'est un bouton poussoir
  30. */
  31. function Button(type, idName, shortName, extension, toolTipText, toggle, mainModel, clickMethod, isAjax, url){
  32. this.type = type;
  33. this.toggle = toggle;
  34. this.isPressed = false;
  35. this.isDisabled = false;
  36. if(this.type == PICTURE_BUTTON){
  37. this.isImage = true;
  38. //this.parentDom = parentDom ;
  39. this.domBT = document.createElement("img") ;
  40. this.domBT.setAttribute("id", idName);
  41. //this.drawButton(idName);
  42. this.normal = new Image();
  43. this.normal.src = shortName + '.' + extension;
  44. this.over = new Image();
  45. this.over.src = shortName + '_o.' + extension;
  46. this.press = new Image();
  47. this.press.src = shortName + '_p.' + extension;
  48. this.disable = new Image();
  49. this.disable.src = shortName + '_d.' + extension;
  50. //Init
  51. this.domBT.src = this.normal.src;
  52. this.domBT.onmouseover = this.getOver;
  53. this.domBT.onmouseout = this.getOut;
  54. this.domBT.onmousedown = this.getDown;
  55. this.domBT.onmouseup = this.getUp;
  56. }else if(this.type == INPUT_BUTTON){
  57. this.isImage = false;
  58. this.domBT = document.createElement("input");
  59. this.domBT.setAttribute("id", idName);
  60. this.domBT.setAttribute("type", "button");
  61. this.domBT.setAttribute("value", shortName);
  62. }else if(this.type == TEXT_BUTTON){
  63. this.isImage = false;
  64. this.domBT = document.createElement("a");
  65. this.domBT.setAttribute("id", idName);
  66. this.domBT.textContent = shortName;
  67. this.domBT.onmouseover = this.getOver;
  68. this.domBT.onmouseout = this.getOut;
  69. this.domBT.onmousedown = this.getDown;
  70. this.domBT.onmouseup = this.getUp;
  71. }
  72. this.toolTipText = toolTipText;
  73. //On attache les mod?les du bouton et du container au DOM
  74. this.domBT.buttonModel = this;
  75. this.domBT.mainModel = mainModel;
  76. //Gestion du clic
  77. /*if(isAjax){
  78. this.url = url;
  79. this.domBT.onclick = clickMethod;
  80. }else{*/
  81. if(clickMethod){
  82. this.domBT.onclick = clickMethod;
  83. }
  84. //}
  85. }
  86. /**
  87. *
  88. */
  89. Button.prototype.drawButton = function(parentName){
  90. document.getElementById(parentName).appendChild(this.getDom());
  91. var ttp = new TooltipParams();
  92. ttp.mustManageEvent = false;
  93. this.tooltip = new Tooltip(this.domBT, this.toolTipText, ttp);
  94. }
  95. Button.prototype.initCSS = function(mainCSS, toggledCSS){
  96. this.mainCSS = mainCSS;
  97. this.toggledCSS = toggledCSS;
  98. this.domBT.setAttribute("class", this.mainCSS);
  99. }
  100. Button.prototype.initUrlAndTarget = function(url, target){
  101. if(target)
  102. this.domBT.setAttribute("target", target);
  103. if(url)
  104. this.domBT.setAttribute("href", url);
  105. }
  106. Button.prototype.gotoUrl = function(){
  107. //parent.frames[1].location.href = "sport.html";
  108. //this.domBT.setAttribute("target", target);
  109. }
  110. /**
  111. * Ecrit le button dans le document
  112. * <img id="test1" />
  113. *//*
  114. Button.prototype.drawButton = function (idName){
  115. this.domBT = document.createElement("img");
  116. var id = document.createAttribute("id");
  117. id.nodeValue = idName;
  118. this.domBT.setAttributeNode(id);
  119. this.parentDom.parentNode.appendChild(this.domBT);
  120. }*/
  121. Button.prototype.getDom = function(){
  122. return this.domBT;
  123. }
  124. Button.prototype.getTooltipDom = function(){
  125. this.tooltip = new Tooltip(this.domBT, this.toolTipText);
  126. }
  127. Button.prototype.getHTML = function(){
  128. return this.domBT.innnerHTML;
  129. }
  130. Button.prototype.unselect = function(){
  131. if(this.type == PICTURE_BUTTON){
  132. if(this.toggle){
  133. this.domBT.src = this.normal.src;
  134. this.isPressed = false;
  135. }
  136. }else if(this.type == TEXT_BUTTON){
  137. if(this.toggle){
  138. this.domBT.className = this.mainCSS;
  139. this.isPressed = false;
  140. }
  141. }
  142. }
  143. Button.prototype.select = function(){
  144. if(this.type == PICTURE_BUTTON){
  145. if(this.toggle){
  146. this.domBT.src = this.press.src;
  147. this.isPressed = true;
  148. }
  149. }else if(this.type == TEXT_BUTTON){
  150. if(this.toggle){
  151. this.domBT.className = this.mainCSS + " " + this.toggledCSS;
  152. this.isPressed = true;
  153. }
  154. }
  155. }
  156. /**
  157. *@return true si le bouton est desactivé et false sinon
  158. */
  159. Button.prototype.isDisable = function(){
  160. return this.isDisabled;
  161. }
  162. Button.prototype.changeVisibility = function(enable, visible){
  163. if(enable){
  164. this.domBT.src = this.normal.src;
  165. this.domBT.style.visibility = 'visible';
  166. this.isDisabled = false;
  167. }else{
  168. this.isDisabled = true;
  169. if(visible){
  170. if(this.type == PICTURE_BUTTON){
  171. this.domBT.src = this.disable.src;
  172. }
  173. this.domBT.style.visibility = 'visible';
  174. }else{
  175. this.domBT.style.visibility = 'hidden';
  176. }
  177. }
  178. }
  179. /**
  180. * Methods called by an event
  181. */
  182. Button.prototype.getOver = function(){
  183. //On affiche un tooltip apr?s l'avoir survolé pendant au moins 1000ms
  184. if(this.buttonModel.tooltip)
  185. this.buttonModel.tooltip.mayShow();
  186. if(this.buttonModel.type == PICTURE_BUTTON && !this.buttonModel.isDisable() && !this.buttonModel.isPressed){
  187. this.src = this.buttonModel.over.src;
  188. }else if(this.buttonModel.type == TEXT_BUTTON && !this.buttonModel.isDisable() && !this.buttonModel.isPressed){
  189. this.className = this.buttonModel.mainCSS + " " + "over";
  190. }
  191. }
  192. Button.prototype.getOut = function(){
  193. if(this.buttonModel.tooltip)
  194. this.buttonModel.tooltip.mayHide();
  195. if(this.buttonModel.type == PICTURE_BUTTON && !this.buttonModel.isDisable() && !this.buttonModel.isPressed){
  196. this.src = this.buttonModel.normal.src;
  197. }else if(this.buttonModel.type == TEXT_BUTTON && !this.buttonModel.isDisable() && !this.buttonModel.isPressed){
  198. this.className = this.buttonModel.mainCSS;
  199. }
  200. }
  201. Button.prototype.getDown = function(e){
  202. if(!this.buttonModel.isDisable())
  203. if(this.buttonModel.type == PICTURE_BUTTON){
  204. if(this.buttonModel.toggle){
  205. if(this.buttonModel.isPressed){
  206. this.src = this.buttonModel.normal.src;
  207. this.buttonModel.isPressed = false;
  208. }else{
  209. this.src = this.buttonModel.press.src;
  210. this.buttonModel.isPressed = true;
  211. }
  212. }else{
  213. this.src = this.buttonModel.press.src;
  214. }
  215. }else if(this.buttonModel.type == TEXT_BUTTON){
  216. if(this.buttonModel.toggle){
  217. if(this.buttonModel.isPressed){
  218. this.className = this.buttonModel.mainCSS;
  219. this.buttonModel.isPressed = false;
  220. }else{
  221. this.className = this.buttonModel.mainCSS + " " + this.buttonModel.toggledCSS;
  222. this.buttonModel.isPressed = true;
  223. }
  224. }else{
  225. this.className = this.buttonModel.mainCSS;
  226. }
  227. }
  228. }
  229. /**
  230. *
  231. */
  232. Button.prototype.getUp = function(){
  233. if(this.buttonModel.type == PICTURE_BUTTON && !this.buttonModel.isDisable() && !this.buttonModel.toggle)
  234. this.src = this.buttonModel.normal.src;
  235. }