Vote.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. MWF.xApplication.ForumDocument = MWF.xApplication.ForumDocument || {};
  2. MWF.require("MWF.widget.O2Identity", null,false);
  3. MWF.xDesktop.requireApp("Template", "MPopupForm", null, false);
  4. MWF.xApplication.ForumDocument.Vote = new Class({
  5. Extends: MWF.widget.Common,
  6. Implements: [Options, Events],
  7. options: {
  8. "style": "default",
  9. "isNew" : false,
  10. "isEdited" : true
  11. },
  12. initialize : function( container, app, options, data ){
  13. this.setOptions(options);
  14. this.container = container;
  15. this.app = app;
  16. this.lp = app.lp;
  17. this.actions = app.restActions || app.action;
  18. this.data = data;
  19. this.userName = (layout.user && layout.user.distinguishedName) ? layout.user.distinguishedName :layout.desktop.session.user.distinguishedName;
  20. this.path = "/x_component_ForumDocument/$Vote/";
  21. this.cssPath = "/x_component_ForumDocument/$Vote/" + this.options.style + "/css.wcss";
  22. this._loadCss();
  23. },
  24. load: function(){
  25. this.voted = this.data.voted || false;
  26. if( !this.options.isNew && this.data.voteOptionGroupList ){
  27. this.sortData();
  28. }
  29. if( this.options.isNew || this.options.isEdited ){
  30. //this.loadContent_read()
  31. this.loadContent_edit();
  32. }else{
  33. this.loadContent_read();
  34. }
  35. },
  36. reload : function(){
  37. this.container.empty();
  38. this.getData( function(){
  39. this.load( true );
  40. }.bind(this))
  41. },
  42. getData : function( callback ){
  43. this.actions.getSubjectView( this.data.id , function( data ){
  44. this.data = data.data.currentSubject;
  45. if( callback )callback();
  46. }.bind(this))
  47. },
  48. sortData: function(){
  49. var groupList = this.data.voteOptionGroupList || [];
  50. groupList.sort( function( a, b ){
  51. return a.orderNumber - b.orderNumber;
  52. });
  53. for( var i=0; i<groupList.length;i++ ){
  54. var group = groupList[i];
  55. group.voteOptions.sort( function( a, b ){
  56. return a.orderNumber - b.orderNumber;
  57. })
  58. }
  59. },
  60. loadContent_read : function(){
  61. var overTime = false;
  62. var dateStr = "";
  63. if( this.data.voteLimitTime && this.data.voteLimitTime != "" ){
  64. var now = new Date();
  65. var end = new Date( this.data.voteLimitTime.replace(/-/g,"/") );
  66. if( now < end ){
  67. dateStr = ",距结束还有" + this.diffTime( now , end ) + "";
  68. }else{
  69. overTime = true;
  70. dateStr = ",投票已经结束"
  71. }
  72. }
  73. var personStr = "";
  74. if( this.data.voteUserCount || this.data.voteCount ){
  75. personStr = ",目前已有"+ this.data.voteUserCount || this.data.voteCount +"人参与"
  76. }else{
  77. personStr = ",目前还没有人参与"
  78. }
  79. var inforNode = new Element("div", {
  80. styles : this.css.inforNode,
  81. text : this.lp.vote
  82. }).inject(this.container);
  83. new Element("span", {
  84. styles : this.css.infor2Node,
  85. "text" : personStr + dateStr
  86. }).inject(inforNode);
  87. if( this.data.votePersonVisible && this.data.votePerson!=0 ){
  88. //var showPersonNode = new Element("span", {
  89. // styles : this.showPersonNode,
  90. // text : "查看投票参与人"
  91. //}).inject( inforNode );
  92. //showPersonNode.addEvents({
  93. // "click" : function(){ this.showVotePerson(); }.bind(this)
  94. //})
  95. }
  96. this.groupContainer = new Element("div", {
  97. styles : this.css.groupContainer
  98. }).inject( this.container );
  99. this.data.voteOptionGroupList.each( function( d , i){
  100. if( this.voted || overTime ){
  101. if( this.data.voteResultVisible || this.data.creatorName == this.userName){
  102. this.createGroupVoted(d, i);
  103. }
  104. }else{
  105. this.createGroupVoting(d, i);
  106. }
  107. }.bind(this));
  108. if( this.voted || overTime ){
  109. if( !this.data.voteResultVisible && this.data.creatorName != this.userName){
  110. new Element("div",{
  111. "styles" : { "margin-bottom" : "5px" },
  112. "text" : "此为不公开投票,只有发帖人能看到投票结果"
  113. }).inject( this.container );
  114. }
  115. }
  116. if( !this.voted ){
  117. if( !overTime ){
  118. var bottomContainer = new Element( "div").inject( this.container );
  119. var button = new Element( "input", {
  120. "type" : "button",
  121. styles : this.css.submitButton,
  122. value : "提交"
  123. }).inject( bottomContainer );
  124. button.addEvent("click", function(){
  125. this.submitVote();
  126. }.bind(this));
  127. button.addEvents({
  128. mouseover : function(){
  129. this.node.setStyles( this.obj.css.submitButton_over )
  130. }.bind({obj : this, node : button}),
  131. mouseout : function(){
  132. this.node.setStyles( this.obj.css.submitButton )
  133. }.bind({obj : this, node : button})
  134. });
  135. if( this.data.votePersonVisible ){
  136. new Element("span", {
  137. styles : { "margin-left" : "10px" },
  138. text : "(此为公开投票,其他人可以看到您的投票项目)"
  139. }).inject( bottomContainer )
  140. }else{
  141. new Element("span", {
  142. styles : { "margin-left" : "10px" },
  143. text : "(此为匿名投票,他人无法查看您的投票项目)"
  144. }).inject( bottomContainer )
  145. }
  146. }
  147. }else{
  148. new Element("span",{
  149. "text" : "您已经投过票,谢谢您的参与"
  150. }).inject( this.container );
  151. }
  152. },
  153. createGroupVoted : function(data, idx){
  154. var bgColor = this.getRandomColor();
  155. var maxWidth = "800";
  156. var sum = 0;
  157. var contentUsePicture = false;
  158. data.voteOptions.each( function(opt){
  159. sum += parseInt( opt.chooseCount );
  160. if( opt.optionContentType == "图片" ){
  161. contentUsePicture = true;
  162. }
  163. });
  164. if( sum!=0 ) {
  165. var groupTitle = new Element("div", {
  166. styles : this.css.groupTitle
  167. }).inject( this.groupContainer );
  168. var str = parseInt( data.voteChooseCount ) > 1 ? "多选(最多可选" + data.voteChooseCount + "项):" : "单选:";
  169. new Element( "span", { styles : this.css.groupSubject , text : str }).inject( groupTitle );
  170. new Element( "span", { styles : this.css.groupSubject , text : data.groupName } ).inject( groupTitle );
  171. var optionContainer = new Element("div", { styles : this.css.optionContainer }).inject( this.groupContainer );
  172. var optionTable;
  173. if( contentUsePicture ){
  174. if( layout.mobile ){
  175. optionTable = new Element("div").inject( optionContainer )
  176. }else {
  177. optionTable = new Element("table", {
  178. "cellSpacing": "0",
  179. "border": "0"
  180. }).inject(optionContainer);
  181. }
  182. var tr;
  183. data.voteOptions.each( function( n, i ){
  184. var optionsDiv;
  185. if( layout.mobile ){
  186. optionsDiv = new Element("div", {
  187. styles : this.css.optionsPictureDiv_mobile
  188. }).inject( optionTable );
  189. var area = new Element("div",{
  190. styles : this.css.optionPictureArea_mobile
  191. }).inject( optionsDiv );
  192. var img = new Element("img", {
  193. "src" : MWF.xDesktop.getImageSrc( n.optionPictureId ),
  194. "styles" : this.css.optionPicture_mobile
  195. }).inject( area );
  196. }else {
  197. if (i % 3 == 0) {
  198. tr = new Element("tr").inject(optionTable);
  199. }
  200. var td = new Element("td").inject(tr);
  201. optionsDiv = new Element("div", {
  202. styles: this.css.optionsPictureDiv
  203. }).inject(td);
  204. var area = new Element("div", {
  205. styles: this.css.optionPictureArea
  206. }).inject(optionsDiv);
  207. var img = new Element("img", {
  208. "src": MWF.xDesktop.getImageSrc(n.optionPictureId),
  209. "styles": this.css.optionPicture
  210. }).inject(area);
  211. img.addEvent("click", function () {
  212. window.open(MWF.xDesktop.getImageSrc(this.id), "_blank");
  213. }.bind({id: n.optionPictureId}));
  214. }
  215. var present = ( ( n.chooseCount / sum ) * 100 ).toString().substr(0, 5) + "%";
  216. var pre = new Element("div", {
  217. text: present,
  218. styles : this.css.presentDiv
  219. }).inject(optionsDiv);
  220. new Element("span", {
  221. styles: {color: "#ccc"},
  222. text: "(" + n.chooseCount + ")"
  223. }).inject(pre);
  224. var textNode = new Element("div", {
  225. text : (i+1)+"." + n.optionTextContent
  226. }).inject( optionsDiv );
  227. }.bind(this));
  228. }
  229. data.voteOptions.each( function( n, i ) {
  230. var optionText = new Element("div.optionText", {
  231. styles : {},
  232. text : (i+1)+". "+ n.optionTextContent
  233. }).inject( optionContainer );
  234. var resultContainer = new Element("div.resultContainer", {
  235. styles: this.css.optionItem
  236. }).inject(optionContainer);
  237. var result = new Element("div", {
  238. styles: this.css.optionItemBack
  239. }).inject(resultContainer);
  240. result.setStyle("width", layout.mobile ? "70%" : "85%");
  241. var width = Math.floor(( n.chooseCount / sum) * 100 );
  242. var front = new Element("div.optionItemFront", {
  243. styles: this.css.optionItemFront,
  244. text: " "
  245. }).inject( result );
  246. front.setStyles({"background-color": bgColor, width: width+"%"});
  247. if( this.data.votePersonVisible && !layout.mobile){
  248. front.setStyles({"cursor": "pointer"});
  249. front.addEvents({
  250. "click" : function(){
  251. this.obj.openLog( this.data );
  252. }.bind({ obj : this, data : n }),
  253. "mouseover" : function(){
  254. this.node.setStyle("opacity" , "0.8");
  255. }.bind({ node : front }),
  256. "mouseout" : function(){
  257. this.node.setStyle("opacity" , "1");
  258. }.bind({ node : front })
  259. })
  260. }
  261. var present = ( ( n.chooseCount / sum ) * 100 ).toString().substr(0, 5) + "%";
  262. var pre = new Element("div", {
  263. text: present,
  264. styles : this.css.presentNode
  265. }).inject(resultContainer);
  266. new Element("span", {
  267. styles: {color: "#ccc"},
  268. text: "(" + n.chooseCount + ")"
  269. }).inject(pre);
  270. if( n.voted ){
  271. new Element("div", {
  272. styles : this.css.checkedOption
  273. }).inject(resultContainer);
  274. }
  275. }.bind(this));
  276. }
  277. },
  278. createGroupVoting : function( data, idx ){
  279. this.groupObject = this.groupObject || {};
  280. this.groupObject[idx] = {};
  281. this.groupObject[idx].id = data.id;
  282. this.groupObject[idx].inputs = [];
  283. var groupTitle = new Element("div", {
  284. styles : this.css.groupTitle
  285. }).inject( this.groupContainer );
  286. var str = parseInt(data.voteChooseCount) > 1 ? "多选(最多可选" + data.voteChooseCount + "项):" : "单选:";
  287. new Element( "span", { styles : this.css.groupSubject , text : str }).inject( groupTitle );
  288. new Element( "span", { styles : this.css.groupSubject , text : data.groupName } ).inject( groupTitle );
  289. var contentUsePicture = false;
  290. for( var i=0; i<data.voteOptions.length; i++ ){
  291. if( data.voteOptions[i].optionContentType == "图片" ){
  292. contentUsePicture = true;
  293. break;
  294. }
  295. }
  296. var optionContainer = new Element("div", {
  297. styles : contentUsePicture ? this.css.optionPictureContainer : this.css.optionContainer
  298. }).inject( this.groupContainer );
  299. var optionTable;
  300. if( contentUsePicture ){
  301. if( layout.mobile ){
  302. optionTable = new Element("div").inject( optionContainer )
  303. }else{
  304. optionTable = new Element("table", {
  305. "cellSpacing" : "0",
  306. "border" : "0"
  307. }).inject( optionContainer )
  308. }
  309. }
  310. var tr;
  311. data.voteOptions.each( function( n, i ){
  312. var optionsDiv;
  313. if( contentUsePicture ){
  314. if( layout.mobile ){
  315. optionsDiv = new Element("div", {
  316. styles : this.css.optionsPictureDiv_mobile
  317. }).inject( optionTable );
  318. var area = new Element("div",{
  319. styles : this.css.optionPictureArea_mobile
  320. }).inject( optionsDiv );
  321. var img = new Element("img", {
  322. "src" : MWF.xDesktop.getImageSrc( n.optionPictureId ),
  323. "styles" : this.css.optionPicture_mobile
  324. }).inject( area );
  325. }else{
  326. if( i % 3 == 0 ){
  327. tr = new Element("tr").inject( optionTable );
  328. }
  329. var td = new Element("td").inject( tr );
  330. optionsDiv = new Element("div", {
  331. styles : this.css.optionsPictureDiv
  332. }).inject( td );
  333. var area = new Element("div",{
  334. styles : this.css.optionPictureArea
  335. }).inject( optionsDiv );
  336. var img = new Element("img", {
  337. "src" : MWF.xDesktop.getImageSrc( n.optionPictureId ),
  338. "styles" : this.css.optionPicture
  339. }).inject( area );
  340. img.addEvent("click",function(){
  341. window.open( MWF.xDesktop.getImageSrc( this.id ), "_blank" );
  342. }.bind({id: n.optionPictureId}))
  343. }
  344. }else{
  345. optionsDiv = new Element("div", {
  346. styles : this.css.optionsDiv
  347. }).inject( optionContainer );
  348. }
  349. var input = new Element("input", {
  350. type : data.voteChooseCount == 1 ? "radio" : "checkbox",
  351. name : "voteCheck_" + idx,
  352. value : n.id,
  353. styles : contentUsePicture ? this.css.optionsPictureInput : this.css.optionsInput
  354. }).inject( optionsDiv );
  355. var textNode = new Element("span", {
  356. text : (i+1)+"." + ( contentUsePicture ? "" : " ") + n.optionTextContent
  357. }).inject( optionsDiv );
  358. if( data.voteChooseCount > 1 ){
  359. input.addEvents({
  360. "click" : function( ev ){
  361. var inputs = this.obj.groupObject[this.groupIndex].inputs;
  362. this.obj.checkCountLimited( this.limit, inputs );
  363. }.bind( { obj : this, groupIndex : idx, optionIndex : i, limit : data.voteChooseCount } )
  364. });
  365. textNode.addEvents({
  366. "click" : function(ev){
  367. if( this.input.get("checked") ){
  368. this.input.set("checked", false);
  369. }else if( !this.input.get("disabled") ){
  370. this.input.set("checked", true );
  371. }
  372. var inputs = this.obj.groupObject[this.groupIndex].inputs;
  373. this.obj.checkCountLimited( this.limit, inputs );
  374. }.bind( { obj : this, groupIndex : idx, optionIndex : i, limit : data.voteChooseCount , input : input } )
  375. })
  376. }else{
  377. textNode.addEvents({
  378. "click" : function(ev){
  379. if( this.input.get("checked") ){
  380. return;
  381. }
  382. var inputs = this.obj.groupObject[this.groupIndex].inputs;
  383. inputs.each( function( input ){
  384. input.set("checked", false);
  385. });
  386. this.input.set("checked", true);
  387. }.bind( { obj : this, groupIndex : idx, optionIndex : i, input : input } )
  388. })
  389. }
  390. this.groupObject[idx].inputs.push( input );
  391. }.bind(this));
  392. },
  393. checkCountLimited : function( limit, inputs ){
  394. var checkedCount = 0;
  395. inputs.each( function( input ){
  396. if( input.get("checked") )checkedCount++;
  397. });
  398. if( checkedCount >= limit ){
  399. inputs.each( function( input ){
  400. if( !input.get("checked") )input.set("disabled", true);
  401. });
  402. }else{
  403. inputs.each( function( input ){
  404. if( !input.get("checked") )input.set("disabled", false);
  405. });
  406. }
  407. },
  408. loadContent_edit : function(){
  409. var html = "<table width='100%' bordr='0' cellpadding='5' cellspacing='0' style='margin-top:15px;'>" +
  410. "<tr>" +
  411. " <td styles='formTableTitleRight' lable='voteLimitTime' width='7%'></td>" +
  412. " <td styles='formTableValue' item='voteLimitTime' width='20%'></td>" +
  413. " <td styles='formTableTitleRight' lable='voteResultVisible' width='16%'></td>" +
  414. " <td styles='formTableValue' item='voteResultVisible' width='20%'></td>" +
  415. " <td styles='formTableTitleRight' lable='votePersonVisible' width='16%'></td>" +
  416. " <td styles='formTableValue' item='votePersonVisible' width='20%'></td>" +
  417. "</tr>"+
  418. "</table>";
  419. this.container.set("html", html);
  420. this.voteTable = this.container.getElement("table");
  421. MWF.xDesktop.requireApp("Template", "MForm", function () {
  422. this.form_vote = new MForm(this.container, this.data , {
  423. style: "forum",
  424. isEdited: true || this.isEdited || this.isNew,
  425. itemTemplate: {
  426. voteLimitTime: {text: this.lp.voteLimitTime, tType : "date" },
  427. voteResultVisible :{ text: this.lp.voteResultVisible, type : "select", selectText : this.lp.yesOrNo.split(","), selectValue : ["true","false"] },
  428. votePersonVisible : { text: this.lp.votePersonVisible, type : "select", selectText : this.lp.votePersonValue.split(","), selectValue : ["true","false"] }
  429. }
  430. }, this );
  431. this.form_vote.load();
  432. }.bind(this), true);
  433. this.addVoteGroupTr = new Element("tr").inject( this.voteTable );
  434. var td = new Element( "td", {colspan : 6}).inject( this.addVoteGroupTr );
  435. var input = new Element("input", { type : "button", styles : this.css.addVoteGroupNode , value : this.lp.addVoteGroup }).inject(td);
  436. input.addEvent("click", function(){
  437. this.createGroupEdited();
  438. }.bind(this));
  439. if( this.options.isNew || !this.data.voteOptionGroupList ){
  440. this.createGroupEdited()
  441. }else{
  442. this.data.voteOptionGroupList.each( function( d , i){
  443. this.createGroupEdited(d, i);
  444. }.bind(this));
  445. }
  446. },
  447. createGroupEdited : function( d, index ){
  448. this.groupObject = this.groupObject || {};
  449. this.groupIndex = this.groupIndex || 1;
  450. var data = d || {};
  451. var contentUsePicture = false;
  452. if( data.voteOptions ){
  453. for( var i=0; i<data.voteOptions.length; i++ ){
  454. if( data.voteOptions[i].optionContentType == "图片" ){
  455. contentUsePicture = true;
  456. break;
  457. }
  458. }
  459. }
  460. var tr = new Element("tr").inject( this.addVoteGroupTr, "before" );
  461. var td = new Element( "td", {colspan : 6}).inject( tr );
  462. var grid;
  463. MWF.xDesktop.requireApp("Template", "MGrid", function () {
  464. var vote_grid = new MGrid(td, data.voteOptions , {
  465. style: "forum",
  466. isEdited: true || this.isEdited || this.isNew,
  467. hasOperation : true,
  468. hasSequence : false,
  469. minTrCount : 2,
  470. tableAttributes : { width : "100%", border : "0" , cellpadding : "5", cellspacing : "0" },
  471. itemTemplate: {
  472. optionTextContent: { text: this.lp.option, defaultValue : this.lp.option, notEmpty : true, defaultValueAsEmpty : true, event : {
  473. focus : function( item, ev ){ if( item.getValue() == this.lp.option )item.setValue("") }.bind(this),
  474. blur : function( item, ev ){ if( item.getValue() == "" )item.setValue(this.lp.option) }.bind(this)
  475. }},
  476. optionPictureId : { type : "imageClipper",
  477. text : "图片",
  478. notEmpty : true,
  479. disable : ( !contentUsePicture ),
  480. style : {
  481. imageStyle : this.css.optionPicture,
  482. imageWrapStyle : this.css.optionPictureArea,
  483. actionStyle : this.css.uploadActionNode
  484. },
  485. aspectRatio : 0,
  486. ratioAdjustedEnable : true,
  487. reference : this.app.advanceId || this.app.data.id,
  488. referenceType: "forumDocument"
  489. }
  490. },
  491. onQueryCreateTr : function(){
  492. if( this.form ){
  493. if( this.form.getItem( "voteContentType").getValue() == "true"){
  494. this.itemTemplate["optionPictureId"].disable = false;
  495. }else{
  496. this.itemTemplate["optionPictureId"].disable = true;
  497. }
  498. }
  499. }
  500. }, this.app );
  501. vote_grid.setThTemplate("<tr><th style='text-align: left;font-size:14px;font-weight: normal;'></th><th button_add></th></tr>");
  502. vote_grid.setTrTemplate( "<tr><td><div item='optionTextContent' style='padding-top:10px'></div><div item='optionPictureId' style='padding-top: 5px;'></div></td><td button_remove style='vertical-align: top;padding-top:15px;'></td></tr>" );
  503. vote_grid.load();
  504. if( !d ){
  505. vote_grid.addTrs(3);
  506. }
  507. var th = vote_grid.tableHead.getElement("th");
  508. var html = "<div><span style='font-size:18px;margin-right:10px;' item='groupLabel' index='"+this.groupIndex+"'>"+this.lp.group+ (this.groupIndex) +":</span>"+
  509. "<span item='groupName' style='margin-right:15px;'></span>"+
  510. "<span lable='voteChooseCount' style='margin-right:5px;'></span><span item='voteChooseCount' style='margin-right:5px;'></span><span style='margin-right:10px;''>"+this.lp.opt+"</span>" +
  511. "<span item='voteContentType' style='margin-right:15px;'></span>"+
  512. "<span style='margin-right:15px;' item='removeVoteGroup'></span></div>"+
  513. "<div item='tipNode'></div>";
  514. th.set("html", html);
  515. var groupNameNode = th.getElement("[item='groupLabel']");
  516. var tipNode = th.getElement("[item='tipNode']");
  517. var vote_form = new MForm(th, data , {
  518. style: "forum",
  519. verifyType : "batch",
  520. isEdited: true || this.isEdited || this.isNew,
  521. itemTemplate: {
  522. groupName: { text: this.lp.voteSubject, defaultValue : this.lp.voteSubject, className : "inputTextNoWidth", style : { width : "500px" },
  523. notEmpty : true, defaultValueAsEmpty : true,
  524. event : {
  525. focus : function( item, ev ){ if( item.getValue() == this.lp.voteSubject )item.setValue("") }.bind(this),
  526. blur : function( item, ev ){ if( item.getValue() == "" )item.setValue(this.lp.voteSubject) }.bind(this)
  527. },
  528. onPostLoad : function(item){
  529. item.tipNode = tipNode;
  530. }
  531. },
  532. voteChooseCount : {text: this.lp.voteCountLimit, defaultValue : 1, className : "inputTextNoWidth", tType : "number", style : { width : "30px" } },
  533. voteContentType : { type : "checkbox", defaultValue : contentUsePicture ? "true" : "", selectValue: ["true"], selectText : ["上传图片"], event : {
  534. change : function( item, ev ){ this.obj.setPicture( item.getValue(), this.grid ) }.bind( { obj : this, grid : vote_grid} )
  535. }},
  536. removeVoteGroup : {
  537. disable : this.groupIndex == 1, type : "button", value : this.lp.removeVoteGroup, className : "removeVoteGroupNode", event : {
  538. click : function( item ,ev ){
  539. var _self = this;
  540. _self.obj.app.confirm("warn", ev, _self.obj.lp.confirmRemoveVoteGroupTitle, _self.obj.lp.confirmRemoveVoteGroupContent, 350, 120, function(){
  541. _self.obj.removeGroup( parseInt( _self.node.get("index")) );
  542. this.close();
  543. }, function(){
  544. this.close();
  545. });
  546. }.bind({ obj : this, node : groupNameNode })
  547. }
  548. }
  549. }
  550. }, this.app, this.css );
  551. vote_form.load();
  552. vote_grid.form = vote_form;
  553. this.groupObject[ this.groupIndex ] = {
  554. grid : vote_grid,
  555. form : vote_form,
  556. groupNameNode : groupNameNode
  557. };
  558. this.groupIndex++;
  559. }.bind(this), true);
  560. },
  561. removeGroup : function( key ){
  562. var voteObject = this.groupObject[ key ];
  563. var container = voteObject.grid.container;
  564. container.destroy();
  565. delete this.groupObject[ key ];
  566. for( var i=key; i<this.groupIndex-1; i++ ){
  567. this.groupObject[i] = this.groupObject[i+1];
  568. this.groupObject[i].groupNameNode.set("text",this.lp.group+ (i) +":" );
  569. this.groupObject[i].groupNameNode.set("index",i);
  570. }
  571. this.groupObject[this.groupIndex-1] = null;
  572. this.groupIndex --;
  573. },
  574. getVoteInfor : function(){
  575. var flag = true;
  576. var result = this.form_vote.getResult(true, ",", true, false, true);
  577. if( !result )flag = false;
  578. if(flag)result.optionGroups = [];
  579. for( var key in this.groupObject ){
  580. var obj = this.groupObject[key];
  581. if( obj ){
  582. var formResult = obj.form.getResult(true, ",", true, false, true);
  583. if( !formResult )flag = false;
  584. var gridResult = obj.grid.getResult(true, ",", true, false, true);
  585. if( !gridResult )flag = false;
  586. if( flag ){
  587. for( var i=0; i<gridResult.length;i++ ){
  588. gridResult[i].optionContentType = (formResult.voteContentType == "true") ? "图片" : "文字";
  589. }
  590. formResult.voteOptions = gridResult;
  591. result.optionGroups.push( formResult );
  592. }
  593. }
  594. }
  595. return flag ? result : null;
  596. },
  597. getVoteResult : function(){
  598. var flag = true;
  599. var result = {};
  600. result.id = this.data.id;
  601. result.optionGroups = [];
  602. for( var key in this.groupObject ){
  603. var g = {};
  604. g.selectedVoteOptionIds = [];
  605. var group = this.groupObject[key];
  606. g.id = group.id;
  607. for( var i=0;i<group.inputs.length; i++ ){
  608. var input = group.inputs[i];
  609. if( input.get("checked") ){
  610. g.selectedVoteOptionIds.push( input.get("value") );
  611. }
  612. }
  613. if( g.selectedVoteOptionIds.length == 0 ){
  614. flag = false;
  615. }
  616. result.optionGroups.push( g );
  617. }
  618. if( !flag ){
  619. //this.app.notice("请至少选择一组投票再提交","error");
  620. if( this.data.voteOptionGroupList.length >= 1 ){
  621. this.app.notice("请对每组选择至少一项再提交","error");
  622. }else{
  623. this.app.notice("请至少选择一项再提交","error");
  624. }
  625. return null;
  626. }else{
  627. return result;
  628. }
  629. },
  630. submitVote : function(){
  631. var data = this.getVoteResult();
  632. if(data){
  633. this.actions.submitVote( data, function(){
  634. this.reload();
  635. }.bind(this))
  636. }
  637. },
  638. setPicture: function( show, grid ){
  639. if( show == "true" ){
  640. grid.enableItem( "optionPictureId" )
  641. }else{
  642. grid.disableItem( "optionPictureId" );
  643. }
  644. },
  645. openLog : function( optionData ){
  646. var form = new MWF.xApplication.ForumDocument.VoteLog(this, this.data, {
  647. onPostOk : function( icon ){
  648. }.bind(this)
  649. });
  650. form.optionData = optionData;
  651. form.edit();
  652. },
  653. diffTime : function(startDate,endDate) {
  654. var diff=endDate.getTime() - startDate.getTime();//时间差的毫秒数
  655. //计算出相差天数
  656. var days=Math.floor(diff/(24*3600*1000));
  657. //计算出小时数
  658. var leave1=diff%(24*3600*1000); //计算天数后剩余的毫秒数
  659. var hours=Math.floor(leave1/(3600*1000));
  660. //计算相差分钟数
  661. var leave2=leave1%(3600*1000); //计算小时数后剩余的毫秒数
  662. var minutes=Math.floor(leave2/(60*1000));
  663. //计算相差秒数
  664. var leave3=leave2%(60*1000); //计算分钟数后剩余的毫秒数
  665. var seconds=Math.round(leave3/1000);
  666. var returnStr = seconds + "秒";
  667. if(minutes>0) {
  668. returnStr = minutes + "分" + returnStr;
  669. }
  670. if(hours>0) {
  671. returnStr = hours + "小时" + returnStr;
  672. }
  673. if(days>0) {
  674. returnStr = days + "天" + returnStr;
  675. }
  676. return returnStr;
  677. },
  678. getRandomColor : function(){
  679. //return "hsb(" + Math.random() + ", 1, 1)";
  680. //return '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
  681. var flag = false;
  682. do{
  683. var color = ('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
  684. var r = parseInt( color.substr( 0, 2 ) , 16);
  685. var g = parseInt( color.substr( 3, 2 ) , 16);
  686. var b = parseInt( color.substr( 5, 2 ) , 16);
  687. if( r > 0xf0 && g>0xf0 && b>0xf0 ){
  688. flag = true
  689. }
  690. }
  691. while( flag );
  692. return '#'+color;
  693. }
  694. });
  695. MWF.xApplication.ForumDocument.VoteLog = new Class({
  696. Extends: MPopupForm,
  697. Implements: [Options, Events],
  698. options: {
  699. "style": "default",
  700. "width": "750",
  701. "height": "600",
  702. "hasTop": true,
  703. "hasIcon": false,
  704. "hasTopIcon" : true,
  705. "hasTopContent" : true,
  706. "hasBottom": false,
  707. "title": "查看投票参与人",
  708. "draggable": true,
  709. "closeAction": true,
  710. "closeByClickMask" : true
  711. },
  712. _createTableContent: function () {
  713. var html = "<table width='95%' bordr='0' cellpadding='5' cellspacing='0' styles='formTable'>" +
  714. "<tr><td styles='formTableTitle' lable='group' width='15%'></td>" +
  715. " <td styles='formTableValue' item='group' width='85%'></td></tr>" +
  716. "<tr><td styles='formTableTitle' lable='option'></td>" +
  717. " <td styles='formTableValue' item='option'></td></tr>" +
  718. "<tr><td styles='formTableTitle'></td>" +
  719. " <td styles='formTableValue'><div item='logArea' styles='logArea'></div></td>" +
  720. " </tr>" +
  721. "</table>";
  722. this.formTableArea.set("html", html);
  723. this.logArea = this.formTableArea.getElement("[item='logArea']");
  724. var groupNames = [], groupIds = [], optionNames = [], optionIds = [], currentGroup, currentOption;
  725. this.data.voteOptionGroupList.each( function( d, i ){
  726. var flag = false;
  727. groupNames.push( "组"+ (i+1) + ":" + d.groupName );
  728. groupIds.push( d.id );
  729. if( this.optionData ){
  730. if ( this.optionData.optionGroupId == d.id ){
  731. flag = true;
  732. }
  733. }else if( i == 0 ){
  734. flag = true;
  735. }
  736. if( flag ){
  737. currentGroup = d.id;
  738. d.voteOptions.each( function( opt, j ){
  739. optionNames.push( opt.optionTextContent );
  740. optionIds.push( opt.id );
  741. if( this.optionData && this.optionData.id == opt.id ){
  742. currentOption = opt.id
  743. }else if( j == 0 ){
  744. currentOption = opt.id
  745. }
  746. }.bind(this))
  747. }
  748. }.bind(this));
  749. MWF.xDesktop.requireApp("Template", "MForm", function () {
  750. var form = new MForm(this.formTableArea, this.data , {
  751. style: "forum",
  752. isEdited: true || this.isEdited || this.isNew,
  753. itemTemplate: {
  754. group :{ type : "select",
  755. text : "组别:",
  756. selectText : groupNames,
  757. selectValue : groupIds,
  758. defaultValue : currentGroup,
  759. event : { change : function( item, ev ){
  760. var groupId = item.getValue();
  761. var opts = [], ids = [];
  762. this.data.voteOptionGroupList.each( function( d, i ){
  763. if( groupId == d.id ){
  764. d.voteOptions.each( function( opt, j ){
  765. opts.push( opt.optionTextContent );
  766. ids.push( opt.id );
  767. if( j == 0 )currentOption = opt.id;
  768. })
  769. }
  770. }.bind(this));
  771. item.form.getItem("option").resetItemOptions( ids, opts );
  772. this.showLog( currentOption )
  773. }.bind(this)}
  774. },
  775. option :{ type : "select",
  776. text : "选项:",
  777. selectText : optionNames,
  778. selectValue : optionIds,
  779. defaultValue : currentOption,
  780. event : { change : function( item, ev ){
  781. this.showLog( item.getValue() );
  782. }.bind(this)}
  783. }
  784. }
  785. }, this, this.css );
  786. form.load();
  787. }.bind(this), true);
  788. this.showLog( currentOption );
  789. },
  790. showLog : function( optId ){
  791. this.logArea.empty();
  792. this.recordView = new MWF.xApplication.ForumDocument.VoteRecordView( this.logArea, this.app, this, {
  793. templateUrl : this.explorer.path + "listItemVote.json",
  794. scrollEnable : true
  795. } );
  796. this.recordView.filterData = {
  797. subjectId : this.data.id,
  798. voteOptionId : optId
  799. };
  800. this.recordView.load();
  801. }
  802. });
  803. MWF.xApplication.ForumDocument.VoteRecordView = new Class({
  804. Extends: MWF.xApplication.Template.Explorer.ComplexView,
  805. _createDocument: function(data, index){
  806. data.index = index;
  807. return new MWF.xApplication.ForumDocument.VoteRecordDocument(this.viewNode, data, this.explorer, this, null, data.index );
  808. },
  809. _getCurrentPageData: function(callback, count, pageNum){
  810. this.clearBody();
  811. if(!count)count=50;
  812. if(!pageNum)pageNum = 1;
  813. if( pageNum == 1 ){
  814. }else{
  815. }
  816. var filter = this.filterData || {};
  817. this.actions.listVoteRecord( pageNum, count, filter, function(json){
  818. if( !json.data )json.data = [];
  819. if( !json.count )json.count=0;
  820. if( callback )callback(json);
  821. }.bind(this))
  822. },
  823. _removeDocument: function(documentData, all){
  824. },
  825. _create: function(){
  826. },
  827. _queryCreateViewNode: function(){
  828. },
  829. _postCreateViewNode: function( viewNode ){
  830. },
  831. _queryCreateViewHead:function(){
  832. },
  833. _postCreateViewHead: function( headNode ){
  834. }
  835. });
  836. MWF.xApplication.ForumDocument.VoteRecordDocument = new Class({
  837. Extends: MWF.xApplication.Template.Explorer.ComplexDocument,
  838. _queryCreateDocumentNode:function( itemData ){
  839. },
  840. _postCreateDocumentNode: function( itemNode, itemData ){
  841. if (itemData.votorName) new MWF.widget.O2Person({"name" : itemData.votorName }, itemNode, {"style": "xform"});
  842. }
  843. });