1/* jshint browser: true */ 2/* global wp, bp, BP_Nouveau, _, Backbone, bbPollsVars, BBTopicsManager */ 3window.wp = window.wp || {}; 4window.bp = window.bp || {}; 5 6( function ( exports, $ ) { 7 8 // Bail if not set. 9 if ( 'undefined' === typeof BP_Nouveau ) { 10 return; 11 } 12 13 _.extend( bp, _.pick( wp, 'Backbone', 'ajax', 'template' ) ); 14 15 bp.Models = bp.Models || {}; 16 bp.Collections = bp.Collections || {}; 17 bp.Views = bp.Views || {}; 18 19 bp.Nouveau = bp.Nouveau || {}; 20 21 /** 22 * [Poll description] 23 * 24 * @type {Object} 25 */ 26 bp.Nouveau.Poll = { 27 /** 28 * [start description] 29 * 30 * @return {[type]} [description] 31 */ 32 start: function () { 33 this.setupGlobals(); 34 this.addListeners(); 35 }, 36 37 /** 38 * [setupGlobals description] 39 * 40 * @return {[type]} [description] 41 */ 42 setupGlobals: function () { 43 this.pollFormInitialized = false; 44 }, 45 46 /** 47 * [addListeners description] 48 */ 49 addListeners: function () { 50 $( '#bp-nouveau-activity-form, #bb-rl-activity-form' ).on( 'click', '.post-elements-buttons-item.post-poll', this.showPollForm.bind( this ) ); 51 $( '#bp-nouveau-activity-form, #bb-rl-activity-form' ).on( 'click', '.bb-activity-poll-cancel, .bb-activity-poll_modal .bb-model-close-button', this.hidePollForm.bind( this ) ); 52 $( document ).on( 'keyup', '.bb-activity-poll-new-option-input', this.validateNewOption.bind( this ) ); 53 $( document ).on( 'click', '.bb-activity-option-submit', this.submitNewOption.bind( this ) ); 54 $( document ).on( 'click', '.bb-activity-poll-see-more-link', this.seeMoreOptions.bind( this ) ); 55 $( document ).on( 'click', '.bb-poll-option-view-state', this.showPollState.bind( this ) ); 56 $( document ).on( 'click', '.bb-poll-option_remove', this.removePollOption.bind( this ) ); 57 $( document ).on( 'click', '.bb-activity-poll-option input[type="radio"]', this.addPollVote.bind( this ) ); 58 $( document ).on( 'change', '.bb-activity-poll-option input[type="checkbox"]', this.addPollVote.bind( this ) ); 59 $( document ).on( 'click', '.bb-close-action-popup, .bb-activity-poll-state_overlay', this.closeVoteState.bind( this ) ); 60 61 }, 62 63 showPollForm: function () { 64 if ( $.fn.sortable && ! $( '.bb-poll-question_options' ).hasClass( 'ui-sortable' ) ) { 65 $( '.bb-poll-question_options' ).sortable( 66 { 67 placeholder: 'sortable-placeholder', 68 update : function () { 69 reIndexOptions(); 70 }, 71 cancel : 'input, .bb-poll-edit-option_remove' // Prevent dragging when interacting with inputs or the "x" icon. 72 } 73 ); 74 } 75 76 if ( ! this.pollFormInitialized ) { 77 // Handle adding new option. 78 $( document ).on( 79 'click', 80 '.bb-poll-option_add', 81 function ( e ) { 82 e.preventDefault(); 83 var $this = $( this ); 84 $this.attr( 'disabled', 'disabled' ); 85 86 var $popupContent = $this.closest( '.bb-action-popup-content' ); 87 var customIndex = $popupContent.find( '.bb-poll_option_draggable:not(.custom)' ).length; 88 var optLength = customIndex; 89 90 // Max 10 options are allowed. 91 if ( customIndex >= 10 ) { 92 return; 93 } 94 95 // Clone the first existing option element. 96 var $newOptionClone = $popupContent.find( '.bb-poll_option_draggable:first' ).clone(); 97 98 // Clear the value of the cloned input field. 99 $newOptionClone.find( '.bb-poll-question_option' ).val( '' ).removeAttr( 'disabled' ); 100 101 // Update the name attribute with the correct index. 102 $newOptionClone.find( '.bb-poll-question_option' ).attr( 'name', 'bb-poll-question-option[' + customIndex + ']' ); 103 104 $newOptionClone.find( '.bb-poll-question_option' ).attr( 'data-opt_id', ++optLength ); 105 106 if ( 2 <= customIndex ) { 107 $newOptionClone.append( '<a href="#" class="bb-poll-edit-option_remove"><span class="bb-icon-l bb-icon-times"></span></a>' ); 108 } 109 110 // Insert the new option element before the add button. 111 $newOptionClone.insertAfter( $popupContent.find( '.bb-poll-question_options .bb-poll_option_draggable' ).last() ); 112 113 $this.removeAttr( 'disabled' ); 114 115 if ( customIndex === 9 ) { 116 $popupContent.find( '.bb-poll-option_add' ).attr( 'disabled', 'disabled' ).parent().addClass( 'bp-hide' ); 117 } 118 119 // Re-validate form. 120 validateForm(); 121 } 122 ); 123 124 // Handle removing a new option. 125 $( document ).on( 126 'click', 127 '.bb-poll-edit-option_remove', 128 function ( e ) { 129 e.preventDefault(); 130 var $this = $( this ); 131 var $popupContent = $this.closest( '.bb-action-popup-content' ); 132 var customIndex = $popupContent.find( '.bb-poll_option_draggable:not(.custom)' ).length; 133 134 // Min 2 options are required. 135 if ( customIndex <= 2 ) { 136 return; 137 } 138 $this.closest( '.bb-poll_option_draggable' ).remove(); 139 $popupContent.find( '.bb-poll-option_add' ).removeAttr( 'disabled' ).parent().removeClass( 'bp-hide' ); 140 reIndexOptions(); 141 validateForm(); 142 } 143 ); 144 145 $( document ).on( 146 'keyup', 147 '.bb-poll-question-field, .bb-poll-question_option', 148 function () { 149 validateForm(); 150 } 151 ); 152 153 $( document ).on( 154 'change', 155 '.bb-poll_duration, #bb-poll-allow-multiple-answer, #bb-poll-allow-new-option', 156 function () { 157 validateForm(); 158 } 159 ); 160 161 this.pollFormInitialized = true; 162 } 163 164 // Re-index the options. 165 function reIndexOptions() { 166 $( '#bb-activity-poll-form_modal .bb-action-popup-content' ).find( '.bb-poll_option_draggable:not(.custom)' ).each( 167 function ( index ) { 168 $( this ).find( '.bb-poll-question_option' ).attr( 'name', 'bb-poll-question-option[' + index + ']' ); 169 } 170 ); 171 $( '#bb-activity-poll-form_modal .bb-activity-poll-submit' ).removeAttr( 'disabled' ); 172 } 173 174 function validateForm() { 175 var isValid = true; 176 var $PollPopup = $( '#bb-activity-poll-form_modal' ); 177 178 // Check options. 179 $PollPopup.find( '.bb-poll-question_option' ).each( 180 function () { 181 if ( $( this ).val() === '' && ! $( this ).parent().hasClass( 'custom' ) ) { 182 isValid = false;
183 } 184 } 185 ); 186 187 // Check question. 188 if ( $PollPopup.find( '.bb-poll-question-field' ).val() === '' ) { 189 isValid = false; 190 } 191 192 if ( isValid ) { 193 $PollPopup.find( '.bb-activity-poll-submit' ).removeAttr( 'disabled' ); 194 } else { 195 $PollPopup.find( '.bb-activity-poll-submit' ).attr( 'disabled', true ); 196 } 197 198 } 199 200 $( '[class*="whats-new-form-footer"]' ).find( '.bb-poll-form .bb-action-popup' ).show(); 201 }, 202 203 hidePollForm: function ( e ) { 204 e.preventDefault(); 205 var pollFormPopup = $( e.currentTarget ).closest( '.bb-action-popup' ); 206 var pollID = pollFormPopup.data( 'poll_id' ); 207 if ( ! pollID ) { 208 // Check if any form field has data. 209 var hasData = pollFormPopup.find( 'input[type="text"]' ).filter( function () { 210 return '' !== $( this ).val(); 211 } ).length > 0 || 1 !== pollFormPopup.find( 'select' ).prop( 'selectedIndex' ) || pollFormPopup.find( 'input[type="checkbox"]' ).is( ':checked' ); 212 213 // Trigger confirmation only if any field has data. 214 if ( hasData && confirm( BP_Nouveau.activity_polls.strings.closePopupConfirm ) ) { 215 pollFormPopup.find( 'input[type="text"]' ).val( '' ); 216 pollFormPopup.find( 'select' ).prop( 'selectedIndex', 1 ); 217 pollFormPopup.find( 'input[type="checkbox"]' ).prop( 'checked', false ); 218 pollFormPopup.find( '.bp-messages.bp-feedback' ).hide(); 219 pollFormPopup.find( '.bb-poll_option_draggable' ).not( ':lt(2)' ).remove(); 220 pollFormPopup.find( '.bb-activity-poll-submit' ).attr( 'disabled', true ); 221 } 222 } 223 $( e.currentTarget ).closest( '.bb-action-popup' ).hide(); 224 var form_footer = $( e.currentTarget ).closest( '.whats-new-form-footer' ).length ? $( e.currentTarget ).closest( '.whats-new-form-footer' ) : $( e.currentTarget ).closest( '.bb-rl-whats-new-form-footer' ); 225 form_footer.find( '.post-poll.active' ).removeClass( 'active' ); 226 // Reset Activity Form Media action. 227 window.activityMediaAction = null; 228 }, 229 230 validateNewOption: function ( e ) { 231 var $newOptionField = $( e.currentTarget ); 232 var $newOption = $newOptionField.closest( '.bb-activity-poll-new-option' ); 233 var value = $newOptionField.val().trim(); 234 if ( '' !== value && value.length <= 50 ) { 235 $newOption.addClass( 'is-valid' ); 236 } else { 237 $newOption.removeClass( 'is-valid' ); 238 } 239 240 // If enter key is pressed, submit the new option. 241 if ( e.key === 'Enter' || e.keyCode === 13 ) { 242 if ( $newOption.hasClass( 'is-valid' ) ) { 243 this.submitNewOption( e ); 244 } 245 } 246 }, 247 248 submitNewOption: function( e ) { 249 e.preventDefault(); 250 // Make a call to submit a new option here. 251 var eventTarget = $( e.currentTarget ); 252 if ( eventTarget.hasClass( 'adding' ) ) { 253 return; 254 } 255 eventTarget.addClass( 'adding' ); 256 var activity = eventTarget.parents( '.activity-item' ).data( 'bp-activity' ); 257 if ( 258 ! _.isUndefined( activity ) && 259 ! _.isUndefined( activity.poll ) && 260 ! _.isUndefined( activity.poll.id ) && 261 ! _.isUndefined( activity.poll.allow_new_option ) 262 ) { 263 var pollID = activity.poll.id; 264 var existingpollOptions = activity.poll.options ? Object.values( activity.poll.options ) : []; 265 var newPollOption = eventTarget.parent().find( '.bb-activity-poll-new-option-input' ).val(); 266 var optionExists = existingpollOptions.some( 267 function ( option ) { 268 return jQuery.trim( option.option_title ) === jQuery.trim( newPollOption ); 269 } 270 ); 271 var activtyId = activity.id; 272 if ( ! optionExists ) { 273 $.ajax( 274 { 275 url: bbPollsVars.ajax_url, 276 data: { 277 'action': 'bb_pro_add_poll_option', 278 'nonce': bbPollsVars.nonce.add_poll_option_nonce, 279 'poll_id': pollID, 280 'activity_id': activtyId, 281 'poll_option': newPollOption, 282 }, 283 method: 'POST' 284 } 285 ).done( 286 function ( response ) { 287 if ( 288 response.success && 289 ! _.isUndefined( response.data ) && 290 ! _.isUndefined( response.data.option_data ) 291 ) { 292 activity.poll.options = response.data.all_options; 293 294 var activityPollEntry = new bp.Views.ActivityPollEntry( response.data ); 295 $( '#activity-' + activtyId + ' .bb-activity-poll_block' ).replaceWith( activityPollEntry.render().el ); 296 297 eventTarget.parents( '.activity-item' ).attr( 'data-bp-activity', JSON.stringify( activity ) ); 298 eventTarget.parent().removeClass( 'is-invalid' ); 299 eventTarget.parent().siblings( '.bb-poll-error' ).removeClass( 'is-visible' ); 300 eventTarget.removeClass( 'adding' ); 301 } else { 302 // error. 303 eventTarget.parent().addClass( 'is-invalid' ).removeClass( 'is-valid' ); 304 eventTarget.closest( '.bb-activity-poll-option' ).siblings( '.bb-poll-error.limit-error' ).addClass( 'is-visible' ).text( response.data ); 305 eventTarget.removeClass( 'adding' ); 306 } 307 } 308 ); 309 } else { 310 // error. 311 eventTarget.parent().addClass( 'is-invalid' ).removeClass( 'is-valid' ); 312 eventTarget.closest( '.bb-activity-poll-option' ).siblings( '.bb-poll-error.duplicate-error' ).addClass( 'is-visible' ); 313 eventTarget.removeClass( 'adding' ); 314 } 315 } 316 }, 317 318 seeMoreOptions: function ( e ) { 319 e.preventDefault(); 320 var $target = $( e.currentTarget ); 321 if ( $target.hasClass( 'see-less' ) ) { 322 $target.removeClass( 'see-less' ); 323 $target.parents( '.bb-activity-poll-options' ).find( '.bb-activity-poll-option' ).not( ':has(.bb-activity-poll-new-option-input)' ).slice( 5, 10 ).addClass( 'bb-activity-poll-option-hide' ).removeClass( 'is-visible' ); 324 } else { 325 $target.parents( '.bb-activity-poll-options' ).find( '.bb-activity-poll-option' ).not( ':has(.bb-activity-poll-new-option-input)' ).slice( 5, 10 ).addClass( 'is-visible' ); 326 $target.addClass( 'see-less' ); 327 } 328 }, 329 330 showPollState: function ( e ) { 331 e.preventDefault(); 332 var $target = $( e.currentTarget ); 333 var self = this; 334 var $statePopup = $target.closest( '#bb-poll-view' ).find( '#bb-activity-poll-state_modal' ); 335 var statePopupHeading = $target.closest( '.bb-activity-poll-option' ).find( 'label' ).text(); 336 var bbActionPopupContent = $target.closest( '#bb-poll-view' ).find( '.bb-action-popup-content' ); 337 $statePopup.find( '.bb-model-header h4' ).text( statePopupHeading ); 338 $statePopup.show(); 339 var activity = $target.parents( '.activity-item' ).data( 'bp-activity' ); 340 if ( 341 ! _.isUndefined( activity ) && 342 ! _.isUndefined( activity.poll ) && 343 ! _.isUndefined( activity.poll.id ) && 344 ! _.isUndefined( activity.poll.options ) 345 ) { 346 var optionId = $target.data( 'opt_id' ); 347 if ( optionId ) { 348 var args = { 349 'element': bbActionPopupContent, 350 'poll_id': activity.poll.id, 351 'activity_id': activity.id, 352 'option_id': optionId, 353 'paged': 1 354 }; 355 self.loadPollState( args ); 356 } 357 } 358 }, 359 360 loadPollState: function ( args ) { 361 var self = this; 362 var contentElement = args.element; 363 var pollID = args.poll_id; 364 var activityId = args.activity_id; 365 var optionId = args.option_id; 366 var paged = args.paged; 367 $.ajax( 368 { 369 url: bbPollsVars.ajax_url, 370 data: { 371 'action': 'bb_pro_poll_vote_state', 372 'nonce': bbPollsVars.nonce.poll_vote_state_nonce, 373 'poll_id': pollID, 374 'activity_id': activityId, 375 'option_id': optionId, 376 'paged': paged 377 }, 378 method: 'POST' 379 } 380 ).done( 381 function ( response ) { 382 if ( response.success && ! _.isUndefined( response.data ) && ! _.isUndefined( response.data.members ) ) { 383 var args = { 384 vote_state: ( response.success ) ? response.data : {}, 385 }; 386 387 var pollState = new bp.Views.ActivityPollState( args ); 388 if ( 1 === paged ) { 389 contentElement.html( pollState.render().el ); // Initial load. 390 } else { 391 contentElement.find( '.has-more-vote-state li:last' ).after( pollState.render().el ); // Load more. 392 } 393 394 contentElement.find( '.bb-poll-state-loader' ).remove(); 395 396 var $hasMoreVoteState = contentElement.find( '.has-more-vote-state' );
397 $hasMoreVoteState.attr( 'data-paged', response.data.others.paged ).attr( 'data-total_pages', response.data.others.total_pages ); 398 399 if ( response.data.others.paged >= response.data.others.total_pages ) { 400 $hasMoreVoteState.removeClass( 'has-more-vote-state' ); 401 } else { 402 // Add scroll event listener for infinite scroll. 403 contentElement.off( 'scroll' ).on( 404 'scroll', 405 function () { 406 if ( contentElement.scrollTop() + contentElement.innerHeight() >= contentElement[0].scrollHeight ) { 407 if ( $hasMoreVoteState.hasClass( 'has-more-vote-state' ) && contentElement.find( '.bb-poll-state-loader' ).length === 0 ) { 408 contentElement.find( 'ul.activity-state_users' ).append( '<li class="bb-poll-state-loader"><span class="bb-icon-spinner animate-spin"></span></li>' ); 409 var args = { 410 'element': contentElement, 411 'poll_id': pollID, 412 'activity_id': activityId, 413 'option_id': optionId, 414 'paged': paged + 1 415 }; 416 self.loadPollState( args ); 417 } 418 } 419 } 420 ); 421 } 422 } 423 } 424 ); 425 }, 426 427 closeVoteState: function ( e ) { 428 e.preventDefault(); 429 var $target = $( e.currentTarget ); 430 var $statePopup = $target.closest( '#bb-poll-view' ).find( '#bb-activity-poll-state_modal' ); 431 $statePopup.find( '.bb-action-popup-content .bb-action-popup-content-dynamic' ).html( '' ); 432 $statePopup.find( '.bb-activity-poll-loader' ).show(); 433 $statePopup.hide(); 434 }, 435 436 removePollOption: function ( e ) { 437 e.preventDefault(); 438 439 var $target = $( e.currentTarget ); 440 var activity = $target.parents( '.activity-item' ).data( 'bp-activity' ); 441 if ( 442 ! _.isUndefined( activity ) && 443 ! _.isUndefined( activity.poll ) && 444 ! _.isUndefined( activity.poll.id ) 445 ) { 446 var pollID = activity.poll.id; 447 var activtyId = activity.id; 448 var optionId = 0; 449 if ( $target.parent( '.bb-activity-poll-option' ).find( '.bp-radio-wrap' ).length ) { 450 optionId = $target.parent( '.bb-activity-poll-option' ).find( '.bs-styled-radio' ).data( 'opt_id' ); 451 } else { 452 optionId = $target.parent( '.bb-activity-poll-option' ).find( '.bs-styled-checkbox' ).data( 'opt_id' ); 453 } 454 if ( optionId ) { 455 if ( confirm( BP_Nouveau.activity_polls.strings.areYouSure ) ) { 456 $.ajax( 457 { 458 url: bbPollsVars.ajax_url, 459 data: { 460 'action': 'bb_pro_remove_poll_option', 461 'nonce': bbPollsVars.nonce.remove_poll_option_nonce, 462 'poll_id': pollID, 463 'activity_id': activtyId, 464 'option_id': optionId, 465 }, 466 method: 'POST' 467 } 468 ).done( 469 function ( response ) { 470 if ( 471 response.success && 472 ! _.isUndefined( response.data ) && 473 ! _.isUndefined( response.data.option_data ) 474 ) { 475 activity.poll.options = response.data.all_options; 476 477 var activityPollEntry = new bp.Views.ActivityPollEntry( response.data ); 478 $( '#activity-' + activtyId + ' .bb-activity-poll_block' ).html( activityPollEntry.render().el ); 479 480 // Remove option and update new data. 481 $target.parents( '.activity-item' ).attr( 'data-bp-activity', JSON.stringify( activity ) ); 482 483 $target.parent( '.bb-activity-poll-option' ).remove(); 484 } 485 } 486 ); 487 } 488 } 489 } 490 }, 491 492 addPollVote: function ( e ) { 493 e.preventDefault(); 494 495 var $target = $( e.currentTarget ); 496 var activity = $target.parents( '.activity-item' ).data( 'bp-activity' ); 497 if ( 498 ! _.isUndefined( activity ) && 499 ! _.isUndefined( activity.poll ) && 500 ! _.isUndefined( activity.poll.id ) && 501 ! _.isUndefined( activity.poll.options ) 502 ) { 503 var pollID = activity.poll.id; 504 var activtyId = activity.id; 505 var optionId = $target.data( 'opt_id' ); 506 $target.addClass( 'is-checked' ); 507 if ( optionId ) { 508 $.ajax( 509 { 510 url: bbPollsVars.ajax_url, 511 data: { 512 'action': 'bb_pro_add_poll_vote', 513 'nonce': bbPollsVars.nonce.add_poll_vote_nonce, 514 'poll_id': pollID, 515 'activity_id': activtyId, 516 'option_id': optionId 517 }, 518 method: 'POST' 519 } 520 ).done( 521 function ( response ) { 522 if ( 523 response.success && 524 ! _.isUndefined( response.data ) && 525 ! _.isUndefined( response.data.vote_data ) 526 ) { 527 // Replace the updated total votes. 528 activity.poll.total_votes = response.data.total_votes; 529 activity.poll.options = response.data.all_options; 530 531 var activityPollEntry = new bp.Views.ActivityPollEntry( response.data ); 532 var activityItem = $( '#activity-' + activtyId ).length ? $( '#activity-' + activtyId + ' .bb-activity-poll_block' ) : $( '#bb-rl-activity-' + activtyId + ' .bb-activity-poll_block' ); 533 534 if ( activityItem.length ) { 535 activityItem.replaceWith( activityPollEntry.render().el ); 536 } 537 538 $target.parents( '.activity-item' ).attr( 'data-bp-activity', JSON.stringify( activity ) ); 539 } 540 } 541 ); 542 } 543 } 544 } 545 546 }; 547 548 bp.Views.activityPollForm = Backbone.View.extend( 549 { 550 tagName: 'div', 551 id: 'bb-poll-form', 552 className: 'bb-poll-form', 553 template: bp.template( 'bb-activity-poll-form' ), 554 555 events: { 556 'click .bb-activity-poll-submit': 'submitPoll', 557 }, 558 559 initialize: function () { 560 this.model.on( 'change:poll change:poll_id', this.render, this ); 561 }, 562 563 render: function () { 564 this.$el.html( this.template( this.model.attributes ) ); 565 return this; 566 }, 567 568 submitPoll: function ( event ) { 569 event.preventDefault(); 570 571 var pollForm = $( event.target ).closest( '#bb-activity-poll-form_modal' ); 572 var pollID = pollForm.data( 'poll_id' ); 573 var poll_que = pollForm.find( '.bb-poll-question-field' ).val(); 574 var poll_options = pollForm.find( '.bb-poll-question_option' ).map( 575 function () { 576 if ( ! $( this ).parent().hasClass( 'custom' ) ) { 577 var obj = {}; 578 var key = $( this ).attr( 'data-opt_id' ); 579 obj[key] = $( this ).val(); 580 return obj; 581 } 582 } 583 ).get(); 584 var poll_allow_multiple_answer = pollForm.find( '#bb-poll-allow-multiple-answer' ).is( ':checked' ); 585 var poll_allow_new_option = pollForm.find( '#bb-poll-allow-new-option' ).is( ':checked' ); 586 var poll_duration = pollForm.find( '.bb-poll_duration' ).val(); 587 var activityID = pollForm.data( 'activity_id' ); 588 var model = this.model; 589 590 var groupID = 0; 591 if ( $( '#item-header' ).length && 'groups' === $( '#item-header' ).data( 'bp-item-component' ) ) { 592 groupID = $( '#item-header' ).data( 'bp-item-id' ); 593 } else if ( ! _.isUndefined( model.get( 'object' ) ) && 'group' === model.get( 'object' ) && ! _.isUndefined( model.get( 'item_id' ) ) ) { 594 groupID = model.get( 'item_id' ); 595 } 596 597 // Duration validate. 598 if ( 599 ! _.isUndefined( model.get( 'edit_activity' ) ) && 600 true === model.get( 'edit_activity' ) && 601 ! _.isUndefined( model.get( 'poll' ) ) && 602 ! _.isUndefined( model.get( 'poll' ).duration ) && 603 model.get( 'poll' ).duration !== poll_duration 604 ) { 605 poll_duration = model.get( 'poll' ).duration; 606 } 607 608 $( event.target ).addClass( 'loading' ); 609 610 $.ajax( 611 { 612 url: bbPollsVars.ajax_url, 613 data: { 614 'action': 'bb_pro_add_poll', 615 'nonce': bbPollsVars.nonce.add_poll_nonce, 616 'poll_id': pollID, 617 'activity_id': activityID, 618 'group_id': groupID, 619 'questions': poll_que, 620 'options': poll_options,
621 'allow_multiple_answer': poll_allow_multiple_answer, 622 'allow_new_option': poll_allow_new_option, 623 'duration': poll_duration 624 }, 625 method: 'POST' 626 } 627 ).done( 628 function ( response ) { 629 if ( response.success && typeof response.data !== 'undefined' ) { 630 var pollObject = { 631 id: response.data.id, 632 item_id: response.data.item_id, 633 user_id: parseInt( response.data.user_id ), 634 vote_disabled_date: response.data.vote_disabled_date, 635 question: response.data.question, 636 options: response.data.options, 637 total_votes: response.data.total_votes 638 }; 639 640 if ( response.data.settings ) { 641 pollObject.allow_multiple_options = response.data.settings.allow_multiple_options || false; 642 pollObject.allow_new_option = response.data.settings.allow_new_option || false; 643 pollObject.duration = response.data.settings.duration || 3; 644 } 645 model.set( 'poll', pollObject ); 646 model.set( 'poll_id', response.data.id ); 647 648 // Validate topic content with poll. 649 if ( 650 ! _.isUndefined( BP_Nouveau.activity.params.topics ) && 651 ! _.isUndefined( BP_Nouveau.activity.params.topics.bb_is_enabled_activity_topics ) && 652 BP_Nouveau.activity.params.topics.bb_is_enabled_activity_topics && 653 ! _.isUndefined( BP_Nouveau.activity.params.topics.bb_is_activity_topic_required ) && 654 BP_Nouveau.activity.params.topics.bb_is_activity_topic_required && 655 'undefined' !== typeof BBTopicsManager && 656 'undefined' !== typeof BBTopicsManager.bbTopicValidateContent 657 ) { 658 BBTopicsManager.bbTopicValidateContent( { 659 self : this, 660 selector : $( '#whats-new-form' ), 661 validContent : true, 662 class : 'focus-in--empty', 663 data : model.attributes, 664 action : 'poll_form_submitted' 665 } ); 666 } else { 667 $( '#whats-new-form' ).removeClass( 'focus-in--empty' ); 668 } 669 670 $( event.target ).closest( '#bb-activity-poll-form_modal' ).hide(); 671 window.activityMediaAction = null; 672 pollForm.find( '#bb-poll-allow-multiple-answer .bb-action-popup-content > .bp-feedback' ).removeClass( 'active' ); 673 } 674 if ( false === response.success ) { 675 pollForm.find( '.bb-action-popup-content > .bp-feedback' ).addClass( 'active' ).find( 'p' ).text( response.data ); 676 } 677 $( event.target ).removeClass( 'loading' ); 678 } 679 ); 680 } 681 } 682 ); 683 684 bp.Views.activityPollView = Backbone.View.extend( 685 { 686 tagName: 'div', 687 id: 'bb-poll-view', 688 className: 'bb-poll-view', 689 template: bp.template( 'bb-activity-poll-view' ), 690 691 events: { 692 'click .bb-activity-poll-options-action': 'showOptions', 693 'click .bb-activity-poll-action-edit': 'editPoll', 694 'click .bb-activity-poll-action-delete': 'deletePoll' 695 }, 696 697 initialize: function () { 698 this.model.on( 'change:activity_poll_title change:activity_poll_options change:activity_poll_allow_multiple_answer change:activity_poll_allow_new_option change:activity_poll_duration', this.render, this ); 699 this.model.on( 'change', this.render, this ); 700 701 $( document ).on( 702 'click', 703 function ( event ) { 704 if ( ! $( event.target ).closest( '.bb-activity-poll-options-wrap' ).length ) { 705 $( '.bb-activity-poll-options-wrap' ).removeClass( 'active' ); 706 } 707 } 708 ); 709 }, 710 711 render: function () { 712 this.$el.html( this.template( this.model.attributes ) ); 713 return this; 714 }, 715 716 showOptions: function ( event ) { 717 event.preventDefault(); 718 $( event.target ).closest( '.bb-activity-poll-options-wrap' ).toggleClass( 'active' ); 719 }, 720 721 editPoll: function ( e ) { 722 e.preventDefault(); 723 bp.Nouveau.Poll.showPollForm(); 724 }, 725 726 deletePoll: function ( e ) { 727 e.preventDefault(); 728 729 var $target = $( e.currentTarget ); 730 var $this = this; 731 var confirm_delete = confirm( BP_Nouveau.activity_polls.strings.DeletePollConfirm ); 732 if ( confirm_delete ) { 733 var pollID = $target.parents( '.poll_view' ).data( 'poll_id' ); 734 var activityID = $target.parents( '.poll_view' ).data( 'activity_id' ); 735 $.ajax( 736 { 737 url: bbPollsVars.ajax_url, 738 data: { 739 'action': 'bb_pro_remove_poll', 740 'nonce': bbPollsVars.nonce.remove_poll_nonce, 741 'poll_id': pollID, 742 'activity_id': activityID 743 }, 744 method: 'POST' 745 } 746 ).done( 747 function ( response ) { 748 if ( response.success ) { 749 $this.resetPollData(); 750 $( '.post-poll.active' ).removeClass( 'active' ); 751 if ( bp.draft_activity.data.poll ) { 752 if ( '' === bp.draft_activity.data.content ) { 753 $( '#discard-draft-activity' ).trigger( 'click' ); 754 } else { 755 bp.Nouveau.Activity.postForm.collectDraftActivity(); 756 } 757 } else { 758 bp.Nouveau.refreshActivities(); 759 } 760 } 761 } 762 ); 763 } else { 764 $( '.bb-activity-poll-options-wrap.active' ).removeClass( 'active' ); 765 } 766 }, 767 768 resetPollData: function () { 769 this.model.set( 'poll', {} ); 770 this.model.set( 'poll_id', '' ); 771 } 772 } 773 ); 774 775 bp.Views.ActivityPollState = Backbone.View.extend( 776 { 777 tagName: 'div', 778 className: 'bb-action-popup-content-dynamic', 779 template: bp.template( 'bb-activity-poll-state' ), 780 781 initialize: function ( options ) { 782 this.data = options; 783 }, 784 render: function () { 785 this.$el.html( this.template( this.data ) ); 786 return this; 787 }, 788 } 789 ); 790 791 bp.Views.ActivityPollEntry = Backbone.View.extend( 792 { 793 tagName: 'div', 794 className: 'bb-activity-poll_block', 795 template: bp.template( 'bb-activity-poll-entry' ), 796 797 initialize: function ( poll_data ) { 798 this.data = poll_data; 799 }, 800 render: function () { 801 this.$el.html( this.template( this.data ) ); 802 return this; 803 }, 804 } 805 ); 806 807 // Launch Poll. 808 bp.Nouveau.Poll.start(); 809 810} )( bp, jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.