Vote.js 39 KB

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