(function(){"use strict";function AddressBook(futureAddressBookData){if(typeof futureAddressBookData.then!=="function"){this.init(futureAddressBookData);if(this.name&&!this.id){var newAddressBookData=AddressBook.$$resource.create("createFolder",this.name);this.$unwrap(newAddressBookData)}else if(this.id){this.$acl=new AddressBook.$$Acl("Contacts/"+this.id)}}else{this.$unwrap(futureAddressBookData)}}AddressBook.$factory=["$q","$timeout","$log","sgSettings","Resource","Card","Acl","Preferences",function($q,$timeout,$log,Settings,Resource,Card,Acl,Preferences){angular.extend(AddressBook,{$q:$q,$timeout:$timeout,$log:$log,$$resource:new Resource(Settings.activeUser("folderURL")+"Contacts",Settings.activeUser()),$Card:Card,$$Acl:Acl,$Preferences:Preferences,$query:{search:"name_or_address",value:"",sort:"c_cn",asc:1},activeUser:Settings.activeUser(),selectedFolder:null,$refreshTimeout:null});Preferences.ready().then(function(){if(Preferences.settings.Contact.SortingState){AddressBook.$query.sort=Preferences.settings.Contact.SortingState[0];AddressBook.$query.asc=parseInt(Preferences.settings.Contact.SortingState[1])}});return AddressBook}];try{angular.module("SOGo.ContactsUI")}catch(e){angular.module("SOGo.ContactsUI",["SOGo.Common","SOGo.PreferencesUI"])}angular.module("SOGo.ContactsUI").factory("AddressBook",AddressBook.$factory);AddressBook.$filterAll=function(search,options,excludedCards){var params={search:search};if(!search){AddressBook.$cards=[];return AddressBook.$q.when(AddressBook.$cards)}if(angular.isUndefined(AddressBook.$cards)){AddressBook.$cards=[]}else if(AddressBook.$query.value==search){return AddressBook.$q.when(AddressBook.$cards)}AddressBook.$query.value=search;angular.extend(params,options);return AddressBook.$$resource.fetch(null,"allContactSearch",params).then(function(response){var results,card,index,compareIds=function(data){return this.id==data.id};if(excludedCards){results=_.filter(response.contacts,function(data){return _.isUndefined(_.find(excludedCards,compareIds,data))})}else{results=response.contacts}for(index=AddressBook.$cards.length-1;index>=0;index--){card=AddressBook.$cards[index];if(_.isUndefined(_.find(results,compareIds,card))){AddressBook.$cards.splice(index,1)}}_.each(results,function(data,index){if(_.isUndefined(_.find(AddressBook.$cards,compareIds,data))){var card=new AddressBook.$Card(data,search);AddressBook.$cards.splice(index,0,card)}});AddressBook.$log.debug(AddressBook.$cards);return AddressBook.$cards})};AddressBook.$add=function(addressbook){var list,sibling,i;list=addressbook.isSubscription?this.$subscriptions:this.$addressbooks;sibling=_.find(list,function(o){return addressbook.id=="personal"||o.id!="personal"&&o.name.localeCompare(addressbook.name)===1});i=sibling?_.indexOf(_.pluck(list,"id"),sibling.id):1;list.splice(i,0,addressbook)};AddressBook.$findAll=function(data){var _this=this;if(data){this.$addressbooks=[];this.$subscriptions=[];this.$remotes=[];angular.forEach(data,function(o,i){var addressbook=new AddressBook(o);if(addressbook.isRemote)_this.$remotes.push(addressbook);else if(addressbook.isSubscription)_this.$subscriptions.push(addressbook);else _this.$addressbooks.push(addressbook)})}return _.union(this.$addressbooks,this.$subscriptions,this.$remotes)};AddressBook.$find=function(addressbookId){var futureAddressBookData=AddressBook.$Preferences.ready().then(function(){return AddressBook.$$resource.fetch(addressbookId,"view",AddressBook.$query)});return new AddressBook(futureAddressBookData)};AddressBook.$subscribe=function(uid,path){var _this=this;return AddressBook.$$resource.userResource(uid).fetch(path,"subscribe").then(function(addressbookData){var addressbook=new AddressBook(addressbookData);if(_.isUndefined(_.find(_this.$subscriptions,function(o){return o.id==addressbookData.id}))){AddressBook.$add(addressbook)}return addressbook})};AddressBook.prototype.init=function(data,options){if(!this.$cards){this.$isLoading=true;this.$cards=[];this.cards=[]}angular.extend(this,data);this.isOwned=AddressBook.activeUser.isSuperUser||this.owner==AddressBook.activeUser.login;this.isSubscription=!this.isRemote&&this.owner!=AddressBook.activeUser.login};AddressBook.prototype.$id=function(){if(this.id){return AddressBook.$q.when(this.id)}else{return this.$futureAddressBookData.then(function(addressbook){return addressbook.id})}};AddressBook.prototype.isSelectedCard=function(cardId){return this.selectedCard==cardId};AddressBook.prototype.$selectedCount=function(){var count;count=0;if(this.cards){count=_.filter(this.cards,function(card){return card.selected}).length}return count};AddressBook.prototype.$startRefreshTimeout=function(){var _this=this;if(AddressBook.$refreshTimeout)AddressBook.$timeout.cancel(AddressBook.$refreshTimeout);AddressBook.$Preferences.ready().then(function(){var refreshViewCheck=AddressBook.$Preferences.defaults.SOGoRefreshViewCheck;if(refreshViewCheck&&refreshViewCheck!="manually"){var f=angular.bind(_this,AddressBook.prototype.$reload);AddressBook.$refreshTimeout=AddressBook.$timeout(f,refreshViewCheck.timeInterval()*1e3)}})};AddressBook.prototype.$reload=function(){var _this=this;this.$startRefreshTimeout();return this.$filter()};AddressBook.prototype.$filter=function(search,options,excludedCards){var _this=this;if(!options||!options.dry)this.$isLoading=true;return AddressBook.$Preferences.ready().then(function(){if(options){angular.extend(AddressBook.$query,options);if(options.dry){if(!search){_this.$cards=[];return AddressBook.$q.when(_this.$cards)}else if(AddressBook.$query.value==search){return AddressBook.$q.when(_this.$cards)}}}if(angular.isDefined(search))AddressBook.$query.value=search;return _this.$id().then(function(addressbookId){return AddressBook.$$resource.fetch(addressbookId,"view",AddressBook.$query)}).then(function(response){var results,cards,card,index,compareIds=function(data){return _this.id==data.id};if(options&&options.dry){cards=_this.$cards}else{cards=_this.cards}if(excludedCards){results=_.filter(response.cards,function(card){return _.isUndefined(_.find(excludedCards,compareIds,card))})}else{results=response.cards}for(index=cards.length-1;index>=0;index--){card=cards[index];if(_.isUndefined(_.find(results,compareIds,card))){cards.splice(index,1)}}_.each(results,function(data,index){if(_.isUndefined(_.find(cards,compareIds,data))){var card=new AddressBook.$Card(data,search);cards.splice(index,0,card)}});_.each(results,function(data,index){var oldIndex,removedCards;if(cards[index].id!=data.id){oldIndex=_.findIndex(cards,compareIds,data);removedCards=cards.splice(oldIndex,1);cards.splice(index,0,removedCards[0])}});_this.$isLoading=false;return cards})})};AddressBook.prototype.$rename=function(name){var i=_.indexOf(_.pluck(AddressBook.$addressbooks,"id"),this.id);this.name=name;AddressBook.$addressbooks.splice(i,1);AddressBook.$add(this);return this.$save()};AddressBook.prototype.$delete=function(){var _this=this,d=AddressBook.$q.defer(),list,promise;if(this.isSubscription){promise=AddressBook.$$resource.fetch(this.id,"unsubscribe");list=AddressBook.$subscriptions}else{promise=AddressBook.$$resource.remove(this.id);list=AddressBook.$addressbooks}promise.then(function(){var i=_.indexOf(_.pluck(list,"id"),_this.id);list.splice(i,1);d.resolve()},d.reject);return d.promise};AddressBook.prototype.$deleteCards=function(cards){var uids=_.map(cards,function(card){return card.id});var _this=this;return AddressBook.$$resource.post(this.id,"batchDelete",{uids:uids}).then(function(){_this.cards=_.difference(_this.cards,cards)})};AddressBook.prototype.$copyCards=function(cards,folder){var uids=_.map(cards,function(card){return card.id});return AddressBook.$$resource.post(this.id,"copy",{uids:uids,folder:folder})};AddressBook.prototype.$save=function(){return AddressBook.$$resource.save(this.id,this.$omit()).then(function(data){return data})};AddressBook.prototype.$getCard=function(cardId){var _this=this;return this.$id().then(function(addressbookId){var fullCard,cachedCard=_.find(_this.cards,function(data){return cardId==data.id});if(cachedCard&&cachedCard.$futureCardData)return cachedCard;fullCard=AddressBook.$Card.$find(addressbookId,cardId);fullCard.$id().then(function(cardId){if(cachedCard)angular.extend(cachedCard,fullCard)});return fullCard})};AddressBook.prototype.$unwrap=function(futureAddressBookData){var _this=this;this.$futureAddressBookData=futureAddressBookData.then(function(data){return AddressBook.$timeout(function(){angular.forEach(AddressBook.$findAll(),function(o,i){if(o.id==data.id){angular.extend(_this,o)}});_this.init(data);angular.forEach(_this.cards,function(o,i){_this.cards[i]=new AddressBook.$Card(o)});_this.$acl=new AddressBook.$$Acl("Contacts/"+_this.id);_this.$startRefreshTimeout();_this.$isLoading=false;return _this})},function(data){_this.isError=true;if(angular.isObject(data)){AddressBook.$timeout(function(){angular.extend(_this,data)})}})};AddressBook.prototype.$omit=function(){var addressbook={};angular.forEach(this,function(value,key){if(key!="constructor"&&key!="cards"&&key[0]!="$"){addressbook[key]=value}});return addressbook}})();(function(){"use strict";function Card(futureCardData,partial){if(typeof futureCardData.then!=="function"){this.init(futureCardData,partial);if(this.pid&&!this.id){var newCardData=Card.$$resource.newguid(this.pid);this.$unwrap(newCardData);this.isNew=true}}else{this.$unwrap(futureCardData)}}Card.$TEL_TYPES=["work","home","cell","fax","pager"];Card.$EMAIL_TYPES=["work","home","pref"];Card.$URL_TYPES=["work","home","pref"];Card.$ADDRESS_TYPES=["work","home"];Card.$factory=["$timeout","sgSettings","Resource","Preferences","Gravatar",function($timeout,Settings,Resource,Preferences,Gravatar){angular.extend(Card,{$$resource:new Resource(Settings.activeUser("folderURL")+"Contacts",Settings.activeUser()),$timeout:$timeout,$gravatar:Gravatar});Preferences.ready().then(function(){if(Preferences.defaults.SOGoContactsCategories){Card.$categories=Preferences.defaults.SOGoContactsCategories}if(Preferences.defaults.SOGoAlternateAvatar)Card.$alternateAvatar=Preferences.defaults.SOGoAlternateAvatar});return Card}];try{angular.module("SOGo.ContactsUI")}catch(e){angular.module("SOGo.ContactsUI",["SOGo.Common","SOGo.PreferencesUI"])}angular.module("SOGo.ContactsUI").factory("Card",Card.$factory);Card.$find=function(addressbookId,cardId){var futureCardData=this.$$resource.fetch([addressbookId,cardId].join("/"),"view");if(cardId)return new Card(futureCardData);return Card.$unwrapCollection(futureCardData)};Card.filterCategories=function(query){var re=new RegExp(query,"i");return _.filter(Card.$categories,function(category){return category.search(re)!=-1})};Card.$unwrapCollection=function(futureCardData){var collection={};collection.$futureCardData=futureCardData;futureCardData.then(function(cards){Card.$timeout(function(){angular.forEach(cards,function(data,index){collection[data.id]=new Card(data)})})});return collection};Card.prototype.init=function(data,partial){this.refs=[];angular.extend(this,data);if(!this.$$fullname)this.$$fullname=this.$fullname();if(!this.$$email)this.$$email=this.$preferredEmail(partial);if(!this.$$image)this.$$image=this.image||Card.$gravatar(this.$preferredEmail(partial),32,Card.$alternateAvatar,{no_404:true});this.selected=false;this.empty=" "};Card.prototype.$id=function(){return this.$futureCardData.then(function(data){return data.id})};Card.prototype.$save=function(){var _this=this,action="saveAsContact";if(this.c_component=="vlist")action="saveAsList";return Card.$$resource.save([this.pid,this.id||"_new_"].join("/"),this.$omit(),{action:action}).then(function(data){_this.$shadowData=_this.$omit(true);return data})};Card.prototype.$delete=function(attribute,index){if(attribute){if(index>-1&&this[attribute].length>index){this[attribute].splice(index,1)}else delete this[attribute]}else{return Card.$$resource.remove([this.pid,this.id].join("/"))}};Card.prototype.$fullname=function(){var fn=this.c_cn||"",names;if(fn.length===0){names=[];if(this.c_givenname&&this.c_givenname.length>0)names.push(this.c_givenname);if(this.nickname&&this.nickname.length>0)names.push(""+this.nickname+"");if(this.c_sn&&this.c_sn.length>0)names.push(this.c_sn);if(names.length>0)fn=names.join(" ");else if(this.c_org&&this.c_org.length>0){fn=this.c_org}else if(this.emails&&this.emails.length>0){fn=_.find(this.emails,function(i){return i.value!==""}).value}else if(this.c_cn&&this.c_cn.length>0){fn=this.c_cn}}return fn};Card.prototype.$description=function(){var description=[];if(this.title)description.push(this.title);if(this.role)description.push(this.role);if(this.orgUnits&&this.orgUnits.length>0)_.forEach(this.orgUnits,function(unit){if(unit.value!=="")description.push(unit.value)});if(this.c_org)description.push(this.c_org);if(this.description)description.push(this.description);return description.join(", ")};Card.prototype.$preferredEmail=function(partial){var email,re;if(partial){re=new RegExp(partial,"i");email=_.find(this.emails,function(o){return re.test(o.value)})}if(email){email=email.value}else{email=_.find(this.emails,function(o){return o.type=="pref"});if(email){email=email.value}else if(this.emails&&this.emails.length){email=this.emails[0].value}else{email=""}}return email};Card.prototype.$shortFormat=function(partial){var fullname=[this.$$fullname],email=this.$preferredEmail(partial);if(email&&email!=this.$$fullname)fullname.push(" <"+email+">");return fullname.join(" ")};Card.prototype.$birthday=function(){if(this.birthday){return[this.birthday.getFullYear(),this.birthday.getMonth()+1,this.birthday.getDate()].join("/")}return""};Card.prototype.$isCard=function(){return this.c_component=="vcard"};Card.prototype.$isList=function(){return this.c_component=="vlist"};Card.prototype.$addOrgUnit=function(orgUnit){if(angular.isUndefined(this.orgUnits)){this.orgUnits=[{value:orgUnit}]}else{for(var i=0;i";return"["+desc+"]"}})(); //# sourceMappingURL=Contacts.services.js.map