Readerfield.js 20 KB

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