_ViewManager.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.grid._ViewManager"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid._ViewManager"] = true;
  8. dojo.provide("dojox.grid._ViewManager");
  9. dojo.declare('dojox.grid._ViewManager', null, {
  10. // summary:
  11. // A collection of grid views. Owned by grid and used internally for managing grid views.
  12. // description:
  13. // Grid creates views automatically based on grid's layout structure.
  14. // Users should typically not need to access individual views or the views collection directly.
  15. constructor: function(inGrid){
  16. this.grid = inGrid;
  17. },
  18. defaultWidth: 200,
  19. views: [],
  20. // operations
  21. resize: function(){
  22. this.onEach("resize");
  23. },
  24. render: function(){
  25. this.onEach("render");
  26. },
  27. // views
  28. addView: function(inView){
  29. inView.idx = this.views.length;
  30. this.views.push(inView);
  31. },
  32. destroyViews: function(){
  33. for(var i=0, v; v=this.views[i]; i++){
  34. v.destroy();
  35. }
  36. this.views = [];
  37. },
  38. getContentNodes: function(){
  39. var nodes = [];
  40. for(var i=0, v; v=this.views[i]; i++){
  41. nodes.push(v.contentNode);
  42. }
  43. return nodes;
  44. },
  45. forEach: function(inCallback){
  46. for(var i=0, v; v=this.views[i]; i++){
  47. inCallback(v, i);
  48. }
  49. },
  50. onEach: function(inMethod, inArgs){
  51. inArgs = inArgs || [];
  52. for(var i=0, v; v=this.views[i]; i++){
  53. if(inMethod in v){
  54. v[inMethod].apply(v, inArgs);
  55. }
  56. }
  57. },
  58. // layout
  59. normalizeHeaderNodeHeight: function(){
  60. var rowNodes = [];
  61. for(var i=0, v; (v=this.views[i]); i++){
  62. if(v.headerContentNode.firstChild){
  63. rowNodes.push(v.headerContentNode);
  64. }
  65. }
  66. this.normalizeRowNodeHeights(rowNodes);
  67. },
  68. normalizeRowNodeHeights: function(inRowNodes){
  69. var h = 0;
  70. var currHeights = [];
  71. if(this.grid.rowHeight){
  72. h = this.grid.rowHeight;
  73. }else{
  74. if(inRowNodes.length <= 1){
  75. // no need to normalize if we are the only one...
  76. return;
  77. }
  78. for(var i=0, n; (n=inRowNodes[i]); i++){
  79. // We only care about the height - so don't use marginBox. This
  80. // depends on the container not having any margin (which it shouldn't)
  81. // Also - we only look up the height if the cell doesn't have the
  82. // dojoxGridNonNormalizedCell class (like for row selectors)
  83. if(!dojo.hasClass(n, "dojoxGridNonNormalizedCell")){
  84. currHeights[i] = n.firstChild.offsetHeight;
  85. h = Math.max(h, currHeights[i]);
  86. }
  87. }
  88. h = (h >= 0 ? h : 0);
  89. //Work around odd FF3 rendering bug: #8864.
  90. //A one px increase fixes FireFox 3's rounding bug for fractional font sizes.
  91. if((dojo.isMoz || dojo.isIE > 8) && h){h++;}
  92. }
  93. for(i=0; (n=inRowNodes[i]); i++){
  94. if(currHeights[i] != h){
  95. n.firstChild.style.height = h + "px";
  96. }
  97. }
  98. },
  99. resetHeaderNodeHeight: function(){
  100. for(var i=0, v, n; (v=this.views[i]); i++){
  101. n = v.headerContentNode.firstChild;
  102. if(n){
  103. n.style.height = "";
  104. }
  105. }
  106. },
  107. renormalizeRow: function(inRowIndex){
  108. var rowNodes = [];
  109. for(var i=0, v, n; (v=this.views[i])&&(n=v.getRowNode(inRowIndex)); i++){
  110. n.firstChild.style.height = '';
  111. rowNodes.push(n);
  112. }
  113. this.normalizeRowNodeHeights(rowNodes);
  114. },
  115. getViewWidth: function(inIndex){
  116. return this.views[inIndex].getWidth() || this.defaultWidth;
  117. },
  118. // must be called after view widths are properly set or height can be miscalculated
  119. // if there are flex columns
  120. measureHeader: function(){
  121. // need to reset view header heights so they are properly measured.
  122. this.resetHeaderNodeHeight();
  123. this.forEach(function(inView){
  124. inView.headerContentNode.style.height = '';
  125. });
  126. var h = 0;
  127. // calculate maximum view header height
  128. this.forEach(function(inView){
  129. h = Math.max(inView.headerNode.offsetHeight, h);
  130. });
  131. return h;
  132. },
  133. measureContent: function(){
  134. var h = 0;
  135. this.forEach(function(inView){
  136. h = Math.max(inView.domNode.offsetHeight, h);
  137. });
  138. return h;
  139. },
  140. findClient: function(inAutoWidth){
  141. // try to use user defined client
  142. var c = this.grid.elasticView || -1;
  143. // attempt to find implicit client
  144. if(c < 0){
  145. for(var i=1, v; (v=this.views[i]); i++){
  146. if(v.viewWidth){
  147. for(i=1; (v=this.views[i]); i++){
  148. if(!v.viewWidth){
  149. c = i;
  150. break;
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. }
  157. // client is in the middle by default
  158. if(c < 0){
  159. c = Math.floor(this.views.length / 2);
  160. }
  161. return c;
  162. },
  163. arrange: function(l, w){
  164. var i, v, vw, len = this.views.length;
  165. // find the client
  166. var c = (w <= 0 ? len : this.findClient());
  167. // layout views
  168. var setPosition = function(v, l){
  169. var ds = v.domNode.style;
  170. var hs = v.headerNode.style;
  171. if(!dojo._isBodyLtr()){
  172. ds.right = l + 'px';
  173. // fixed rtl, the scrollbar is on the right side in FF < 4
  174. if (dojo.isFF < 4) {
  175. hs.right = l + v.getScrollbarWidth() + 'px';
  176. }else{
  177. hs.right = l + 'px';
  178. }
  179. if(!dojo.isWebKit){
  180. hs.width = parseInt(hs.width, 10) - v.getScrollbarWidth() + 'px';
  181. }
  182. }else{
  183. ds.left = l + 'px';
  184. hs.left = l + 'px';
  185. }
  186. ds.top = 0 + 'px';
  187. hs.top = 0;
  188. };
  189. // for views left of the client
  190. //BiDi TODO: The left and right should not appear in BIDI environment. Should be replaced with
  191. //leading and tailing concept.
  192. for(i=0; (v=this.views[i])&&(i<c); i++){
  193. // get width
  194. vw = this.getViewWidth(i);
  195. // process boxes
  196. v.setSize(vw, 0);
  197. setPosition(v, l);
  198. if(v.headerContentNode && v.headerContentNode.firstChild){
  199. vw = v.getColumnsWidth()+v.getScrollbarWidth();
  200. }else{
  201. vw = v.domNode.offsetWidth;
  202. }
  203. // update position
  204. l += vw;
  205. }
  206. // next view (is the client, i++ == c)
  207. i++;
  208. // start from the right edge
  209. var r = w;
  210. // for views right of the client (iterated from the right)
  211. for(var j=len-1; (v=this.views[j])&&(i<=j); j--){
  212. // get width
  213. vw = this.getViewWidth(j);
  214. // set size
  215. v.setSize(vw, 0);
  216. // measure in pixels
  217. vw = v.domNode.offsetWidth;
  218. // update position
  219. r -= vw;
  220. // set position
  221. setPosition(v, r);
  222. }
  223. if(c<len){
  224. v = this.views[c];
  225. // position the client box between left and right boxes
  226. vw = Math.max(1, r-l);
  227. // set size
  228. v.setSize(vw + 'px', 0);
  229. setPosition(v, l);
  230. }
  231. return l;
  232. },
  233. // rendering
  234. renderRow: function(inRowIndex, inNodes, skipRenorm){
  235. var rowNodes = [];
  236. for(var i=0, v, n, rowNode; (v=this.views[i])&&(n=inNodes[i]); i++){
  237. rowNode = v.renderRow(inRowIndex);
  238. n.appendChild(rowNode);
  239. rowNodes.push(rowNode);
  240. }
  241. if(!skipRenorm){
  242. this.normalizeRowNodeHeights(rowNodes);
  243. }
  244. },
  245. rowRemoved: function(inRowIndex){
  246. this.onEach("rowRemoved", [ inRowIndex ]);
  247. },
  248. // updating
  249. updateRow: function(inRowIndex, skipRenorm){
  250. for(var i=0, v; v=this.views[i]; i++){
  251. v.updateRow(inRowIndex);
  252. }
  253. if(!skipRenorm){
  254. this.renormalizeRow(inRowIndex);
  255. }
  256. },
  257. updateRowStyles: function(inRowIndex){
  258. this.onEach("updateRowStyles", [ inRowIndex ]);
  259. },
  260. // scrolling
  261. setScrollTop: function(inTop){
  262. var top = inTop;
  263. for(var i=0, v; v=this.views[i]; i++){
  264. top = v.setScrollTop(inTop);
  265. // Work around IE not firing scroll events that cause header offset
  266. // issues to occur.
  267. if(dojo.isIE && v.headerNode && v.scrollboxNode){
  268. v.headerNode.scrollLeft = v.scrollboxNode.scrollLeft;
  269. }
  270. }
  271. return top;
  272. //this.onEach("setScrollTop", [ inTop ]);
  273. },
  274. getFirstScrollingView: function(){
  275. // summary: Returns the first grid view with a scroll bar
  276. for(var i=0, v; (v=this.views[i]); i++){
  277. if(v.hasHScrollbar() || v.hasVScrollbar()){
  278. return v;
  279. }
  280. }
  281. return null;
  282. }
  283. });
  284. }