mirror of
				https://github.com/Mibew/java.git
				synced 2025-10-31 18:41:09 +03:00 
			
		
		
		
	Add finalizer to chat module in Chat JavaScript application
This commit is contained in:
		
							parent
							
								
									5d0ac36fa4
								
							
						
					
					
						commit
						23812038d9
					
				| @ -5,5 +5,6 @@ | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this))},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread, | ||||
| c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); | ||||
| (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId}); | ||||
| for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this, | ||||
| a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); | ||||
|  | ||||
| @ -5,4 +5,4 @@ | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(a,b){a.Models.Avatar=a.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){a.Objects.server.registerFunction("setupAvatar",b.bind(this.apiSetupAvatar,this))},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); | ||||
| (function(b,c){b.Models.Avatar=b.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){this.registeredFunctions=[];this.registeredFunctions.push(b.Objects.server.registerFunction("setupAvatar",c.bind(this.apiSetupAvatar,this)))},finalize:function(){for(var a=0;a<this.registeredFunctions.length;a++)b.Objects.server.unregisterFunction(this.registeredFunctions[a])},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); | ||||
|  | ||||
| @ -5,8 +5,10 @@ | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=a.Application;j.module("Chat",{startWithParent:!1}).addInitializer(function(d){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(d.layoutsData.chat||{})});a.Objects.chatLayout=f;j.mainRegion.show(f);c.thread=new a.Models.Thread(d.thread);c.user=new a.Models.ChatUser(d.user);c.page=new a.Models.Page(d.page);var e=new a.Collections.Controls; | ||||
| c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:d.links.mailLink}),e.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:d.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:d.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); | ||||
| e.add(b.refresh);d.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:d.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); | ||||
| c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(d.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));c.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.callFunctionsPeriodically(function(){var b= | ||||
| a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))})})})(Mibew); | ||||
| (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=[],k=a.Application,l=k.module("Chat",{startWithParent:!1});l.addInitializer(function(c){var g=a.Objects,d=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(c.layoutsData.chat||{})});a.Objects.chatLayout=f;k.mainRegion.show(f);d.thread=new a.Models.Thread(c.thread);d.user=new a.Models.ChatUser(c.user);d.page=new a.Models.Page(c.page);var e=new a.Collections.Controls; | ||||
| d.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:c.links.mailLink}),e.add(b.sendMail));d.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:c.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:c.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); | ||||
| e.add(b.refresh);c.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:c.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); | ||||
| d.user.get("isAgent")||(d.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:d.avatar})));g.Collections.messages=new a.Collections.Messages;d.messageForm=new a.Models.MessageForm(c.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:d.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));d.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:d.sound}));j.push(g.server.callFunctionsPeriodically(function(){var b= | ||||
| a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))}))});l.addFinalizer(function(){a.Objects.chatLayout.close(); | ||||
| for(var c=0;c<j.length;c++)a.Objects.server.stopCallFunctionsPeriodically(j[c]);"undefined"!=typeof a.Objects.Models.avatar&&a.Objects.Models.avatar.finalize();a.Objects.Collections.messages.finalize();delete a.Objects.chatLayout;delete a.Objects.Models.thread;delete a.Objects.Models.user;delete a.Objects.Models.page;delete a.Objects.Models.avatar;delete a.Objects.Models.messageForm;delete a.Objects.Models.sound;delete a.Objects.Models.Controls;delete a.Objects.Models.Status;delete a.Objects.Collections.messages; | ||||
| delete a.Objects.Collections.controls;delete a.Objects.Collections.status})})(Mibew); | ||||
|  | ||||
| @ -25,7 +25,7 @@ MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null, | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(a,b){a.Models.Avatar=a.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){a.Objects.server.registerFunction("setupAvatar",b.bind(this.apiSetupAvatar,this))},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); | ||||
| (function(b,c){b.Models.Avatar=b.Models.Base.extend({defaults:{imageLink:!1},initialize:function(){this.registeredFunctions=[];this.registeredFunctions.push(b.Objects.server.registerFunction("setupAvatar",c.bind(this.apiSetupAvatar,this)))},finalize:function(){for(var a=0;a<this.registeredFunctions.length;a++)b.Objects.server.unregisterFunction(this.registeredFunctions[a])},apiSetupAvatar:function(a){a.imageLink&&this.set({imageLink:a.imageLink})}})})(Mibew,_); | ||||
| /* | ||||
|  This file is part of Mibew Messenger project. | ||||
|  http://mibew.org
 | ||||
| @ -116,8 +116,9 @@ MibewAPIChatInteraction=function(){this.obligatoryArguments={"*":{threadId:null, | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this))},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId});for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread, | ||||
| c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this,a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); | ||||
| (function(b,d,f){b.Collections.Messages=d.Collection.extend({model:b.Models.Message,initialize:function(){this.periodicallyCalled=[];this.periodicallyCalled.push(b.Objects.server.callFunctionsPeriodically(f.bind(this.updateMessagesFunctionBuilder,this),f.bind(this.updateMessages,this)))},finalize:function(){for(var a=0;a<this.periodicallyCalled.length;a++)b.Objects.server.stopCallFunctionsPeriodically(this.periodicallyCalled[a])},updateMessages:function(a){a.lastId&&b.Objects.Models.thread.set({lastId:a.lastId}); | ||||
| for(var c=[],e=0,d=a.messages.length;e<d;e++)c.push(new b.Models.Message(a.messages[e]));0<c.length&&this.add(c)},updateMessagesFunctionBuilder:function(){var a=b.Objects.Models.thread,c=b.Objects.Models.user;return[{"function":"updateMessages",arguments:{"return":{messages:"messages",lastId:"lastId"},references:{},threadId:a.get("id"),token:a.get("token"),lastId:a.get("lastId"),user:!c.get("isAgent")}}]},add:function(){var a=Array.prototype.slice.apply(arguments),a=d.Collection.prototype.add.apply(this, | ||||
| a);this.trigger("multiple:add");return a}})})(Mibew,Backbone,_); | ||||
| /* | ||||
|  This file is part of Mibew Messenger project. | ||||
|  http://mibew.org
 | ||||
| @ -256,11 +257,13 @@ c=""!=this.ui.message.val();c!=a.get("typing")&&a.set({typing:c})},setFocus:func | ||||
|  Copyright (c) 2005-2011 Mibew Messenger Community | ||||
|  License: http://mibew.org/license.php
 | ||||
| */ | ||||
| (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=a.Application;j.module("Chat",{startWithParent:!1}).addInitializer(function(d){var g=a.Objects,c=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(d.layoutsData.chat||{})});a.Objects.chatLayout=f;j.mainRegion.show(f);c.thread=new a.Models.Thread(d.thread);c.user=new a.Models.ChatUser(d.user);c.page=new a.Models.Page(d.page);var e=new a.Collections.Controls; | ||||
| c.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:d.links.mailLink}),e.add(b.sendMail));c.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:d.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:d.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); | ||||
| e.add(b.refresh);d.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:d.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); | ||||
| c.user.get("isAgent")||(c.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:c.avatar})));g.Collections.messages=new a.Collections.Messages;c.messageForm=new a.Models.MessageForm(d.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:c.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));c.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:c.sound}));g.server.callFunctionsPeriodically(function(){var b= | ||||
| a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))})})})(Mibew); | ||||
| (function(a){a.Objects.Models.Controls={};a.Objects.Models.Status={};var j=[],k=a.Application,l=k.module("Chat",{startWithParent:!1});l.addInitializer(function(c){var g=a.Objects,d=a.Objects.Models,b=a.Objects.Models.Controls,h=a.Objects.Models.Status,f=new a.Layouts.Chat({model:new Backbone.Model(c.layoutsData.chat||{})});a.Objects.chatLayout=f;k.mainRegion.show(f);d.thread=new a.Models.Thread(c.thread);d.user=new a.Models.ChatUser(c.user);d.page=new a.Models.Page(c.page);var e=new a.Collections.Controls; | ||||
| d.user.get("isAgent")||(b.userName=new a.Models.UserNameControl({weight:220}),e.add(b.userName),b.sendMail=new a.Models.SendMailControl({weight:200,link:c.links.mailLink}),e.add(b.sendMail));d.user.get("isAgent")&&(b.redirect=new a.Models.RedirectControl({weight:200,link:c.links.redirectLink}),e.add(b.redirect),b.history=new a.Models.HistoryControl({weight:180,link:c.links.historyLink}),e.add(b.history));b.sound=new a.Models.SoundControl({weight:160});e.add(b.sound);b.refresh=new a.Models.RefreshControl({weight:140}); | ||||
| e.add(b.refresh);c.links.sslLink&&(b.secureMode=new a.Models.SecureModeControl({weight:120,link:c.links.sslLink}),e.add(b.secureMode));b.close=new a.Models.CloseControl({weight:100});e.add(b.close);g.Collections.controls=e;f.controlsRegion.show(new a.Views.ControlsCollection({collection:e}));h.message=new a.Models.StatusMessage({hideTimeout:5E3});h.typing=new a.Models.StatusTyping({hideTimeout:5E3});g.Collections.status=new a.Collections.Status([h.message,h.typing]);f.statusRegion.show(new a.Views.StatusCollection({collection:g.Collections.status})); | ||||
| d.user.get("isAgent")||(d.avatar=new a.Models.Avatar,f.avatarRegion.show(new a.Views.Avatar({model:d.avatar})));g.Collections.messages=new a.Collections.Messages;d.messageForm=new a.Models.MessageForm(c.messageForm);f.messageFormRegion.show(new a.Views.MessageForm({model:d.messageForm}));f.messagesRegion.show(new a.Views.MessagesCollection({collection:g.Collections.messages}));d.sound=new a.Models.Sound;f.soundRegion.show(new a.Views.Sound({model:d.sound}));j.push(g.server.callFunctionsPeriodically(function(){var b= | ||||
| a.Objects.Models.thread,c=a.Objects.Models.user;return[{"function":"update",arguments:{"return":{typing:"typing",canPost:"canPost"},references:{},threadId:b.get("id"),token:b.get("token"),lastId:b.get("lastId"),typed:c.get("typing"),user:!c.get("isAgent")}}]},function(b){b.errorCode?a.Objects.Models.Status.message.setMessage(b.errorMessage||"refresh failed"):(b.typing&&a.Objects.Models.Status.typing.show(),a.Objects.Models.user.set({canPost:b.canPost||!1}))}))});l.addFinalizer(function(){a.Objects.chatLayout.close(); | ||||
| for(var c=0;c<j.length;c++)a.Objects.server.stopCallFunctionsPeriodically(j[c]);"undefined"!=typeof a.Objects.Models.avatar&&a.Objects.Models.avatar.finalize();a.Objects.Collections.messages.finalize();delete a.Objects.chatLayout;delete a.Objects.Models.thread;delete a.Objects.Models.user;delete a.Objects.Models.page;delete a.Objects.Models.avatar;delete a.Objects.Models.messageForm;delete a.Objects.Models.sound;delete a.Objects.Models.Controls;delete a.Objects.Models.Status;delete a.Objects.Collections.messages; | ||||
| delete a.Objects.Collections.controls;delete a.Objects.Collections.status})})(Mibew); | ||||
| /* | ||||
|  This file is part of Mibew Messenger project. | ||||
|  http://mibew.org
 | ||||
|  | ||||
| @ -24,13 +24,34 @@ | ||||
|              * Collection initializer. | ||||
|              */ | ||||
|             initialize: function() { | ||||
| 
 | ||||
|                 /** | ||||
|                  * Contains ids of periodically called functions | ||||
|                  * @type Array | ||||
|                  */ | ||||
|                 this.periodicallyCalled = []; | ||||
| 
 | ||||
|                 // Periodically try to get new messages
 | ||||
|                 Mibew.Objects.server.callFunctionsPeriodically( | ||||
|                     _.bind(this.updateMessagesFunctionBuilder, this), | ||||
|                     _.bind(this.updateMessages, this) | ||||
|                 this.periodicallyCalled.push( | ||||
|                     Mibew.Objects.server.callFunctionsPeriodically( | ||||
|                         _.bind(this.updateMessagesFunctionBuilder, this), | ||||
|                         _.bind(this.updateMessages, this) | ||||
|                     ) | ||||
|                 ); | ||||
|             }, | ||||
| 
 | ||||
|             /** | ||||
|              * Collection finalizer | ||||
|              */ | ||||
|             finalize: function() { | ||||
|                 // Stop call functions periodically
 | ||||
|                 for (var i = 0; i < this.periodicallyCalled.length; i++) { | ||||
|                     Mibew.Objects.server.stopCallFunctionsPeriodically( | ||||
|                         this.periodicallyCalled[i] | ||||
|                     ); | ||||
|                 } | ||||
|             }, | ||||
| 
 | ||||
|             /** | ||||
|              * Update messages if they are exist. | ||||
|              * @param args {Object} An object of passed arguments | ||||
|  | ||||
| @ -30,13 +30,32 @@ | ||||
|              * Model initializer. | ||||
|              */ | ||||
|             initialize: function() { | ||||
| 
 | ||||
|                 /** | ||||
|                  * Contain ids of registered by the model api functions | ||||
|                  * @type Array | ||||
|                  */ | ||||
|                 this.registeredFunctions = []; | ||||
| 
 | ||||
|                 // Register API function
 | ||||
|                 Mibew.Objects.server.registerFunction( | ||||
|                     'setupAvatar', | ||||
|                     _.bind(this.apiSetupAvatar, this) | ||||
|                 this.registeredFunctions.push( | ||||
|                     Mibew.Objects.server.registerFunction( | ||||
|                         'setupAvatar', | ||||
|                         _.bind(this.apiSetupAvatar, this) | ||||
|                     ) | ||||
|                 ); | ||||
|             }, | ||||
| 
 | ||||
|             // Model finalizer
 | ||||
|             finalize: function() { | ||||
|                 // Unregister api functions
 | ||||
|                 for(var i = 0; i < this.registeredFunctions.length; i++) { | ||||
|                     Mibew.Objects.server.unregisterFunction( | ||||
|                         this.registeredFunctions[i] | ||||
|                     ); | ||||
|                 } | ||||
|             }, | ||||
| 
 | ||||
|             /** | ||||
|              * Set avatar | ||||
|              * This is an API function. | ||||
|  | ||||
| @ -18,6 +18,12 @@ | ||||
|      */ | ||||
|     Mibew.Objects.Models.Status = {}; | ||||
| 
 | ||||
|     /** | ||||
|      * Contains ids of periodically called functions | ||||
|      * @type Array | ||||
|      */ | ||||
|     var periodicallyCalled = []; | ||||
| 
 | ||||
| 
 | ||||
|     // Create shortcut for Application object
 | ||||
|     var app = Mibew.Application; | ||||
| @ -43,6 +49,7 @@ | ||||
|         // Show layout at page
 | ||||
|         app.mainRegion.show(layout); | ||||
| 
 | ||||
| 
 | ||||
|         // Initialize Thread and User
 | ||||
|         models.thread = new Mibew.Models.Thread(options.thread); | ||||
|         models.user = new Mibew.Models.ChatUser(options.user); | ||||
| @ -177,49 +184,97 @@ | ||||
| 
 | ||||
|         // TODO: May be move it somewhere else
 | ||||
|         // Periodically call update function at the server side
 | ||||
|         objs.server.callFunctionsPeriodically( | ||||
|             function() { | ||||
|                 // Get thread and user objects
 | ||||
|                 var thread = Mibew.Objects.Models.thread; | ||||
|                 var user = Mibew.Objects.Models.user; | ||||
|         periodicallyCalled.push( | ||||
|             objs.server.callFunctionsPeriodically( | ||||
|                 function() { | ||||
|                     // Get thread and user objects
 | ||||
|                     var thread = Mibew.Objects.Models.thread; | ||||
|                     var user = Mibew.Objects.Models.user; | ||||
| 
 | ||||
|                 // Build functions list
 | ||||
|                 return [ | ||||
|                     { | ||||
|                         "function": "update", | ||||
|                         "arguments": { | ||||
|                             "return": { | ||||
|                                 'typing': 'typing', | ||||
|                                 'canPost': 'canPost' | ||||
|                             }, | ||||
|                             "references": {}, | ||||
|                             "threadId": thread.get('id'), | ||||
|                             "token": thread.get('token'), | ||||
|                             "lastId": thread.get('lastId'), | ||||
|                             "typed": user.get('typing'), | ||||
|                             "user": (! user.get('isAgent')) | ||||
|                     // Build functions list
 | ||||
|                     return [ | ||||
|                         { | ||||
|                             "function": "update", | ||||
|                             "arguments": { | ||||
|                                 "return": { | ||||
|                                     'typing': 'typing', | ||||
|                                     'canPost': 'canPost' | ||||
|                                 }, | ||||
|                                 "references": {}, | ||||
|                                 "threadId": thread.get('id'), | ||||
|                                 "token": thread.get('token'), | ||||
|                                 "lastId": thread.get('lastId'), | ||||
|                                 "typed": user.get('typing'), | ||||
|                                 "user": (! user.get('isAgent')) | ||||
|                             } | ||||
|                         } | ||||
|                     ] | ||||
|                 }, | ||||
|                 function(args) { | ||||
|                     // Check if there was an error
 | ||||
|                     if (args.errorCode) { | ||||
|                         Mibew.Objects.Models.Status.message.setMessage( | ||||
|                             args.errorMessage || 'refresh failed' | ||||
|                         ); | ||||
|                         return; | ||||
|                     } | ||||
|                 ] | ||||
|             }, | ||||
|             function(args) { | ||||
|                 // Check if there was an error
 | ||||
|                 if (args.errorCode) { | ||||
|                     Mibew.Objects.Models.Status.message.setMessage( | ||||
|                         args.errorMessage || 'refresh failed' | ||||
|                     ); | ||||
|                     return; | ||||
|                     // Update typing status
 | ||||
|                     if (args.typing) { | ||||
|                         Mibew.Objects.Models.Status.typing.show(); | ||||
|                     } | ||||
|                     // Update user
 | ||||
|                     Mibew.Objects.Models.user.set({ | ||||
|                         canPost: args.canPost || false | ||||
|                     }); | ||||
|                 } | ||||
|                 // Update typing status
 | ||||
|                 if (args.typing) { | ||||
|                     Mibew.Objects.Models.Status.typing.show(); | ||||
|                 } | ||||
|                 // Update user
 | ||||
|                 Mibew.Objects.Models.user.set({ | ||||
|                     canPost: args.canPost || false | ||||
|                 }); | ||||
|             } | ||||
|             ) | ||||
|         ); | ||||
| 
 | ||||
|     }); | ||||
| 
 | ||||
| 
 | ||||
|     // Add module finalizer
 | ||||
|     chat.addFinalizer(function() { | ||||
|         // Close layout
 | ||||
|         Mibew.Objects.chatLayout.close(); | ||||
| 
 | ||||
| 
 | ||||
|         // Stop call functions periodically
 | ||||
|         for (var i = 0; i < periodicallyCalled.length; i++) { | ||||
|             Mibew.Objects.server.stopCallFunctionsPeriodically( | ||||
|                 periodicallyCalled[i] | ||||
|             ); | ||||
|         } | ||||
| 
 | ||||
|         // Run models finalizers
 | ||||
|         // TODO: may be automate this process
 | ||||
|         // Finalize avatar model
 | ||||
|         if (typeof Mibew.Objects.Models.avatar != 'undefined') { | ||||
|             Mibew.Objects.Models.avatar.finalize(); | ||||
|         } | ||||
| 
 | ||||
|         // Run collections finalizers
 | ||||
|         // Finalize messages collection
 | ||||
|         Mibew.Objects.Collections.messages.finalize(); | ||||
| 
 | ||||
|         // Clean up memory
 | ||||
|         // Delete layout
 | ||||
|         delete Mibew.Objects.chatLayout; | ||||
| 
 | ||||
|         // Delete models
 | ||||
|         delete Mibew.Objects.Models.thread; | ||||
|         delete Mibew.Objects.Models.user; | ||||
|         delete Mibew.Objects.Models.page; | ||||
|         delete Mibew.Objects.Models.avatar; | ||||
|         delete Mibew.Objects.Models.messageForm; | ||||
|         delete Mibew.Objects.Models.sound; | ||||
|         delete Mibew.Objects.Models.Controls; | ||||
|         delete Mibew.Objects.Models.Status; | ||||
| 
 | ||||
|         // Delete collections
 | ||||
|         delete Mibew.Objects.Collections.messages; | ||||
|         delete Mibew.Objects.Collections.controls; | ||||
|         delete Mibew.Objects.Collections.status; | ||||
|     }); | ||||
| 
 | ||||
| })(Mibew); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user