dijit.js 7.3 KB

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