Readerfield.js 21 KB

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