Readerfield.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. MWF.xDesktop.requireApp("cms.Xform", "Personfield", null, false);
  2. MWF.require("MWF.widget.O2Identity", null,false);
  3. MWF.xDesktop.requireApp("Selector", "package", null, false);
  4. MWF.xApplication.cms.Xform.Readerfield = MWF.CMSReaderfield = new Class({
  5. Extends: MWF.CMSPersonfield,
  6. iconStyle: "readerfieldIcon",
  7. loadDescription: function(){
  8. var v = this._getBusinessData();
  9. if (!v || !v.length){
  10. if (this.json.description){
  11. var size = this.node.getFirst().getSize();
  12. var w = size.x-3;
  13. if (COMMON.Browser.safari) w = w-20;
  14. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  15. this.descriptionNode.setStyles({
  16. "width": ""+w+"px",
  17. "height": ""+size.y+"px",
  18. "line-height": ""+size.y+"px"
  19. });
  20. this.setDescriptionEvent();
  21. }
  22. }
  23. },
  24. setDescriptionEvent: function(){
  25. if (this.descriptionNode){
  26. this.descriptionNode.addEvents({
  27. "mousedown": function(){
  28. this.descriptionNode.setStyle("display", "none");
  29. this.clickSelect();
  30. }.bind(this)
  31. });
  32. }
  33. },
  34. _loadUserInterface: function(){
  35. this.field = true;
  36. this._loadNode();
  37. if (this.json.compute === "show"){
  38. this._setValue(this._computeValue());
  39. }else{
  40. this._loadValue();
  41. }
  42. },
  43. _loadNode: function(){
  44. if (this.readonly || this.json.isReadonly){
  45. this._loadNodeRead();
  46. }else{
  47. this._getOrgOptions();
  48. if (this.json.isInput){
  49. this._loadNodeInputEdit();
  50. }else{
  51. this._loadNodeEdit();
  52. }
  53. }
  54. },
  55. _getOrgOptions: function(){
  56. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  57. if( selectType.contains("unit") || selectType.contains("identity")){
  58. this.selectUnits = this.getSelectRange();
  59. if (this.json.range!=="all"){
  60. if (!this.selectUnits.length){
  61. this.form.notice(MWF.xApplication.cms.Xform.LP.noSelectRange, "error", this.node);
  62. return false;
  63. }
  64. }
  65. }else{
  66. this.selectUnits = [];
  67. }
  68. },
  69. _loadNodeRead: function(){
  70. this.node.empty();
  71. this.node.setStyle("overflow" , "hidden");
  72. var node = new Element("div").inject(this.node);
  73. },
  74. _searchConfirmPerson: function(item){
  75. var inforNode = item.inforNode || new Element("div");
  76. if (item.data){
  77. var text = "";
  78. var flag = item.data.distinguishedName.substr(item.data.distinguishedName.length-1, 1);
  79. switch (flag.toLowerCase()){
  80. case "i":
  81. text = item.data.name+"("+item.data.unitName+")";
  82. break;
  83. case "p":
  84. text = item.data.name+(item.data.employee ? "("+item.data.employee+")" : "");
  85. break;
  86. case "u":
  87. text = item.data.levelName;
  88. break;
  89. case "g":
  90. text = item.data.name;
  91. break;
  92. default:
  93. text = item.data.name;
  94. }
  95. inforNode.set({
  96. "styles": {"font-size": "14px", "color": ""},
  97. "text": text
  98. });
  99. }else{
  100. inforNode.set({
  101. "styles": {"font-size": "14px", "color": "#bd0000"},
  102. "text": MWF.xApplication.process.Xform.LP.noOrgObject
  103. });
  104. }
  105. if (!item.inforNode){
  106. new mBox.Tooltip({
  107. content: inforNode,
  108. setStyles: {content: {padding: 15, lineHeight: 20}},
  109. attach: item.node,
  110. transition: 'flyin'
  111. });
  112. item.inforNode = inforNode;
  113. }
  114. },
  115. _searchOptions: function(value, callback){
  116. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  117. var options = {
  118. "type" : "",
  119. "types": selectType,
  120. "units": this.selectUnits, //范围
  121. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType
  122. };
  123. if (!this.comboxFilter) this.comboxFilter = new MWF.O2SelectorFilter(value, options);
  124. this.comboxFilter.filter(value, function(data){
  125. data.map(function(d){
  126. var value = Object.clone(d);
  127. d.value = value;
  128. var flag = d.distinguishedName.substr(d.distinguishedName.length-1, 1);
  129. switch (flag.toLowerCase()){
  130. case "i":
  131. d.text = d.name+"("+d.unitName+")";
  132. break;
  133. case "p":
  134. d.text = d.name+(d.employee ? "("+d.employee+")" : "");
  135. break;
  136. case "u":
  137. d.text = d.name;
  138. break;
  139. case "g":
  140. d.text = d.name;
  141. break;
  142. default:
  143. d.text = d.name;
  144. }
  145. });
  146. if (callback) callback(data);
  147. });
  148. },
  149. _loadNodeInputEdit: function(){
  150. var input=null;
  151. MWF.require("MWF.widget.Combox", function(){
  152. this.combox = input = new MWF.widget.Combox({
  153. "count": this.json.count || 0,
  154. "splitShow": this.json.splitShow || ", ",
  155. "onCommitInput": function(item){
  156. this._searchConfirmPerson(item);
  157. //this.fireEvent("change");
  158. }.bind(this),
  159. "onChange": function(){
  160. this._setBusinessData(this.getInputData());
  161. this.fireEvent("change");
  162. }.bind(this),
  163. "optionsMethod": this._searchOptions.bind(this)
  164. });
  165. }.bind(this), false);
  166. input.setStyles({
  167. "background": "transparent",
  168. "border": "0px"
  169. });
  170. input.set(this.json.properties);
  171. var node = new Element("div", {"styles": {
  172. "overflow": "hidden",
  173. //"position": "relative",
  174. "margin-right": "20px"
  175. }}).inject(this.node, "after");
  176. input.inject(node);
  177. //this.combox = input;
  178. this.node.destroy();
  179. this.node = node;
  180. this.node.set({
  181. "id": this.json.id,
  182. "MWFType": this.json.type
  183. });
  184. if (this.json.showIcon!='no') this.iconNode = new Element("div", {
  185. "styles": this.form.css[this.iconStyle],
  186. "events": {
  187. "click": this.clickSelect.bind(this)
  188. }
  189. }).inject(this.node, "before");
  190. this.combox.addEvent("change", function(){
  191. this.validationMode();
  192. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  193. }.bind(this));
  194. },
  195. _loadNodeEdit : function(){
  196. var input = this.input = new Element("div", {
  197. "styles": {
  198. "background": "transparent",
  199. "border": "0px",
  200. "min-height": "20px"
  201. }
  202. });
  203. input.set(this.json.properties);
  204. var node = new Element("div", {"styles": {
  205. "overflow": "hidden",
  206. "position": "relative",
  207. "min-height" : "20px",
  208. "margin-right": "20px"
  209. }}).inject(this.node, "after");
  210. input.inject(node);
  211. this.node.destroy();
  212. this.node = node;
  213. this.node.set({
  214. "id": this.json.id,
  215. "MWFType": this.json.type,
  216. "readonly": true,
  217. "title" : MWF.xApplication.cms.Xform.LP.readerFieldNotice
  218. });
  219. if( !this.readonly ) {
  220. this.node.setStyle("cursor" , "pointer");
  221. this.node.addEvents({
  222. "click": this.clickSelect.bind(this)
  223. });
  224. if (this.json.showIcon!='no')this.iconNode = new Element("div", { //this.form.css[this.iconStyle],
  225. "styles": {
  226. "background": "url("+"/x_component_cms_Xform/$Form/default/icon/selectreader.png) center center no-repeat",
  227. "width": "18px",
  228. "height": "18px",
  229. "float": "right"
  230. }
  231. }).inject(this.node, "before");
  232. if (this.iconNode){
  233. this.iconNode.setStyle("cursor" , "pointer");
  234. this.iconNode.addEvents({
  235. "click": this.clickSelect.bind(this)
  236. });
  237. }
  238. }
  239. },
  240. getData: function(when){
  241. if (this.json.compute == "save") this._setValue(this._computeValue());
  242. return this._getBusinessData();
  243. },
  244. getInputData: function(){
  245. if (this.json.isInput){
  246. if (this.combox) return this.combox.getData();
  247. return this._getBusinessData();
  248. }else{
  249. return this._getBusinessData();
  250. }
  251. },
  252. addData: function(value){
  253. if (!value) return false;
  254. value.each(function(v){
  255. var vtype = typeOf(v);
  256. if (vtype==="string" ){
  257. var data;
  258. if( this.json.valueType === "string" ){
  259. this.combox.addNewValue(v.split("@")[0], v);
  260. }else{
  261. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), null, v, false);
  262. if (data) this.combox.addNewValue(this.getDataText(data), data);
  263. }
  264. }
  265. if (vtype==="object"){
  266. this.combox.addNewValue(this.getDataText(v), v);
  267. }
  268. }.bind(this));
  269. },
  270. setData: function(data) {
  271. this._setValue(data);
  272. },
  273. _computeValue: function(){
  274. var values = [];
  275. if (this.json.identityValue) {
  276. this.json.identityValue.each(function(v){ if (v) values.push(v)});
  277. }
  278. if (this.json.unitValue) {
  279. this.json.unitValue.each(function(v){ if (v) values.push(v)});
  280. }
  281. if (this.json.defaultValue && this.json.defaultValue.code){
  282. var fd = this.form.Macro.exec(this.json.defaultValue.code, this);
  283. if (typeOf(fd)!=="array") fd = (fd) ? [fd] : [];
  284. fd.each(function(fdd){
  285. if (fdd){
  286. if (typeOf(fdd)==="string" && this.json.valueType !== "string"){
  287. var data;
  288. this.getOrgAction()[this.getValueMethod(fdd)](function(json){ data = json.data }.bind(this), null, fdd, false);
  289. values.push(data);
  290. }else{
  291. values.push(fdd);
  292. }
  293. }
  294. }.bind(this));
  295. }
  296. if (this.json.count>0){
  297. return values.slice(0, this.json.count);
  298. }
  299. return values;
  300. //return (this.json.defaultValue.code) ? this.form.Macro.exec(this.json.defaultValue.code, this): (value || "");
  301. },
  302. _getBusinessData: function(){
  303. if (this.json.section=="yes"){
  304. var data = this._getBusinessSectionData();
  305. }else {
  306. var data = this.form.businessData.data[this.json.id] || "";
  307. }
  308. if (typeOf( data ) != "array"){
  309. if (data) return [data];
  310. return [];
  311. }else{
  312. return data;
  313. }
  314. },
  315. creteShowNode: function(data, islast){
  316. var nodeText = (data.text) ? data.text : data;
  317. if (!islast) nodeText = nodeText + (this.json.splitStr || ", ");
  318. var node = new Element("div", {
  319. "styles": {
  320. "float": "left",
  321. "margin-right": "5px"
  322. },
  323. "text": nodeText
  324. });
  325. var text = "";
  326. if (data.value){
  327. var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-1, 1);
  328. switch (flag.toLowerCase()){
  329. case "i":
  330. text = data.value.name+"("+data.value.unitName+")";
  331. break;
  332. case "p":
  333. text = data.value.name+ ( +data.value.employee ? "("+data.value.employee+")" : "");
  334. break;
  335. case "u":
  336. text = data.value.levelName;
  337. break;
  338. case "g":
  339. text = data.value.name;
  340. break;
  341. default:
  342. text = data.value.name;
  343. }
  344. var inforNode = new Element("div").set({
  345. "styles": {"font-size": "14px", "color": ""},
  346. "text": text
  347. });
  348. new mBox.Tooltip({
  349. content: inforNode,
  350. setStyles: {content: {padding: 15, lineHeight: 20}},
  351. attach: node,
  352. transition: 'flyin'
  353. });
  354. }
  355. return node;
  356. },
  357. _setValue: function(value){
  358. if (value.length==1 && !(value[0])) value=[];
  359. var values = [];
  360. var comboxValues = [];
  361. var type = typeOf(value);
  362. if (type==="array"){
  363. value.each(function(v){
  364. var data=null;
  365. var vtype = typeOf(v);
  366. if (vtype==="string" ){
  367. if( this.json.valueType === "string" ){
  368. data = v;
  369. values.push(data);
  370. comboxValues.push({"text": data.split("@")[0],"value": data});
  371. }else{
  372. var error = (this.json.isInput) ? function(){ comboxValues.push(v); } : null;
  373. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), error, v, false);
  374. values.push(data);
  375. comboxValues.push({"text": this.getDataText(data),"value": data});
  376. }
  377. }
  378. if (vtype==="object"){
  379. data = v;
  380. values.push(data);
  381. comboxValues.push({"text": this.getDataText(data),"value": data});
  382. }
  383. //if (data){
  384. // values.push(data);
  385. // comboxValues.push({"text": this.getDataText(data),"value": data});
  386. //}
  387. }.bind(this));
  388. }
  389. if (type==="string"){
  390. if( this.json.valueType === "string" ){
  391. values.push(value);
  392. comboxValues.push({"text": value.split("@")[0],"value": value});
  393. }else{
  394. var vData;
  395. var error = (this.json.isInput) ? function(){ comboxValues.push(value); } : null;
  396. this.getOrgAction()[this.getValueMethod(value)](function(json){ vData = json.data }.bind(this), error, value, false);
  397. if (vData){
  398. values.push(vData);
  399. comboxValues.push({"text": this.getDataText(vData),"value": vData});
  400. }
  401. }
  402. }
  403. if (type==="object"){
  404. values.push(value);
  405. comboxValues.push({"text": this.getDataText(value),"value": value});
  406. }
  407. this._setBusinessData(value);
  408. if (this.json.isInput) {
  409. if (this.combox) {
  410. this.combox.addNewValues(comboxValues);
  411. } else {
  412. var node = this.node.getFirst();
  413. if (node) {
  414. comboxValues.each(function (v, i) {
  415. this.creteShowNode(v, (i === comboxValues.length - 1)).inject(node);
  416. }.bind(this));
  417. }
  418. }
  419. }else if( this.json.valueType==="string" ){
  420. var input = this.node.getFirst();
  421. if (!input) {
  422. input = this.node;
  423. }
  424. input.empty();
  425. var textList = [];
  426. values.each( function( v ){
  427. if( typeOf(v) === "string" )textList.push( v.split("@")[0] );
  428. if( typeOf(v) === "object" )textList.push(v.distinguishedName.split("@")[0] );
  429. });
  430. input.set("text", textList.join(", "))
  431. }else {
  432. var input = this.node.getFirst();
  433. if (!input) {
  434. input = this.node;
  435. }
  436. input.empty();
  437. this.loadOrgWidget(values, input);
  438. }
  439. },
  440. loadOrgWidget: function(value, node){
  441. var options = {"style": "xform", "canRemove":!this.readonly , "onRemove" : this.removeItem};
  442. value.each(function(data){
  443. if( data.distinguishedName ){
  444. var flag = data.distinguishedName.substr(data.distinguishedName.length-1, 1);
  445. switch (flag.toLowerCase()){
  446. case "i":
  447. var widget = new MWF.widget.O2Identity(data, node, options );
  448. break;
  449. case "p":
  450. var widget = new MWF.widget.O2Person(data, node, options);
  451. break;
  452. case "u":
  453. var widget = new MWF.widget.O2Unit(data, node, options);
  454. break;
  455. case "g":
  456. var widget = new MWF.widget.O2Group(data, node, options);
  457. break;
  458. default:
  459. var widget = new MWF.widget.O2Other(data, node, options);
  460. }
  461. widget.field = this;
  462. if( layout.mobile ){
  463. widget.node.setStyles({
  464. "float" : "none"
  465. })
  466. }
  467. }
  468. }.bind(this));
  469. },
  470. removeItem : function( widget, ev ){
  471. //this 是 MWF.widget.O2Identity 之类的对象
  472. var _self = this.field; //这个才是Readerfield
  473. var dn = this.data.distinguishedName;
  474. var data = _self._getBusinessData();
  475. var index;
  476. data.each( function ( d , i){
  477. if( d.distinguishedName == dn ){
  478. index = i
  479. }
  480. });
  481. data.splice( index, 1 );
  482. _self._setBusinessData( data );
  483. this.node.destroy();
  484. ev.stopPropagation();
  485. },
  486. getValue: function(){
  487. var value = this._getBusinessData();
  488. if( typeOf( value ) === "array" ){
  489. if( value.length === 0 )value = this._computeValue();
  490. }else if (!value){
  491. value = this._computeValue();
  492. }
  493. return value || "";
  494. },
  495. _loadValue: function(){
  496. this._setValue(this.getValue());
  497. },
  498. clickSelect: function(){
  499. this.validationMode();
  500. var count = (this.json.count) ? this.json.count : 0;
  501. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  502. //if( selectType.contains("unit") || selectType.contains("identity")){
  503. // var selectUnits = this.getSelectRange();
  504. // if (this.json.range!=="all"){
  505. // if (!selectUnits.length){
  506. // this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
  507. // return false;
  508. // }
  509. // }
  510. //}else{
  511. // var selectUnits = [];
  512. //}
  513. var selectUnits = this.selectUnits;
  514. if( !selectType[0] ){
  515. this.form.notice(MWF.xApplication.process.Xform.LP.noSelectType, "error", this.node);
  516. return false;
  517. }
  518. var exclude = [];
  519. if( this.json.exclude ){
  520. var v = this.form.Macro.exec(this.json.exclude.code, this);
  521. exclude = typeOf(v)==="array" ? v : [v];
  522. }
  523. var options = {
  524. "type" : "",
  525. "types": selectType,
  526. "values" : (this.json.isInput) ? [] : this._getBusinessData(),
  527. "count": count,
  528. "units": selectUnits, //范围
  529. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
  530. "exclude" : exclude,
  531. "expandSubEnable" : (this.json.expandSubEnable=="no") ? false : true,
  532. //"expand" : false,
  533. "onComplete": function(items, itemsObject){
  534. var values = [];
  535. items.each( function(it){
  536. values.push(MWF.org.parseOrgData(it.data));
  537. });
  538. if (this.json.isInput){
  539. this.addData(values);
  540. }else{
  541. this.setData(values);
  542. }
  543. //this.setData( values );
  544. this.validation()
  545. }.bind(this),
  546. "onCancel": function(){
  547. this.validation();
  548. }.bind(this),
  549. "onLoad": function(){
  550. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  551. }.bind(this),
  552. "onClose": function(){
  553. //var v = this.node.getFirst().get("value");
  554. var v = this.getInputData();
  555. if (!v || !v.length) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  556. }.bind(this)
  557. };
  558. var selector = new MWF.O2Selector(this.form.app.content, options);
  559. },
  560. _loadStyles: function(){
  561. if (this.readonly || this.json.isReadonly){
  562. if (this.json.styles) this.node.setStyles(this.json.styles);
  563. }else{
  564. if (this.json.styles) this.node.setStyles(this.json.styles);
  565. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  566. if (this.iconNode){
  567. var size = this.node.getSize();
  568. this.iconNode.setStyle("height", ""+size.y+"px");
  569. }
  570. }
  571. }
  572. });