dijit.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.cells.dijit"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid.cells.dijit"] = true;
  8. dojo.provide("dojox.grid.cells.dijit");
  9. dojo.require("dojox.grid.cells");
  10. // TODO: shouldn't it be the test file's job to require these modules,
  11. // if it is using them? Most of these modules aren't referenced by this file.
  12. dojo.require("dijit.form.DateTextBox");
  13. dojo.require("dijit.form.TimeTextBox");
  14. dojo.require("dijit.form.ComboBox");
  15. dojo.require("dojo.data.ItemFileReadStore");
  16. dojo.require("dijit.form.CheckBox");
  17. dojo.require("dijit.form.TextBox");
  18. dojo.require("dijit.form.NumberSpinner");
  19. dojo.require("dijit.form.NumberTextBox");
  20. dojo.require("dijit.form.CurrencyTextBox");
  21. dojo.require("dijit.form.HorizontalSlider");
  22. dojo.require("dijit.Editor");
  23. (function(){
  24. var dgc = dojox.grid.cells;
  25. dojo.declare("dojox.grid.cells._Widget", dgc._Base, {
  26. widgetClass: dijit.form.TextBox,
  27. constructor: function(inCell){
  28. this.widget = null;
  29. if(typeof this.widgetClass == "string"){
  30. dojo.deprecated("Passing a string to widgetClass is deprecated", "pass the widget class object instead", "2.0");
  31. this.widgetClass = dojo.getObject(this.widgetClass);
  32. }
  33. },
  34. formatEditing: function(inDatum, inRowIndex){
  35. this.needFormatNode(inDatum, inRowIndex);
  36. return "<div></div>";
  37. },
  38. getValue: function(inRowIndex){
  39. return this.widget.get('value');
  40. },
  41. setValue: function(inRowIndex, inValue){
  42. if(this.widget&&this.widget.set){
  43. //Look for lazy-loading editor and handle it via its deferred.
  44. if(this.widget.onLoadDeferred){
  45. var self = this;
  46. this.widget.onLoadDeferred.addCallback(function(){
  47. self.widget.set("value",inValue===null?"":inValue);
  48. });
  49. }else{
  50. this.widget.set("value", inValue);
  51. }
  52. }else{
  53. this.inherited(arguments);
  54. }
  55. },
  56. getWidgetProps: function(inDatum){
  57. return dojo.mixin(
  58. {
  59. dir: this.dir,
  60. lang: this.lang
  61. },
  62. this.widgetProps||{},
  63. {
  64. constraints: dojo.mixin({}, this.constraint) || {}, //TODO: really just for ValidationTextBoxes
  65. value: inDatum
  66. }
  67. );
  68. },
  69. createWidget: function(inNode, inDatum, inRowIndex){
  70. return new this.widgetClass(this.getWidgetProps(inDatum), inNode);
  71. },
  72. attachWidget: function(inNode, inDatum, inRowIndex){
  73. inNode.appendChild(this.widget.domNode);
  74. this.setValue(inRowIndex, inDatum);
  75. },
  76. formatNode: function(inNode, inDatum, inRowIndex){
  77. if(!this.widgetClass){
  78. return inDatum;
  79. }
  80. if(!this.widget){
  81. this.widget = this.createWidget.apply(this, arguments);
  82. }else{
  83. this.attachWidget.apply(this, arguments);
  84. }
  85. this.sizeWidget.apply(this, arguments);
  86. this.grid.views.renormalizeRow(inRowIndex);
  87. this.grid.scroller.rowHeightChanged(inRowIndex, true/*fix #11101*/);
  88. this.focus();
  89. return undefined;
  90. },
  91. sizeWidget: function(inNode, inDatum, inRowIndex){
  92. var p = this.getNode(inRowIndex);
  93. dojo.marginBox(this.widget.domNode, {w: dojo.style(p, 'width')});
  94. },
  95. focus: function(inRowIndex, inNode){
  96. if(this.widget){
  97. setTimeout(dojo.hitch(this.widget, function(){
  98. dojox.grid.util.fire(this, "focus");
  99. }), 0);
  100. }
  101. },
  102. _finish: function(inRowIndex){
  103. this.inherited(arguments);
  104. dojox.grid.util.removeNode(this.widget.domNode);
  105. if(dojo.isIE){
  106. dojo.setSelectable(this.widget.domNode, true);
  107. }
  108. }
  109. });
  110. dgc._Widget.markupFactory = function(node, cell){
  111. dgc._Base.markupFactory(node, cell);
  112. var d = dojo;
  113. var widgetProps = d.trim(d.attr(node, "widgetProps")||"");
  114. var constraint = d.trim(d.attr(node, "constraint")||"");
  115. var widgetClass = d.trim(d.attr(node, "widgetClass")||"");
  116. if(widgetProps){
  117. cell.widgetProps = d.fromJson(widgetProps);
  118. }
  119. if(constraint){
  120. cell.constraint = d.fromJson(constraint);
  121. }
  122. if(widgetClass){
  123. cell.widgetClass = d.getObject(widgetClass);
  124. }
  125. };
  126. dojo.declare("dojox.grid.cells.ComboBox", dgc._Widget, {
  127. widgetClass: dijit.form.ComboBox,
  128. getWidgetProps: function(inDatum){
  129. var items=[];
  130. dojo.forEach(this.options, function(o){
  131. items.push({name: o, value: o});
  132. });
  133. var store = new dojo.data.ItemFileReadStore({data: {identifier:"name", items: items}});
  134. return dojo.mixin({}, this.widgetProps||{}, {
  135. value: inDatum,
  136. store: store
  137. });
  138. },
  139. getValue: function(){
  140. var e = this.widget;
  141. // make sure to apply the displayed value
  142. e.set('displayedValue', e.get('displayedValue'));
  143. return e.get('value');
  144. }
  145. });
  146. dgc.ComboBox.markupFactory = function(node, cell){
  147. dgc._Widget.markupFactory(node, cell);
  148. var d=dojo;
  149. var options = d.trim(d.attr(node, "options")||"");
  150. if(options){
  151. var o = options.split(',');
  152. if(o[0] != options){
  153. cell.options = o;
  154. }
  155. }
  156. };
  157. dojo.declare("dojox.grid.cells.DateTextBox", dgc._Widget, {
  158. widgetClass: dijit.form.DateTextBox,
  159. setValue: function(inRowIndex, inValue){
  160. if(this.widget){
  161. this.widget.set('value', new Date(inValue));
  162. }else{
  163. this.inherited(arguments);
  164. }
  165. },
  166. getWidgetProps: function(inDatum){
  167. return dojo.mixin(this.inherited(arguments), {
  168. value: new Date(inDatum)
  169. });
  170. }
  171. });
  172. dgc.DateTextBox.markupFactory = function(node, cell){
  173. dgc._Widget.markupFactory(node, cell);
  174. };
  175. dojo.declare("dojox.grid.cells.CheckBox", dgc._Widget, {
  176. widgetClass: dijit.form.CheckBox,
  177. getValue: function(){
  178. return this.widget.checked;
  179. },
  180. setValue: function(inRowIndex, inValue){
  181. if(this.widget&&this.widget.attributeMap.checked){
  182. this.widget.set("checked", inValue);
  183. }else{
  184. this.inherited(arguments);
  185. }
  186. },
  187. sizeWidget: function(inNode, inDatum, inRowIndex){
  188. return;
  189. }
  190. });
  191. dgc.CheckBox.markupFactory = function(node, cell){
  192. dgc._Widget.markupFactory(node, cell);
  193. };
  194. dojo.declare("dojox.grid.cells.Editor", dgc._Widget, {
  195. widgetClass: dijit.Editor,
  196. getWidgetProps: function(inDatum){
  197. return dojo.mixin({}, this.widgetProps||{}, {
  198. height: this.widgetHeight || "100px"
  199. });
  200. },
  201. createWidget: function(inNode, inDatum, inRowIndex){
  202. // widget needs its value set after creation
  203. var widget = new this.widgetClass(this.getWidgetProps(inDatum), inNode);
  204. dojo.connect(widget, 'onLoad', dojo.hitch(this, 'populateEditor'));
  205. return widget;
  206. },
  207. formatNode: function(inNode, inDatum, inRowIndex){
  208. this.content = inDatum;
  209. this.inherited(arguments);
  210. if(dojo.isMoz){
  211. // FIXME: seem to need to reopen the editor and display the toolbar
  212. var e = this.widget;
  213. e.open();
  214. if(this.widgetToolbar){
  215. dojo.place(e.toolbar.domNode, e.editingArea, "before");
  216. }
  217. }
  218. },
  219. populateEditor: function(){
  220. this.widget.set('value', this.content);
  221. this.widget.placeCursorAtEnd();
  222. }
  223. });
  224. dgc.Editor.markupFactory = function(node, cell){
  225. dgc._Widget.markupFactory(node, cell);
  226. var d = dojo;
  227. var h = dojo.trim(dojo.attr(node, "widgetHeight")||"");
  228. if(h){
  229. if((h != "auto")&&(h.substr(-2) != "em")){
  230. h = parseInt(h, 10)+"px";
  231. }
  232. cell.widgetHeight = h;
  233. }
  234. };
  235. })();
  236. }