ips.templates.set('dulive.shoutbox.message'," <li class='shoutboxRow ipsContained ipsType_break {{hasTools}}' data-id='{{id}}'>  <div class='shoutboxRow_user'>  <span class='name'>{{{userName}}}</span>  <span class='ipsResponsive_showPhone ipsResponsive_inline cChatRow_responsiveDate ipsType_light pull-right'>{{date}}</span>  {{#edited}}   <em class='ipsResponsive_showPhone ipsResponsive_inline cChatRow_responsiveDate ipsType_light pull-right edited'>(edited)</em>  {{/edited}} </div>  <div class='shoutboxRow_content'>{{{message}}}</div>  <div class='shoutboxRow_date'>  {{#edited}}   <em class='edited'>(edited)</em>  {{/edited}}  <span class='date' data-timestamp='{{createdAt}}'>{{date}}</span>  </div>  {{#notSystem}} <aside class='shoutboxRow_tools'>   {{#canEdit}}<a href='#' data-action='edit' class='ipsButton ipsButton_positive ipsButton_veryVerySmall'><i class='fa fa-edit'></i> Edit</a>{{/canEdit}}   {{#canDelete}}<a href='#' data-action='delete' class='ipsButton ipsButton_negative ipsButton_veryVerySmall'><i class='fa fa-remove'></i> Delete</a>{{/canDelete}}  </aside>  {{/notSystem}}</li> ");ips.templates.set('dulive.shoutbox.message_edit'," <li class='shoutboxRow ipsContained ipsType_break edit' data-id='{{id}}'>  <div class='shoutboxRow_user'><span class='name'>{{{userName}}}</span> <span class='ipsResponsive_showPhone ipsResponsive_inline cChatRow_responsiveDate ipsType_light pull-right'>{{date}}</span></div>  <div class='shoutboxRow_content'><input type='text' value='{{message}}' /></div>  <div class='shoutboxRow_date'>  <span class='date' data-timestamp='{{createdAt}}'>{{date}}</span>  </div>  <aside class='shoutboxRow_tools'>   <a href='#' data-action='save' class='ipsButton ipsButton_positive ipsButton_veryVerySmall'><i class='fa fa-check'></i> Save</a>   <a href='#' data-action='cancel' class='ipsButton ipsButton_negative ipsButton_veryVerySmall'><i class='fa fa-remove'></i> Cancel</a>  </aside> </li> ");;
;(function($,_){"use strict";function Shout(_data){var $output;var _timer=null;function data(){_data.date=moment(_data.createdAt).fromNow();_data.canEdit=(ips.getSetting('shoutbox_edit')===1);_data.canDelete=(ips.getSetting('shoutbox_delete')===1);_data.hasTools=((_data.canEdit||_data.canDelete)&&_data.system===false)?"hasTools":"";_data.notSystem=(_data.system===false);return _data;}
function update(newData,$replace){_data=newData;_data=data();if($replace instanceof jQuery){var $output=render();$output.addClass('flash');$replace.replaceWith($output);}}
function render($replace){var renderData=_.extend({},data());$output=$(ips.templates.render('dulive.shoutbox.message',renderData));$output.data('shout',scope);if($replace instanceof jQuery){$replace.replaceWith($output);return;}
return $output;}
function renderEdit(cleanShout,$replace){var editData=_.extend({},data());editData.message=cleanShout.message;var $editOutput=$(ips.templates.render('dulive.shoutbox.message_edit',editData));$editOutput.data('shout',scope);setTimeout(function(){var $input=$editOutput.find('input');$input.focus();$input[0].setSelectionRange(500,500);},1);$replace.replaceWith($editOutput);}
function destroy(){clearInterval(_timer);}
function init(){_timer=setInterval(function(){var $timestamp=$output.find('[data-timestamp]');$timestamp.text(moment(data().createdAt).fromNow());},15*1000);}
init();var scope={update:update,render:render,renderEdit:renderEdit,destroy:destroy,toJSON:function(){var d=data();delete d.date;return d;}};return scope;}
ips.controller.register('dulive.front.shoutbox.main',{_shouts:{},_notifs:false,_settings:{},_lastShout:0,initialize:function(){this.on('click','button[type=submit]',this.sendMessage);this.on('keypress','.shoutboxTextarea',this.sendMessage);this.on('click','[data-action]',this.rowAction);this.on('keypress','.shoutboxRow_content input[type="text"]',this._editShoutSave);this.on('click','.shoutbox-notifs',this.toggleNotifs);this._settings={"edit":ips.getSetting('shoutbox_edit')===1,"delete":ips.getSetting('shoutbox_delete')===1,"ban":ips.getSetting('shoutbox_ban')===1,"unban":ips.getSetting('shoutbox_unban')===1};this.enableNotifications();this.loadCachedShouts();this.setup();},enableNotifications:function(setNow){if(ips.utils.notification.needsPermission()){ips.utils.notification.requestPermission();}
this._notifs=ips.utils.notification.hasPermission();this._notifs=(ips.utils.db.get('dulive-shoutbox','notifications')!==true)?false:this._notifs;if(this._notifs){this.scope.find(".shoutbox-notifs i.fa").removeClass('fa-bell-o').addClass('fa-bell');}},loadCachedShouts:function(){var messages=ips.utils.db.get('dulive-shoutbox','messages');if(messages!==null){var $shouts=this.scope.find('ul.shoutboxMessages');for(var id in messages){var message=messages[id];this._shouts[id]=new Shout(message);$shouts.append(this._shouts[id].render());this._lastShout=(id>this._lastShout)?id:this._lastShout;}
this.scrollDown(true);this.scope.find('.shoutboxLoading_mini').hide();}},setup:function(){if(!ips.getSetting('shoutbox_view')){return;}
this.socket=DULive.registerModule('shoutbox:shouts');this._bind('authorized');this._bind('disconnected');this._bind('receivedMessages');this._bind('shoutSaved');this._bind('shoutDeleted');this._bind('cleanShout');},_bind:function(evt){var scope=this;this.socket.listen(evt,function(){scope[evt].apply(scope,arguments);});},authorized:function(){this.scope.find('.shoutbox').removeClass('loading');this.socket.emit('shoutsSince',this._lastShout);this.socket.emit('syncShouts',Object.keys(this._shouts));},disconnected:function(){this.scope.find('.shoutbox').addClass('loading');},receivedMessages:function(messages){var $shouts=this.scope.find('ul.shoutboxMessages');var ctxt=this;messages.forEach(function(message){ctxt._shouts[message.id]=new Shout(message);$shouts.append(ctxt._shouts[message.id].render());ctxt._lastShout=(message.id>ctxt._lastShout)?message.id:ctxt._lastShout;});if(messages.length===1&&!messages[0].system&&messages[0].userID!==ips.getSetting('memberID')&&this._notifs===true){var notif=ips.utils.notification.create({title:$("<div>").html(messages[0].userName).text()+" said:",body:messages[0].message,icon:messages[0].photo,timeout:5,onClick:function(){try{window.focus();}catch(err){}}});notif.show();ips.loader.get(['core/interface/buzz/buzz.min.js']).then(function(){var sound=new buzz.sound(ips.getSetting('baseURL')+'applications/core/interface/sounds/notification',{formats:["mp3"]});sound.play();});}
else{this.scrollDown(true);}
this.scrollDown();this.saveShouts();},sendMessage:function(e){var $shoutbox=this.scope.find('.shoutboxTextarea');if($shoutbox.val().length===0){return;}
switch(e.type){case"click":this.socket.emit('sendMessage',$shoutbox.val());$shoutbox.val('');break;case"keypress":if(e.keyCode===ips.ui.key.ENTER){this.socket.emit('sendMessage',$shoutbox.val());$shoutbox.val('');}
break;}},rowAction:function(e){e.preventDefault();var action=$(e.target).data('action');var $row=$(e.target).closest('li.shoutboxRow');var id=$row.data('id');switch(action){case"edit":Debug.log("Editing shout id: "+id);this.socket.emit('cleanShout',id);break;case"delete":Debug.log("Deleting shout id: "+id);this.socket.emit('deleteShout',id);break;case"save":var val=$row.find('.shoutboxRow_content input').val();if(val.length===0||val===this._shouts[id].message){return;}
Debug.log("Saving shout id: "+id);this.socket.emit('editShout',{id:id,message:val});break;case"cancel":Debug.log("Cancelling shout id: "+id);$row.data('shout').render($row);break;}},shoutSaved:function(newShout){var $row=this.scope.find('li.shoutboxRow[data-id="'+newShout.id+'"]');$row.data('shout').update(newShout,$row);this.saveShouts();},_editShoutSave:function(e){if(e.keyCode===ips.ui.key.ENTER){var $row=$(e.target).closest('li.shoutboxRow');var id=$row.data('id');if($(e.target).val().length===0||$(e.target).val()===this._shouts[id].message){return;}
Debug.log("Saving shout id: %s",id);this.socket.emit('editShout',{id:id,message:$(e.target).val()});}
if(e.keyCode===ips.ui.key.ESCAPE){var $row=$(e.target).closest('li.shoutboxRow');$row.data('shout').render($row);}},shoutDeleted:function(shoutID){this._shouts[shoutID].destroy();delete this._shouts[shoutID];var ctxt=this;this.scope.find('li.shoutboxRow[data-id="'+shoutID+'"]').one('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd',function(){$(this).remove();ctxt.saveShouts();}).addClass('fadeOut');},toggleNotifs:function(e){e.preventDefault();this._notifs=!this._notifs;ips.utils.db.set('dulive-shoutbox','notifications',this._notifs);if(this._notifs){this.scope.find(".shoutbox-notifs i.fa").removeClass('fa-bell-o').addClass('fa-bell');}
else{this.scope.find(".shoutbox-notifs i.fa").removeClass('fa-bell').addClass('fa-bell-o');}},cleanShout:function(shout){DULive.log("Got clean shout",shout);var $row=this.scope.find('li.shoutboxRow[data-id="'+shout.id+'"]');$row.data('shout').renderEdit(shout,$row);},scrollDown:function(force){var $shouts=this.scope.find('ul.shoutboxMessages');var scrollHeight=$shouts[0].scrollHeight;var scrollTop=$shouts.scrollTop();if(scrollHeight-scrollTop-$shouts.height()<200||force){$shouts.scrollTop(scrollHeight);}},saveShouts:function(){var shoutsToSave={};var keys=Object.keys(this._shouts).sort().reverse();for(var i=0,len=keys.length;i<len&&i<100;i++){var shout=this._shouts[keys[i]];shoutsToSave[keys[i]]=shout;}
ips.utils.db.set('dulive-shoutbox','messages',shoutsToSave);}});}(jQuery,_));;