_FormSelectWidget.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //>>built
  2. define("dijit/form/_FormSelectWidget",["dojo/_base/array","dojo/_base/Deferred","dojo/aspect","dojo/data/util/sorter","dojo/_base/declare","dojo/dom","dojo/dom-class","dojo/_base/kernel","dojo/_base/lang","dojo/query","dojo/when","dojo/store/util/QueryResults","./_FormValueWidget"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d){
  3. var _e=_5("dijit.form._FormSelectWidget",_d,{multiple:false,options:null,store:null,query:null,queryOptions:null,labelAttr:"",onFetch:null,sortByLabel:true,loadChildrenOnOpen:false,onLoadDeferred:null,getOptions:function(_f){
  4. var _10=this.options||[];
  5. if(_f==null){
  6. return _10;
  7. }
  8. if(_9.isArray(_f)){
  9. return _1.map(_f,"return this.getOptions(item);",this);
  10. }
  11. if(_9.isString(_f)){
  12. _f={value:_f};
  13. }
  14. if(_9.isObject(_f)){
  15. if(!_1.some(_10,function(_11,idx){
  16. for(var a in _f){
  17. if(!(a in _11)||_11[a]!=_f[a]){
  18. return false;
  19. }
  20. }
  21. _f=idx;
  22. return true;
  23. })){
  24. _f=-1;
  25. }
  26. }
  27. if(_f>=0&&_f<_10.length){
  28. return _10[_f];
  29. }
  30. return null;
  31. },addOption:function(_12){
  32. _1.forEach(_9.isArray(_12)?_12:[_12],function(i){
  33. if(i&&_9.isObject(i)){
  34. this.options.push(i);
  35. }
  36. },this);
  37. this._loadChildren();
  38. },removeOption:function(_13){
  39. var _14=this.getOptions(_9.isArray(_13)?_13:[_13]);
  40. _1.forEach(_14,function(_15){
  41. if(_15){
  42. this.options=_1.filter(this.options,function(_16){
  43. return (_16.value!==_15.value||_16.label!==_15.label);
  44. });
  45. this._removeOptionItem(_15);
  46. }
  47. },this);
  48. this._loadChildren();
  49. },updateOption:function(_17){
  50. _1.forEach(_9.isArray(_17)?_17:[_17],function(i){
  51. var _18=this.getOptions({value:i.value}),k;
  52. if(_18){
  53. for(k in i){
  54. _18[k]=i[k];
  55. }
  56. }
  57. },this);
  58. this._loadChildren();
  59. },setStore:function(_19,_1a,_1b){
  60. var _1c=this.store;
  61. _1b=_1b||{};
  62. if(_1c!==_19){
  63. var h;
  64. while((h=this._notifyConnections.pop())){
  65. h.remove();
  66. }
  67. if(!_19.get){
  68. _9.mixin(_19,{_oldAPI:true,get:function(id){
  69. var _1d=new _2();
  70. this.fetchItemByIdentity({identity:id,onItem:function(_1e){
  71. _1d.resolve(_1e);
  72. },onError:function(_1f){
  73. _1d.reject(_1f);
  74. }});
  75. return _1d.promise;
  76. },query:function(_20,_21){
  77. var _22=new _2(function(){
  78. if(_23.abort){
  79. _23.abort();
  80. }
  81. });
  82. _22.total=new _2();
  83. var _23=this.fetch(_9.mixin({query:_20,onBegin:function(_24){
  84. _22.total.resolve(_24);
  85. },onComplete:function(_25){
  86. _22.resolve(_25);
  87. },onError:function(_26){
  88. _22.reject(_26);
  89. }},_21));
  90. return new _c(_22);
  91. }});
  92. if(_19.getFeatures()["dojo.data.api.Notification"]){
  93. this._notifyConnections=[_3.after(_19,"onNew",_9.hitch(this,"_onNewItem"),true),_3.after(_19,"onDelete",_9.hitch(this,"_onDeleteItem"),true),_3.after(_19,"onSet",_9.hitch(this,"_onSetItem"),true)];
  94. }
  95. }
  96. this._set("store",_19);
  97. }
  98. if(this.options&&this.options.length){
  99. this.removeOption(this.options);
  100. }
  101. if(this._queryRes&&this._queryRes.close){
  102. this._queryRes.close();
  103. }
  104. if(_1b.query){
  105. this._set("query",_1b.query);
  106. this._set("queryOptions",_1b.queryOptions);
  107. }
  108. if(_19){
  109. this._loadingStore=true;
  110. this.onLoadDeferred=new _2();
  111. this._queryRes=_19.query(this.query,this.queryOptions);
  112. _b(this._queryRes,_9.hitch(this,function(_27){
  113. if(this.sortByLabel&&!_1b.sort&&_27.length){
  114. if(_19.getValue){
  115. _27.sort(_4.createSortFunction([{attribute:_19.getLabelAttributes(_27[0])[0]}],_19));
  116. }else{
  117. var _28=this.labelAttr;
  118. _27.sort(function(a,b){
  119. return a[_28]>b[_28]?1:b[_28]>a[_28]?-1:0;
  120. });
  121. }
  122. }
  123. if(_1b.onFetch){
  124. _27=_1b.onFetch.call(this,_27,_1b);
  125. }
  126. _1.forEach(_27,function(i){
  127. this._addOptionForItem(i);
  128. },this);
  129. if(this._queryRes.observe){
  130. this._queryRes.observe(_9.hitch(this,function(_29,_2a,_2b){
  131. if(_2a==_2b){
  132. this._onSetItem(_29);
  133. }else{
  134. if(_2a!=-1){
  135. this._onDeleteItem(_29);
  136. }
  137. if(_2b!=-1){
  138. this._onNewItem(_29);
  139. }
  140. }
  141. }),true);
  142. }
  143. this._loadingStore=false;
  144. this.set("value","_pendingValue" in this?this._pendingValue:_1a);
  145. delete this._pendingValue;
  146. if(!this.loadChildrenOnOpen){
  147. this._loadChildren();
  148. }else{
  149. this._pseudoLoadChildren(_27);
  150. }
  151. this.onLoadDeferred.resolve(true);
  152. this.onSetStore();
  153. }),function(err){
  154. console.error("dijit.form.Select: "+err.toString());
  155. this.onLoadDeferred.reject(err);
  156. });
  157. }
  158. return _1c;
  159. },_setValueAttr:function(_2c,_2d){
  160. if(!this._onChangeActive){
  161. _2d=null;
  162. }
  163. if(this._loadingStore){
  164. this._pendingValue=_2c;
  165. return;
  166. }
  167. if(_2c==null){
  168. return;
  169. }
  170. if(_9.isArray(_2c)){
  171. _2c=_1.map(_2c,function(_2e){
  172. return _9.isObject(_2e)?_2e:{value:_2e};
  173. });
  174. }else{
  175. if(_9.isObject(_2c)){
  176. _2c=[_2c];
  177. }else{
  178. _2c=[{value:_2c}];
  179. }
  180. }
  181. _2c=_1.filter(this.getOptions(_2c),function(i){
  182. return i&&i.value;
  183. });
  184. var _2f=this.getOptions()||[];
  185. if(!this.multiple&&(!_2c[0]||!_2c[0].value)&&!!_2f.length){
  186. _2c[0]=_2f[0];
  187. }
  188. _1.forEach(_2f,function(opt){
  189. opt.selected=_1.some(_2c,function(v){
  190. return v.value===opt.value;
  191. });
  192. });
  193. var val=_1.map(_2c,function(opt){
  194. return opt.value;
  195. });
  196. if(typeof val=="undefined"||typeof val[0]=="undefined"){
  197. return;
  198. }
  199. var _30=_1.map(_2c,function(opt){
  200. return opt.label;
  201. });
  202. this._setDisplay(this.multiple?_30:_30[0]);
  203. this.inherited(arguments,[this.multiple?val:val[0],_2d]);
  204. this._updateSelection();
  205. },_getDisplayedValueAttr:function(){
  206. var ret=_1.map([].concat(this.get("selectedOptions")),function(v){
  207. if(v&&"label" in v){
  208. return v.label;
  209. }else{
  210. if(v){
  211. return v.value;
  212. }
  213. }
  214. return null;
  215. },this);
  216. return this.multiple?ret:ret[0];
  217. },_setDisplayedValueAttr:function(_31){
  218. this.set("value",this.getOptions(typeof _31=="string"?{label:_31}:_31));
  219. },_loadChildren:function(){
  220. if(this._loadingStore){
  221. return;
  222. }
  223. _1.forEach(this._getChildren(),function(_32){
  224. _32.destroyRecursive();
  225. });
  226. _1.forEach(this.options,this._addOptionItem,this);
  227. this._updateSelection();
  228. },_updateSelection:function(){
  229. this.focusedChild=null;
  230. this._set("value",this._getValueFromOpts());
  231. var val=[].concat(this.value);
  232. if(val&&val[0]){
  233. var _33=this;
  234. _1.forEach(this._getChildren(),function(_34){
  235. var _35=_1.some(val,function(v){
  236. return _34.option&&(v===_34.option.value);
  237. });
  238. if(_35&&!_33.multiple){
  239. _33.focusedChild=_34;
  240. }
  241. _7.toggle(_34.domNode,this.baseClass.replace(/\s+|$/g,"SelectedOption "),_35);
  242. _34.domNode.setAttribute("aria-selected",_35?"true":"false");
  243. },this);
  244. }
  245. },_getValueFromOpts:function(){
  246. var _36=this.getOptions()||[];
  247. if(!this.multiple&&_36.length){
  248. var opt=_1.filter(_36,function(i){
  249. return i.selected;
  250. })[0];
  251. if(opt&&opt.value){
  252. return opt.value;
  253. }else{
  254. _36[0].selected=true;
  255. return _36[0].value;
  256. }
  257. }else{
  258. if(this.multiple){
  259. return _1.map(_1.filter(_36,function(i){
  260. return i.selected;
  261. }),function(i){
  262. return i.value;
  263. })||[];
  264. }
  265. }
  266. return "";
  267. },_onNewItem:function(_37,_38){
  268. if(!_38||!_38.parent){
  269. this._addOptionForItem(_37);
  270. }
  271. },_onDeleteItem:function(_39){
  272. var _3a=this.store;
  273. this.removeOption({value:_3a.getIdentity(_39)});
  274. },_onSetItem:function(_3b){
  275. this.updateOption(this._getOptionObjForItem(_3b));
  276. },_getOptionObjForItem:function(_3c){
  277. var _3d=this.store,_3e=(this.labelAttr&&this.labelAttr in _3c)?_3c[this.labelAttr]:_3d.getLabel(_3c),_3f=(_3e?_3d.getIdentity(_3c):null);
  278. return {value:_3f,label:_3e,item:_3c};
  279. },_addOptionForItem:function(_40){
  280. var _41=this.store;
  281. if(_41.isItemLoaded&&!_41.isItemLoaded(_40)){
  282. _41.loadItem({item:_40,onItem:function(i){
  283. this._addOptionForItem(i);
  284. },scope:this});
  285. return;
  286. }
  287. var _42=this._getOptionObjForItem(_40);
  288. this.addOption(_42);
  289. },constructor:function(_43){
  290. this._oValue=(_43||{}).value||null;
  291. this._notifyConnections=[];
  292. },buildRendering:function(){
  293. this.inherited(arguments);
  294. _6.setSelectable(this.focusNode,false);
  295. },_fillContent:function(){
  296. if(!this.options){
  297. this.options=this.srcNodeRef?_a("> *",this.srcNodeRef).map(function(_44){
  298. if(_44.getAttribute("type")==="separator"){
  299. return {value:"",label:"",selected:false,disabled:false};
  300. }
  301. return {value:(_44.getAttribute("data-"+_8._scopeName+"-value")||_44.getAttribute("value")),label:String(_44.innerHTML),selected:_44.getAttribute("selected")||false,disabled:_44.getAttribute("disabled")||false};
  302. },this):[];
  303. }
  304. if(!this.value){
  305. this._set("value",this._getValueFromOpts());
  306. }else{
  307. if(this.multiple&&typeof this.value=="string"){
  308. this._set("value",this.value.split(","));
  309. }
  310. }
  311. },postCreate:function(){
  312. this.inherited(arguments);
  313. _3.after(this,"onChange",_9.hitch(this,"_updateSelection"));
  314. var _45=this.store;
  315. if(_45&&(_45.getIdentity||_45.getFeatures()["dojo.data.api.Identity"])){
  316. this.store=null;
  317. this.setStore(_45,this._oValue);
  318. }
  319. },startup:function(){
  320. this._loadChildren();
  321. this.inherited(arguments);
  322. },destroy:function(){
  323. var h;
  324. while((h=this._notifyConnections.pop())){
  325. h.remove();
  326. }
  327. if(this._queryRes&&this._queryRes.close){
  328. this._queryRes.close();
  329. }
  330. this.inherited(arguments);
  331. },_addOptionItem:function(){
  332. },_removeOptionItem:function(){
  333. },_setDisplay:function(){
  334. },_getChildren:function(){
  335. return [];
  336. },_getSelectedOptionsAttr:function(){
  337. return this.getOptions({selected:true});
  338. },_pseudoLoadChildren:function(){
  339. },onSetStore:function(){
  340. }});
  341. return _e;
  342. });