(function(){"use strict";u8.Crm.Favorites=function(){var e=u8.services.events;e.bind("u8.services.crud.onCreate",this._onCrudCreate.bind(this),"favorites").bind("u8.services.crud.onUpdate",this._onCrudUpdate.bind(this),"favorites").bind("u8.services.crud.onDelete",this._onCrudDelete.bind(this),"favorites");this.commandBindings={"Favorites/Remove":this._onCommandFavoritesRemove.bind(this),"Favorites/Add":this._onCommandFavoritesAdd.bind(this),"Favorites/EditQuickLinks":this._openQuickLinksEditor.bind(this)}};$.extend(u8.Crm.Favorites.prototype,{add:function(e,i){if(!this.isFavorable(e)){i(this,{isFavorable:false});return}u8.request("FavoritesChannel.Add",{uid:e},function(s,t){var n=t.response?t.response.favorite:null;var r=n?"{0} {TXT_favorites_text_1}":"{0} {TXT_favorites_text_3}";r.format({0:u8.services.schema.idToLabel(e.infoAreaId)},function(e){u8.services.notification.info(e)});if(n){this._onAdd(e,n)}if(i){i(this,t)}}.bind(this))},remove:function(e,i){if(!this.isFavorable(e)){i(this,{isFavorable:false});return}u8.request("FavoritesChannel.Remove",{uid:e},function(s,t){var n=t.error?false:true;var r=n?"{0} {TXT_favorites_text_2}":"{0} {TXT_favorites_text_4}";r.format({0:u8.services.schema.idToLabel(e.infoAreaId)},function(e){u8.services.notification.info(e)});if(n){this._onRemove(e)}if(i){i(this,t)}}.bind(this))},isFavorite:function(e,i){if(!this.isFavorable(e)){i(this,{isFavorable:false});return}u8.request("FavoritesChannel.IsFavorite",{uid:e},function(e,s){s=s.error?s:s.response;s.isFavorable=true;i(this,s)}.bind(this),true)},isFavorable:function(e){var i=u8.session.webConfigValues.getStringArray("Favorites.InfoAreas",[]);return i.contains(e.infoAreaId)},dispose:function(){if(this._isDisposed){return}var e=u8.services.events;e.unbind("u8.services.crud.onCreate","favorites").unbind("u8.services.crud.onUpdate","favorites").unbind("u8.services.crud.onDelete","favorites");this._isDisposed=true},readTableCaptions:function(e,i){$.extend(e,{__type:"ReadTableCaptionsFavorites"});u8.services.tableCaptions.readBatch({items:[e]},function(e,s){if(s.tableCaptions){i(this,{tableCaptions:s.tableCaptions})}else{i(this,{error:s.error})}})},_onAdd:function(e,i){var s={uid:e,reason:"AddFavorite"};if(typeof i!="undefined"){s.favorite=i}u8.services.events.raise("u8.services.favorites.onChange",this,s)},_onRemove:function(e){var i={uid:e,reason:"RemoveFavorite"};u8.services.events.raise("u8.services.favorites.onChange",this,i)},_onCrudCreate:function(e,i){var s=i.businessObject,t=s&&s.uid;if(!t||t.infoAreaId!="FV"){return}this._onAdd(s.uid)},_onCrudUpdate:function(e,i){var s=i.businessObject,t=s&&s.uid;if(t&&this.isFavorable(t)){var n={uid:t,reason:"UpdateRecord"};u8.services.events.raise("u8.services.favorites.onChange",this,n)}},_onCrudDelete:function(e,i){if(i.uid&&i.uid.infoAreaId=="FV"){this._onRemove(i.uid)}else if(i.uid&&this.isFavorable(i.uid)){var s={uid:i.uid,reason:"RemoveRecord"};u8.services.events.raise("u8.services.favorites.onChange",this,s)}},_onCommandFavoritesRemove:function(e,i,s){this.remove(s.uid)},_onCommandFavoritesAdd:function(e,i,s){this.add(s.uid)},_openQuickLinksEditor:function(){u8.using({types:["QuickLinksEditor"]},function(){u8.Crm.Widgets.QuickLinksEditor.openPopup({widgetOptions:{onSave:e.bind(this)}})}.bind(this));function e(){var e={reason:"QuickLinksChange"};u8.services.events.raise("u8.services.favorites.onChange",this,e)}}});u8.services.add({name:"favorites",scope:"Login",ctor:u8.Crm.Favorites});u8.services.menus.registerMenuItem("D_Favorites",{create:function(i,s){var t=s.menuItem,n=i&&i.executeOptions&&i.executeOptions.arguments,r=n&&n.uid;if(!r||!u8.services.favorites.isFavorable(r)){t.visible=false;t.update();return}u8.using({texts:"favorites_text"},function(){u8.services.favorites.isFavorite(r,function(i,s){if(s.error){return}if(s.isFavorite){t.action="A_FromFavorites";t.label=$t("TXT_favorites_text_9");t.onAction=e}else{t.action="A_ToFavorites";t.label=$t("TXT_favorites_text_8");t.onAction=e}t.update()})})}});function e(e,i){e.update({menuItem:this,dynamic:true})}})();