var SendMessage = new Class({ Implements: [Options, Events], options:{ 'recipient':null }, initialize:function(options){ var self = this; this.setOptions(options || {}); this.recipientList = []; this.elementList = new Hash(); this.blockEvent = false; this.elements = {}; this.buildInterface(); }, buildInterface: function() { var self = this; // this is the main message skeleton this.tempUserToInputElem = new Element('input', { 'tabindex':'1', 'autocomplete':'off', 'type':'text', 'name':'temp_userTo', 'id':'temp_userTo' }); this.userToInputElem = new Element('input', { 'tabindex':'-1', 'type':'hidden', 'name':'userTo', 'id':'userTo', 'value':'' }); this.elements.subjectInput = new Element('input', { 'id':'subject', 'tabindex':'2', 'type':'text', 'name':'subject', 'maxlength':'250', 'class': "ig-text js {'validate':{'suppresssubmit':true,'donotdisablesubmitbuttons':true,'popup':true,'suppressmessages':'true','length':{'trim':'true','min':'1','minmessage':'" + window.translations('system-page-error_Validation_Cannot_Be_Blank') + "','max':'250','maxmessage':'" + window.translations('system-page-error_Validation_Too_Long') + "'}}}" }); this.elements.bodyIsTextInput = new Element('input', { 'tabindex':'-1', 'type':'hidden', 'name':'bodyIsText', 'value':'true' }); this.elements.bodyTextarea = new Element('textarea', { 'id':'body', 'tabindex':'3', 'rows':'4', 'cols':'30', 'name':'body', 'class':"js {'validate':{'suppresssubmit':true,'noborder':true,'donotdisablesubmitbuttons':true,'popup':true,'length':{'min':'1','minmessage':window.translations('system-page-error_Validation_Cannot_Be_Blank'),'trim':'true'},'suppressmessages':'true'}}" }); this.elements.sendButton = new Element('input', { 'tabindex':'4', 'type':'submit', 'class':'ig-button ig-bold js-submitbutton', 'value':window.translations('comps-message-Send_Message') }); this.elements.cancelButton = new Element('input', { 'tabindex':'5', 'type':'button', 'class':'ig-button ig-button_gray ig-right', 'value':window.translations('comps-message-Cancel') }).addEvent('click', function(){ self.close(); }) this.elements.footer = new Element('div', { 'class':'modalfooter' }).adopt( this.elements.sendButton, this.elements.cancelButton ); this.elements.bodyContainer = new Element('div', { 'class':'ig-formitem ig-clearfix' }).adopt( new Element('label', { 'html':window.translations('comps-message-Body') }), new Element('div', { 'class':'ig-textarea' }).adopt( new Element('span').adopt( this.elements.bodyIsTextInput, this.elements.bodyTextarea ) ) ); this.elements.subjectContainer = new Element('div', { 'class':'ig-formitem ig-clearfix' }).adopt( new Element('label', { 'class':'inline', 'html':window.translations('comps-message-Subject') }), new Element('div', { 'class':'ig-text' }).adopt( new Element('span').adopt( this.elements.subjectInput ) ) ); this.elements.recipientList = new Element('div', { 'class':'myRecipientList ig-clearfix' }); this.elements.titleDiv = new Element('div', { 'class':'title' }).adopt( new Element('label', { 'class':'inline', 'html':window.translations('media-js-To') }), new Element('span', { 'class':'text ig-recipients-box' }).adopt( this.elements.recipientList, this.tempUserToInputElem ) ); this.elements.form = new Element('form', { 'action':'/my.discussions.compose', 'method':'post', 'class':'modalform noDisableOnSubmit ig-form' }).adopt( this.userToInputElem, this.elements.titleDiv, this.elements.subjectContainer, this.elements.bodyContainer, this.elements.footer ).addEvent('onvalidformsubmit', function(){ this.sendMessage(); }.bind(this)); this.elements.messageCore = new Element('div', { 'class':'ig-modal-pad ig-compose' }).adopt( this.elements.form ); }, getElementById:function(userTo){ return this.elementList.get(userTo); }, newRecipientElem:function(user){ var self = this; var hiddenUserTo = this.userToInputElem; var visibleInput = this.tempUserToInputElem; if(user && user.userTo){ var elem = new Element('div', { 'class':'ig-message-member ig-left' }).adopt( new Element('span', { 'class': 'ig-nowrap', 'html': user.name.substr(0, 45) }), new Element('a', { 'href':'#', 'title':window.translations('comps-message-Remove') }).adopt( new Element('img', { 'src':'/cmedia/img/interface/v3/ig-icon-recepient-close.gif' }) ).addEvent('click', function(event){ event.stop(); self.removeRecipient( this.getParent('div.ig-message-member') ); visibleInput.focus(); }) ); if(user.type == 'member'){ elem.getElement('span').setAttribute('title',window.translations('comps-message-Member')); }else{ elem.getElement('span').setAttribute('title',window.translations('comps-message-Group')); } elem.user = user; return elem; } return null; }, addRecipient:function(elem){ var self = this; var hiddenUserTo = this.userToInputElem; var visibleInput = this.tempUserToInputElem; var recipients = this.recipientList; if(elem && elem.user && elem.user.userTo && !recipients.contains(elem.user.userTo)){ recipients.push(elem.user.userTo); this.elements.recipientList.adopt(elem); } hiddenUserTo.value = recipients.join(','); this.elementList.set(elem.user.userTo, elem); if(this.modal){ this.modal.adjustposition(); } this.autocompleter.hideList(); visibleInput.value = ''; visibleInput.focus(); }, removeRecipient:function(elem){ var hiddenUserTo = this.userToInputElem; var recipients = this.recipientList; recipients.erase(elem.user.userTo); hiddenUserTo.value = recipients.join(','); elem.dispose(); if(this.modal){ this.modal.adjustposition(); } }, create:function(){ var self = this; // will open a new modalbox adopting the messageCore as the body this.modal = new Modalbox({ 'width':'450', 'position':{'absolute':{}}, 'title':window.translations('media-js-send_a_message'), 'adopt':this.elements.messageCore, 'onClose':function(){ self.autocompleter.closeList(); $$('.ig-form-validation-popup').dispose(); }, 'onAdopt':function() { bootstrap(this.mymodalfront); if (self.options.recipient !== null) { self.elements.subjectInput.focus(); } else { self.tempUserToInputElem.focus(); } } }); // create an instance of the AutocompleteUserSearch class this.autocompleter = new AutocompleteSearchDisplayUser({ 'inputBox': this.tempUserToInputElem.id, 'qType': Igloo.mSearchType.EMAIL + Igloo.mSearchType.NAME + Igloo.mSearchType.NAMESPACE, 'searchMethod': 'user', 'autohighlightfirst': true, 'onSelect': function(user){ self.addRecipient( self.newRecipientElem( user ) ); } }); if (this.options.recipient) { this.addRecipient( this.newRecipientElem(this.options.recipient) ); } }, close:function(){ // close the modal, if one is present if(this.modal){ this.modal.close(); } }, sendMessage:function(){ // makes the API call to send the message var self = this; var userTo = this.userToInputElem.get('value'); if (userTo == '') { //alert('need at least one recipient'); this.tempUserToInputElem.coords = this.tempUserToInputElem.getCoordinates(); injectpopup(this.tempUserToInputElem, window.translations('comps-message-Select_a_community_member')); return false; } var subject = this.elements.subjectInput.get('value').trim(); var body = this.elements.bodyTextarea.get('value').trim(); requestData = { 'messageType':4, 'userTo':userTo, 'subject':subject, 'bodyIsText':'true', 'bodyprefix':'', 'body':body }; // Disable multi submit if (!this.blockEvent) { this.blockEvent = true; var createCall = new ApiClient({ 'apimethod':'account/discussions/create', 'postdata':requestData, onRequest:function(){ // Disable Api request self.elements.sendButton.addClass('ig-button_disabled'); }, onSuccess:function(){ var returnstring = this.response.text; var returnjson = JSON.decode(returnstring,true); // tell the validator that this message has been sent self.elements.messageCore.children[0].fireEvent('submitcompleted'); if(returnjson !== null){ var successmsg; var modalpage = new Element('div',{ 'class':'success ig-modal-pad' }); var OKmessage = new Element('h3',{ 'style':'margin-bottom: 9px', 'html':window.translations('media-js-success_message_sent') }); var OKbutt = new Element('a',{ 'href':'#', 'html':window.translations('media-js-close_window') }); var OKor = new Element('span', { 'html':' ' + window.translations('media-js-_or_') + ' ' }); var OKoptions = new Element('a',{ 'href':'/my.discussions', 'html':window.translations('media-js-view_my_messages') }); OKmessage.inject(modalpage,'bottom'); OKbutt.addEvent('click', function(event){ event.preventDefault(); successmsg.close(); }); OKbutt.inject(modalpage,'bottom'); OKor.inject(OKbutt,'after'); OKoptions.inject(OKor,'after'); self.close(); successmsg = new Modalbox({ 'title':window.translations('media-js-success'), 'adopt':modalpage }); }else{ // what did return was not JSON. self.close(); var errormsg = new Modalbox({ 'title':window.translations('media-js-Error') }).setcontents('

'+window.translations('media-js-your_message_could_not_be_delivered')+'

'); return false; } return null; }, onFailure:function(){ self.close(); var errormsg = new Modalbox({ 'title':window.translations('media-js-Error') }).setcontents('

'+window.translations('media-js-your_message_could_not_be_delivered')+'

'); return false; }, onComplete:function(){ self.elements.sendButton.removeClass('ig-button_disabled'); // Re-enable submit trigger self.blockEvent = false; } }); } } }); document.addEvent('domready', function(){ if(Igloo.asset_ajax_sendmessage){ var elem; Object.each(Igloo.asset_ajax_sendmessage, function(object, id){ elem = $(id); if(elem && object){ elem.addEvent('click', function(event){ event.preventDefault(); var message = new SendMessage(object); message.create(); }); } }); } }); window.addEvent('bootstrap4finished', function(){ // apply modal behaviour to the links var elements = window.dependents['ajax_sendmessage']; if (elements) { elements.each(function(elem){ elem.addEvent('click', function(event){ event.preventDefault(); var message = new SendMessage(elem.dataobj['ajax_sendmessage']); message.create(); }); }); } });