sogo/UI/WebServerResources/js/Contacts.services.js
2019-11-22 01:17:37 -05:00

2 lines
19 KiB
JavaScript

!function(){"use strict";function l(e){if("function"!=typeof e.then)if(this.init(e),this.name&&!this.id){var t=l.$$resource.create("createFolder",this.name);this.$unwrap(t),this.acls={objectEditor:1,objectCreator:1,objectEraser:1}}else this.id&&(this.$acl=new l.$$Acl("Contacts/"+this.id));else this.$unwrap(e)}l.$factory=["$q","$timeout","$log","sgSettings","sgAddressBook_PRELOAD","Resource","Card","Acl","Preferences",function(e,t,i,r,s,n,o,a,d){return angular.extend(l,{$q:e,$timeout:t,$log:i,PRELOAD:s,$$resource:new n(r.activeUser("folderURL")+"Contacts",r.activeUser()),$Card:o,$$Acl:a,$Preferences:d,$query:{value:"",sort:"c_cn",asc:1},activeUser:r.activeUser(),$addressbooks:[],$subscriptions:[],$remotes:[],selectedFolder:null,$refreshTimeout:null}),d.settings.Contact.SortingState&&(l.$query.sort=d.settings.Contact.SortingState[0],l.$query.asc=parseInt(d.settings.Contact.SortingState[1])),l}];try{angular.module("SOGo.ContactsUI")}catch(e){angular.module("SOGo.ContactsUI",["SOGo.Common","SOGo.PreferencesUI"])}angular.module("SOGo.ContactsUI").constant("sgAddressBook_PRELOAD",{LOOKAHEAD:50,SIZE:100}).factory("AddressBook",l.$factory),l.$filterAll=function(n,e,o){var t={search:n};return n?(angular.isUndefined(l.$cards)&&(l.$cards=[]),angular.extend(t,e),l.$$resource.fetch(null,"allContactSearch",t).then(function(e){function r(e){return this.id==e.id}var t,i,s;for(t=o?_.filter(e.contacts,function(e){return _.isUndefined(_.find(o,_.bind(r,e)))}):e.contacts,s=l.$cards.length-1;0<=s;s--)i=l.$cards[s],_.isUndefined(_.find(t,_.bind(r,i)))&&l.$cards.splice(s,1);return _.forEach(t,function(e,t){if(_.isUndefined(_.find(l.$cards,_.bind(r,e)))){var i=new l.$Card(_.mapKeys(e,function(e,t){return t.toLowerCase()}),n);l.$cards.splice(t,0,i)}}),l.$log.debug(l.$cards),l.$cards})):(l.$cards=[],l.$q.when(l.$cards))},l.$add=function(t){var e,i,r;e=t.isSubscription?this.$subscriptions:this.$addressbooks,r=(i=_.find(e,function(e){return"personal"==t.id||"personal"!=e.id&&1===e.name.localeCompare(t.name)}))?_.indexOf(_.map(e,"id"),i.id):1,e.splice(r,0,t)},l.$findAll=function(e){var r=this;if(e&&e.length)this.$addressbooks.splice(0,this.$addressbooks.length),this.$subscriptions.splice(0,this.$subscriptions.length),this.$remotes.splice(0,this.$remotes.length),angular.forEach(e,function(e,t){var i=new l(e);i.isRemote?r.$remotes.push(i):i.isSubscription?r.$subscriptions.push(i):r.$addressbooks.push(i)});else if(angular.isArray(e))return l.$$resource.fetch("addressbooksList").then(function(e){return l.$findAll(e.addressbooks)});return _.union(this.$addressbooks,this.$subscriptions,this.$remotes)},l.$subscribe=function(e,t){var i=this;return l.$$resource.userResource(e).fetch(t,"subscribe").then(function(t){var e=new l(t);return _.isUndefined(_.find(i.$subscriptions,function(e){return e.id==t.id}))&&l.$add(e),e})},l.$reloadAll=function(){var r=this;return l.$$resource.fetch("addressbooksList").then(function(e){_.forEach(e.addressbooks,function(t){var e,i;e=t.isRemote?r.$remotes:t.owner!=l.activeUser.login?r.$subscriptions:r.$addressbooks,(i=_.find(e,function(e){return e.id==t.id}))&&i.init(t)})})},l.prototype.init=function(e,t){var i=this;this.$$cards||(this.$$cards=[]),this.idsMap={},this.$cards=[],angular.forEach(e,function(e,t){"headers"!=t&&"cards"!=t&&(i[t]=e)}),this.isOwned=l.activeUser.isSuperUser||this.owner==l.activeUser.login,this.isSubscription=!this.isRemote&&this.owner!=l.activeUser.login},l.prototype.$id=function(){return this.id?l.$q.when(this.id):this.$futureAddressBookData.then(function(e){return e?e.id:l.$q.reject()})},l.prototype.getLength=function(){return this.$cards.length},l.prototype.getItemAtIndex=function(e){var t;return!this.$isLoading&&0<=e&&e<this.$cards.length&&(t=this.$cards[e],this.$lastVisibleIndex=Math.max(0,e-3),this.$loadCard(t))?t:null},l.prototype.$loadCard=function(e){var t,i,r,s,n=e.id,o=this.idsMap[n],a=this.$cards.length,d=!1;if(angular.isUndefined(this.ids)&&e.id)d=!0;else if(angular.isDefined(o)&&o<this.$cards.length&&(e.$loaded!=l.$Card.STATUS.NOT_LOADED&&(d=!0),t=Math.min(o+l.PRELOAD.LOOKAHEAD,a-1),this.$cards[t].$loaded!=l.$Card.STATUS.NOT_LOADED?(i=Math.max(o-l.PRELOAD.LOOKAHEAD,0),this.$cards[i].$loaded!=l.$Card.STATUS.LOADED&&(t=o,o=Math.max(o-l.PRELOAD.SIZE,0))):t=Math.min(o+l.PRELOAD.SIZE,a-1),this.$cards[o].$loaded==l.$Card.STATUS.NOT_LOADED||this.$cards[t].$loaded==l.$Card.STATUS.NOT_LOADED)){for(r=[];o<t&&o<a;o++)this.$cards[o].$loaded!=l.$Card.STATUS.NOT_LOADED?t++:(r.push(this.$cards[o].id),this.$cards[o].$loaded=l.$Card.STATUS.LOADING);l.$log.debug("Loading Ids "+r.join(" ")+" ("+r.length+" cards)"),0<r.length&&(s=l.$$resource.post(this.id,"headers",{ids:r}),this.$unwrapHeaders(s))}return d},l.prototype.hasSelectedCard=function(){return angular.isDefined(this.selectedCard)},l.prototype.isSelectedCard=function(e){return this.hasSelectedCard()&&this.selectedCard==e},l.prototype.$selectedCard=function(){var t=this;return _.find(this.$cards,function(e){return e.id==t.selectedCard})},l.prototype.$selectedCardIndex=function(){return _.indexOf(_.map(this.$cards,"id"),this.selectedCard)},l.prototype.$selectedCards=function(){return _.filter(this.$cards,function(e){return e.selected})},l.prototype.$selectedCount=function(){var e;return e=0,this.$cards&&(e=_.filter(this.$cards,function(e){return e.selected}).length),e},l.prototype.$startRefreshTimeout=function(){l.$refreshTimeout&&l.$timeout.cancel(l.$refreshTimeout);var e=l.$Preferences.defaults.SOGoRefreshViewCheck;if(e&&"manually"!=e){var t=angular.bind(this,l.prototype.$reload);l.$refreshTimeout=l.$timeout(t,1e3*e.timeInterval())}},l.prototype.$reload=function(){return this.$startRefreshTimeout(),this.$filter()},l.prototype.$filter=function(u,e,c){var t,h=this,i=e&&e.dry;return i?t={value:"",sort:"c_cn",asc:1}:(this.$isLoading=!0,t=l.$query,this.isRemote||(t.partial=1)),e&&(angular.extend(t,e),i&&!u)?(h.$$cards=[],l.$q.when(h.$$cards)):(angular.isDefined(u)&&(t.value=u),h.$id().then(function(d){var e=l.$$resource.post(d,"view",t);return i?e.then(function(e){function s(e){return this==e.id}var t,i,r,n,o,a=h.$$cards;for(e.headers&&(n=_.invokeMap(e.headers[0],"toLowerCase"),o=n.indexOf("id"),e.headers.splice(0,1),t=_.map(e.headers,function(e){return e[o]})),e.ids&&(t=c?_.filter(e.ids,function(e){return _.isUndefined(_.find(c,_.bind(s,e)))}):e.ids),r=a.length-1;0<=r;r--)i=a[r],_.isUndefined(_.find(t,_.bind(s,i.id)))&&a.splice(r,1);return _.forEach(t,function(e,t){if(_.isUndefined(_.find(a,_.bind(s,e)))){var i=new l.$Card({pid:d,id:e},u);a.splice(t,0,i)}}),_.forEach(t,function(e,t){var i,r;a[t].id!=e&&(i=_.findIndex(a,_.bind(s,e)),r=a.splice(i,1),a.splice(t,0,r[0]))}),_.forEach(e.headers,function(e){var t,i=_.findIndex(a,_.bind(s,e[o]));-1<i&&(t=_.zipObject(n,e),a[i].init(t,u))}),a}):h.$unwrap(e)}))},l.prototype.$rename=function(e){var t,i,r=this;return i=this.isSubscription?l.$subscriptions:l.$addressbooks,t=_.indexOf(_.map(i,"id"),this.id),this.$save().then(function(){i.splice(t,1),r.name=e,l.$add(r)})},l.prototype.$delete=function(){var t,e,i=this,r=l.$q.defer();return t=this.isSubscription?(e=l.$$resource.fetch(this.id,"unsubscribe"),l.$subscriptions):(e=l.$$resource.remove(this.id),l.$addressbooks),e.then(function(){var e=_.indexOf(_.map(t,"id"),i.id);t.splice(e,1),r.resolve()},r.reject),r.promise},l.prototype.$_deleteCards=function(r){var s=this;_.forEachRight(this.$cards,function(t,e){var i=_.findIndex(r,function(e){return t.id==e});-1<i?(r.splice(i,1),delete s.idsMap[t.id],s.isSelectedCard(t.id)&&delete s.selectedCard,s.$cards.splice(e,1)):s.idsMap[t.id]-=r.length})},l.prototype.$deleteCards=function(e){var t=this,i=_.map(e,"id");return l.$$resource.post(this.id,"batchDelete",{uids:i}).then(function(){t.$_deleteCards(i)})},l.prototype.$copyCards=function(e,t){var i=_.map(e,"id");return l.$$resource.post(this.id,"copy",{uids:i,folder:t})},l.prototype.$moveCards=function(e,t){var i,r=this;return i=_.map(e,"id"),l.$$resource.post(this.id,"move",{uids:i,folder:t}).then(function(){return r.$_deleteCards(i)})},l.prototype.$save=function(){return l.$$resource.save(this.id,this.$omit()).then(function(e){return e})},l.prototype.exportCards=function(e){var t,i,r=null;return t={type:"application/octet-stream",filename:this.name+".ldif"},e&&(i=_.filter(this.$cards,function(e){return e.selected}),r={uids:_.map(i,"id")}),r?l.$$resource.download(this.id,"export",r,t):l.$$resource.open(this.id,"export",r,t)},l.prototype.$unwrap=function(e){var o=this;this.$isLoading=!0,this.$futureAddressBookData=e.then(function(i){var n=_.map(o.$selectedCards(),"id");return l.$timeout(function(){var r;return(!i.ids||o.$topIndex>i.ids.length-1)&&(o.$topIndex=0),angular.forEach(l.$findAll(),function(e,t){e.id==i.id&&angular.extend(o,e)}),o.init(i),o.ids&&(l.$log.debug("unwrapping "+o.ids.length+" cards"),_.reduce(o.ids,function(e,t,i){var r,s={pid:o.id,id:t};return o.idsMap[s.id]=i,(r=new l.$Card(s)).selected=-1<n.indexOf(r.id),e.push(r),e},o.$cards)),i.headers&&(r=_.invokeMap(i.headers[0],"toLowerCase"),i.headers.splice(0,1),o.ids?_.forEach(i.headers,function(e){var t=_.zipObject(r,e),i=o.idsMap[t.id];o.$cards[i].init(t)}):(o.$cards=[],angular.forEach(i.headers,function(e){var t,i=_.zipObject(r,e);angular.extend(i,{pid:o.id}),(t=new l.$Card(i)).selected=-1<n.indexOf(t.id),o.$cards.push(t)}))),o.$acl=new l.$$Acl("Contacts/"+o.id),o.$startRefreshTimeout(),o.$isLoading=!1,l.$log.debug("addressbook "+o.id+" ready"),o})},function(e){o.isError=!0,angular.isObject(e)&&l.$timeout(function(){angular.extend(o,e)})})},l.prototype.$unwrapHeaders=function(e){var r=this;e.then(function(e){l.$timeout(function(){var t,i;0<e.length&&(t=_.invokeMap(e[0],"toLowerCase"),e.splice(0,1),_.forEach(e,function(e){e=_.zipObject(t,e),i=r.idsMap[e.id],angular.isDefined(i)&&r.$cards[i].init(e)}))})})},l.prototype.$omit=function(){var i={};return angular.forEach(this,function(e,t){"constructor"!=t&&"acls"!=t&&"ids"!=t&&"idsMap"!=t&&"urls"!=t&&"$"!=t[0]&&(i[t]=e)}),i}}(),function(){"use strict";function a(e,t){if("function"!=typeof e.then){if(this.init(e,t),this.pid&&!this.id){var i=a.$$resource.newguid(this.pid);this.$unwrap(i),this.isNew=!0}}else this.$unwrap(e)}a.$TEL_TYPES=["work","home","cell","fax","pager"],a.$EMAIL_TYPES=["work","home","pref"],a.$URL_TYPES=["work","home","pref"],a.$ADDRESS_TYPES=["work","home"],a.$factory=["$q","$timeout","sgSettings","sgCard_STATUS","encodeUriFilter","Resource","Preferences",function(e,t,i,r,s,n,o){return angular.extend(a,{STATUS:r,encodeUri:s,$$resource:new n(i.activeUser("folderURL")+"Contacts",i.activeUser()),$q:e,$timeout:t,$Preferences:o}),o.defaults.SOGoContactsCategories&&(a.$categories=o.defaults.SOGoContactsCategories),o.defaults.SOGoAlternateAvatar&&(a.$alternateAvatar=o.defaults.SOGoAlternateAvatar),a}];try{angular.module("SOGo.ContactsUI")}catch(e){angular.module("SOGo.ContactsUI",["SOGo.Common","SOGo.PreferencesUI"])}angular.module("SOGo.ContactsUI").constant("sgCard_STATUS",{NOT_LOADED:0,DELAYED_LOADING:1,LOADING:2,LOADED:3,DELAYED_MS:300}).factory("Card",a.$factory),a.$find=function(e,t){var i=this.$$resource.fetch([e,t].join("/"),"view");return t?new a(i):a.$unwrapCollection(i)},a.filterCategories=function(e){var t=new RegExp(e,"i");return _.map(_.filter(a.$categories,function(e){return-1!=e.search(t)}),function(e){return{value:e}})},a.$unwrapCollection=function(e){var i={};return(i.$futureCardData=e).then(function(e){a.$timeout(function(){angular.forEach(e,function(e,t){i[e.id]=new a(e)})})}),i},a.prototype.init=function(e,t){var i=this;if(angular.isUndefined(this.refs)&&(this.refs=[]),angular.isUndefined(this.categories)&&(this.categories=[]),this.c_screenname=null,angular.extend(this,e),this.pid||(this.pid=this.container),this.$$fullname||(this.$$fullname=this.$fullname()),this.$$email||(this.$$email=this.$preferredEmail(t)),this.$$image||(this.$$image=this.image),this.$$image||(this.$$image=a.$Preferences.avatar(this.$$email,40,{no_404:!0})),this.hasphoto&&(this.photoURL=a.$$resource.path(this.pid,this.id,"photo")),this.isgroup&&(this.c_component="vlist"),this.$avatarIcon=this.$isList()?"group":"person",e.orgs&&e.orgs.length&&(this.orgs=_.map(e.orgs,function(e){return{value:e}})),e.notes&&e.notes.length?this.notes=_.map(e.notes,function(e){return{value:e}}):this.notes&&this.notes.length||(this.notes=[{value:""}]),angular.forEach(["addresses","phones","urls"],function(e){angular.forEach(i[e],function(e){e.type&&(e.type=e.type.toLowerCase())})}),angular.forEach(this.refs,function(e,t){e.email&&(e.emails=[{value:e.email}]),e.id=e.reference,i.refs[t]=new a(e)}),this.birthday&&angular.isString(this.birthday)){var r=a.$Preferences.$mdDateLocaleProvider;this.birthday=this.birthday.parseDate(r,"%Y-%m-%d"),this.$birthday=r.formatDate(this.birthday)}this.$loaded=angular.isDefined(this.c_name)?a.STATUS.LOADED:a.STATUS.NOT_LOADED,this.empty=" "},a.prototype.$id=function(){return this.$futureCardData.then(function(e){return e.id})},a.prototype.$path=function(){return[a.encodeUri(this.pid),a.encodeUri(this.id)].join("/")},a.prototype.$isLoading=function(){return this.$loaded==a.STATUS.LOADING},a.prototype.$reload=function(){var e;return this.$futureCardData?this:(e=a.$$resource.fetch(this.$path(),"view"),this.$unwrap(e))},a.prototype.$members=function(){var t=this;return this.members?a.$q.when(this.members):this.$isGroup({expandable:!0})?a.$$resource.fetch(this.$path(),"members").then(function(e){return t.members=_.map(e.members,function(e){return new a(e)}),t.members}):a.$q.reject("Card "+this.id+" is not an LDAP group")},a.prototype.$save=function(){var t=this,e="saveAsContact";return"vlist"==this.c_component&&(e="saveAsList",_.forEach(this.refs,function(e){e.reference=e.id})),a.$$resource.save([a.encodeUri(this.pid),a.encodeUri(this.id)||"_new_"].join("/"),this.$omit(),{action:e}).then(function(e){return t.birthday&&(t.$birthday=a.$Preferences.$mdDateLocaleProvider.formatDate(t.birthday)),t.$shadowData=t.$omit(!0),e})},a.prototype.$delete=function(e,t){if(!e)return a.$$resource.remove(this.$path());-1<t&&this[e].length>t?this[e].splice(t,1):delete this[e]},a.prototype.export=function(){var e,t;return e={uids:[this.id]},t={type:"application/octet-stream",filename:this.$$fullname+".ldif"},a.$$resource.download(this.pid,"export",e,t)},a.prototype.$fullname=function(e){var t,i,r=this.c_cn||"",s=e&&e.html;return 0===r.length&&(i=[],this.c_givenname&&0<this.c_givenname.length&&i.push(this.c_givenname),this.nickname&&0<this.nickname.length&&i.push((s?"<em>":"")+this.nickname+(s?"</em>":"")),this.c_sn&&0<this.c_sn.length&&i.push(this.c_sn),0<i.length?r=i.join(" "):this.org&&0<this.org.length?r=this.org:this.emails&&0<this.emails.length&&(t=_.find(this.emails,function(e){return""!==e.value}))&&(r=t.value)),this.contactinfo&&(r+=" ("+this.contactinfo.split("\n").join("; ")+")"),r},a.prototype.$description=function(){var e=[];return this.title&&e.push(this.title),this.role&&e.push(this.role),this.org&&e.push(this.org),this.orgs&&(e=_.concat(e,_.map(this.orgs,"value"))),this.description&&e.push(this.description),e.join(", ")},a.prototype.$preferredEmail=function(e){var t,i;return e&&(i=new RegExp(e,"i"),t=_.find(this.emails,function(e){return i.test(e.value)})),t=t?t.value:(t=_.find(this.emails,function(e){return"pref"==e.type}))?t.value:this.emails&&this.emails.length?this.emails[0].value:this.c_mail&&this.c_mail.length?this.c_mail[0]:""},a.prototype.$shortFormat=function(e){var t=[this.$$fullname],i=this.$preferredEmail(e);return i&&i!=this.$$fullname&&t.push(" <"+i+">"),t.join(" ")},a.prototype.$isCard=function(){return"vcard"==this.c_component},a.prototype.$isList=function(e){var t=!e||!e.expandable||e.expandable&&!this.isgroup;return"vlist"==this.c_component&&t},a.prototype.$isGroup=function(e){var t=!e||!e.expandable||e.expandable&&a.$Preferences.defaults.SOGoLDAPGroupExpansionEnabled;return this.isgroup&&t},a.prototype.$addOrg=function(e){return angular.isUndefined(this.orgs)?this.orgs=[e]:e==this.org||_.includes(this.orgs,e)||this.orgs.push(e),this.orgs.length-1},a.prototype.$addEmail=function(e){return angular.isUndefined(this.emails)?this.emails=[{type:e,value:""}]:_.isUndefined(_.find(this.emails,function(e){return""===e.value}))&&this.emails.push({type:e,value:""}),this.emails.length-1},a.prototype.$addScreenName=function(e){this.c_screenname=e},a.prototype.$addPhone=function(e){return angular.isUndefined(this.phones)?this.phones=[{type:e,value:""}]:_.isUndefined(_.find(this.phones,function(e){return""===e.value}))&&this.phones.push({type:e,value:""}),this.phones.length-1},a.prototype.$addUrl=function(e,t){return angular.isUndefined(this.urls)?this.urls=[{type:e,value:t}]:_.isUndefined(_.find(this.urls,function(e){return e.value==t}))&&this.urls.push({type:e,value:t}),this.urls.length-1},a.prototype.$addAddress=function(e,t,i,r,s,n,o,a){return angular.isUndefined(this.addresses)?this.addresses=[{type:e,postoffice:t,street:i,street2:r,locality:s,region:n,country:o,postalcode:a}]:_.find(this.addresses,function(e){return e.street==i&&e.street2==r&&e.locality==s&&e.country==o&&e.postalcode==a})||this.addresses.push({type:e,postoffice:t,street:i,street2:r,locality:s,region:n,country:o,postalcode:a}),this.addresses.length-1},a.prototype.$addMember=function(e){var t,i=new a({email:e,emails:[{value:e}]});if(angular.isUndefined(this.refs))this.refs=[i];else if(0===e.length)this.refs.push(i);else{for(t=0;t<this.refs.length&&this.refs[t].email!=e;t++);t==this.refs.length&&this.refs.push(i)}return this.refs.length-1},a.prototype.$certificate=function(){var t=this;return this.hasCertificate?this.$$certificate?a.$q.when(this.$$certificate):a.$$resource.fetch(this.$path(),"certificate").then(function(e){return t.$$certificate=e}):a.$q.reject()},a.prototype.$removeCertificate=function(){var e=this;return a.$$resource.fetch(this.$path(),"removeCertificate").then(function(){e.hasCertificate=!1})},a.prototype.explode=function(){var i,r=[];return this.emails?1<this.emails.length?(i=this.$omit(),_.forEach(this.emails,function(e){var t=new a(angular.extend({},i,{emails:[e]}));r.push(t)}),r):[this]:[]},a.prototype.$reset=function(){var i=this;angular.forEach(this,function(e,t){"constructor"!=t&&"$"!=t[0]&&delete i[t]}),this.init(this.$shadowData),this.$shadowData=this.$omit(!0)},a.prototype.$unwrap=function(e){var t=this;return this.$loaded=a.STATUS.DELAYED_LOADING,a.$timeout(function(){t.$loaded!=a.STATUS.LOADED&&(t.$loaded=a.STATUS.LOADING)},a.STATUS.DELAYED_MS),this.$futureCardData=e.then(function(e){return t.init(e),t.$loaded=a.STATUS.LOADED,t.$shadowData=t.$omit(!0),t}),this.$futureCardData},a.prototype.$omit=function(i){var r={};return angular.forEach(this,function(e,t){"refs"==t?r.refs=_.map(e,function(e){return e.$omit(i)}):"constructor"!=t&&"$"!=t[0]&&(r[t]=i?angular.copy(e):e)}),i||(r.birthday?r.birthday=r.birthday.format(a.$Preferences.$mdDateLocaleProvider,"%Y-%m-%d"):r.birthday=""),this.orgs&&(r.orgs=_.map(this.orgs,"value")),this.notes&&(r.notes=_.map(this.notes,"value")),r},a.prototype.toString=function(){var e=this.id+" "+this.$$fullname;return this.$$email&&(e+=" <"+this.$$email+">"),"["+e+"]"}}();
//# sourceMappingURL=Contacts.services.js.map