Readerfield.js 20 KB

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