Readerfield.js 21 KB

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