{"size": 10013, "ext": "js", "max_stars_repo_path": "pioclpoplcdbaefihamjohnefbikjilc/6.13.1_0/content/global_tools/AccountSelector.js", "max_stars_repo_name": "krishna2nd/chrome-exts", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "pioclpoplcdbaefihamjohnefbikjilc/6.13.1_0/content/global_tools/AccountSelector.js", "max_issues_repo_name": "krishna2nd/chrome-exts", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "pioclpoplcdbaefihamjohnefbikjilc/6.13.1_0/content/global_tools/AccountSelector.js", "max_forks_repo_name": "krishna2nd/chrome-exts", "max_forks_repo_forks_event_min_datetime": "2020-02-19T14:58:04.000Z", "max_forks_repo_forks_event_max_datetime": "2021-08-28T07:54:33.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 3337.6666666667, "max_line_length": 9942, "alphanum_fraction": 0.7979626486}
/*! Copyright 2009-2017 Evernote Corporation. All rights reserved. */

"use strict";function AccountField(a){this.defaultModel={header:"",description:"",iconUrl:"",remoteIconUrl:"",isHeader:!1,selection:!1,selected:!1},this.model={},this.view=createFieldView(),a&&this.updateModel(a),this.view.container.onclick=this.select.bind(this)}function AccountSelector(a){this.model=this.getDefaultModel(),this.updateModel(a)}var BUSINESS=GlobalUtils.ACCOUNT_TYPE_BUSINESS,PERSONAL=GlobalUtils.ACCOUNT_TYPE_PERSONAL,ONLY_BUSINESS=GlobalUtils.ACCOUNT_TYPE_ONLY_BUSINESS,BUSINESS_ICON_URL="images/account-selector-avatar-business-disabled.svg",PERSONAL_ICON_URL="images/account-selector-avatar-empty.svg";AccountField.prototype.updateModel=function(a,b){b?(this.model=Object.assign({},this.defaultModel,a),this.updateView(this.model)):(this.model=Object.assign(this.model,a),this.updateView(a))},AccountField.prototype.updateView=function(a){"string"==typeof a.iconUrl&&(""===a.iconUrl?this.view.userIcon.setAttribute("style",""):this.view.userIcon.style.backgroundImage="url("+a.iconUrl+")"),a.remoteIconUrl&&this.downloadImage(a.remoteIconUrl),"string"==typeof a.header&&(this.view.header.innerText=a.header),"string"==typeof a.description&&(this.view.description.innerText=a.description),"boolean"==typeof a.isHeader&&this.view.dropdownIcon.classList.toggle("hidden",!a.isHeader),"boolean"==typeof a.selection&&this.view.container.classList.toggle("asSelection",a.selection),"boolean"==typeof a.selected&&this.view.container.classList.toggle("asSelected",a.selected),this.accountRequiresAuth()&&!this.model.isHeader?this.view.header.innerText=Browser.i18n.getMessage("Sign_in")+" "+this.model.header:this.view.header.innerText=this.model.header,a.container&&a.container.appendChild(this.view.container)},AccountField.prototype.accountRequiresAuth=function(){return this.model.account?!this.model.account.isAuthenticated||this.model.subpart===BUSINESS&&!this.model.account.isAuthenticatedBiz:null},AccountField.prototype.select=function(a){if(this.model.isSelectable){var b=new CustomEvent("selected",{detail:this,bubbles:!0});this.view.container.dispatchEvent(b),this.updateModel({selected:!0,selection:!0})}"function"==typeof this.model.callback&&this.model.callback(this.model)},AccountField.prototype.downloadImage=function(a){var b=this,c=new XMLHttpRequest;c.url=a+"?size=48",c.method="GET",c.withCredentials=!0,c.open(c.method,c.url),c.setRequestHeader("Content-type","application/x-www-form-urlencoded"),c.responseType="arraybuffer",c.send(),c.onreadystatechange=function(){if(4===this.readyState&&200===this.status){for(var c="",d=new Uint8Array(this.response),e=0;e<d.length;e++)c+=String.fromCharCode(d[e]);b.model.remoteIconUrl&&b.model.remoteIconUrl===a&&(a="data:"+this.getResponseHeader("Content-type")+";base64,"+btoa(c),b.updateModel({iconUrl:a}))}}},AccountSelector.prototype.getDefaultModel=function(){return{header:new AccountField,fields:[],current:null,isOpened:!1}},AccountSelector.prototype.updateModel=function(a){this.model=Object.assign(this.model,a)},AccountSelector.prototype.reset=function(){this.view&&(this.view.globalContainer.removeChild(this.view.dropdownOverlay),this.view.globalContainer.removeChild(this.view.dropdown),this.view.header.innerHTML="",this.view=null),this.updateModel(this.getDefaultModel()),this.view=createAccountSelectorView(this.model),this.keyHandler=this.handleKey.bind(this),this.view.header.onclick=function(){this.openDropdown()}.bind(this),this.view.dropdownOverlay.onclick=function(){this.closeDropdown()}.bind(this),this.view.dropdown.addEventListener("selected",function(a){var b=a.detail;if(b.model.account){this.model.currentAccount=b.model;var c=this.model.currentAccount;"function"==typeof this.model.changeCallback&&this.model.changeCallback(b.model.account.userId,b.model.subpart),this.accountRequiresAuth(c.account.userId,c.subpart)&&"function"==typeof this.model.signInCallback&&this.model.signInCallback(c.account.userId,c.subpart,c.account.isAuthenticated)}this.current=b,this.updateHeader(b.model),this.closeDropdown();for(var d=0;d<this.model.fields.length;d++)this.model.fields[d].updateModel({selection:!1,selected:!1})}.bind(this))},AccountSelector.prototype.setData=function(a,b){this.model.isOpened&&Browser.popKeyHandler(this.keyHandler),this.reset();for(var c=Object.keys(a),d=0;d<c.length;d++)this.addOption(a[c[d]]);b?this.selectItem(b):this.model.fields[0].select()},AccountSelector.prototype.updateHeader=function(a){this.model.header.updateModel(Object.assign({},a,{container:this.view.header,isHeader:!this.model.dropdownDisabled}),!0)},AccountSelector.prototype.addOption=function(a){a.accountType==BUSINESS&&this.model.fields.push(new AccountField(this.parseOption(a,PERSONAL))),this.model.fields.push(new AccountField(this.parseOption(a)))},AccountSelector.prototype.removeOption=function(a){},AccountSelector.prototype.parseOption=function(a,b){var c={};return a.accountType?(c.account=Object.assign({},a),c.header=a.fullName,c.subpart=b||a.accountType,a.accountType===ONLY_BUSINESS&&(c.subpart=BUSINESS),c.subpart===BUSINESS&&a.accountType!==ONLY_BUSINESS?c.iconUrl=Browser.getExtensionURL(BUSINESS_ICON_URL):(c.iconUrl=Browser.getExtensionURL(PERSONAL_ICON_URL),c.remoteIconUrl=a.photoUrl),c.isSelectable=!0,b||(c.description=a.businessName||"")):c=a,c.container=this.view.dropdown,c},AccountSelector.prototype.selectItem=function(a){var b;a.selectedAccountId?b=this.model.fields.find(function(b){return b.model.account&&b.model.account.userId===a.selectedAccountId&&b.model.subpart===a.selectedSubpart}):a.id&&(b=this.model.fields.find(function(b){return b.model.id&&b.model.id===a.id})),b?b.select():(log.warn("Account selector hasn't field with options "+JSON.stringify(a)),this.selectFirstLoggedIn())},AccountSelector.prototype.accountRequiresAuth=function(a,b){var c=this.model.fields.find(function(c){return!!c.model.account&&(c.model.account.userId===a&&c.model.subpart===b)});return c?c.accountRequiresAuth():null},AccountSelector.prototype.updateAccountTier=function(a,b){for(var c=0;c<this.model.fields.length;c++)this.model.fields[c].model.account&&this.model.fields[c].model.account.userId===a&&this.model.fields[c].updateModel({account:b})},AccountSelector.prototype.toggleSelectedAccount=function(){var a=this.getSelectedIndex(),b=a+1;b>=this.model.fields.length&&(b=0);for(var c=b;c<this.model.fields.length;c++){if(this.model.fields[c].model.account){this.model.fields[c].select();break}c==this.model.fields.length-1&&(c=-1)}},AccountSelector.prototype.getSelectedAccount=function(){return this.model.currentAccount?{userInfo:this.model.currentAccount.account,selectedSubpart:this.model.currentAccount.subpart}:{userInfo:null,selectedSubpart:null}},AccountSelector.prototype.selectFirstLoggedIn=function(){var a=this.model.fields.find(function(a){return!a.accountRequiresAuth()});a&&a.select()},AccountSelector.prototype.openDropdown=function(){if(!this.model.dropdownDisabled){this.view.dropdown.classList.toggle("hidden",!1),this.view.dropdownOverlay.classList.toggle("hidden",!1);var a=this.view.globalContainer.getBoundingClientRect(),b=this.view.header.getBoundingClientRect(),c=b.bottom-a.top;this.view.dropdown.style.top=c+"px",Browser.pushKeyHandler(this.keyHandler),this.model.isOpened=!0}},AccountSelector.prototype.closeDropdown=function(){this.model.isOpened&&(this.view.dropdown.classList.toggle("hidden",!0),this.view.dropdownOverlay.classList.toggle("hidden",!0),Browser.popKeyHandler(this.keyHandler),this.model.isOpened=!1)},AccountSelector.prototype.hideDropdown=function(){this.model.dropdownDisabled=!0,this.view.header.classList.add("disabled"),this.model.header.updateModel({isHeader:!1})},AccountSelector.prototype.getSelectionIndex=function(){for(var a=0,b=0;b<this.model.fields.length;b++)this.model.fields[b].view.container.classList.contains("asSelection")&&(a=b);return a},AccountSelector.prototype.getSelectedIndex=function(){for(var a=0,b=0;b<this.model.fields.length;b++)this.model.fields[b].model.selected&&(a=b);return a},AccountSelector.prototype.moveSelection=function(a){for(var b=0,c=0;c<this.model.fields.length;c++)this.model.fields[c].view.container.classList.contains("asSelection")&&(b=c),this.model.fields[c].updateModel({selection:!1});b+=a,b>=this.model.fields.length&&(b=0),b<0&&(b=this.model.fields.length-1),this.model.fields[b].updateModel({selection:!0})},AccountSelector.prototype.handleKey=function(a){switch(a.keyCode){case 38:this.moveSelection(-1),a.stopPropagation(),a.preventDefault();break;case 40:this.moveSelection(1),a.stopPropagation(),a.preventDefault();break;case 27:this.closeDropdown(),a.stopPropagation(),a.preventDefault();break;case 13:this.closeDropdown(),this.model.fields[this.getSelectionIndex()].select(),a.stopPropagation(),a.preventDefault()}};var createAccountSelectorView=function(a){var b={};return b.globalContainer=a.globalContainer,b.header=a.fieldContainer,b.dropdownOverlay=document.createElement("div"),b.dropdownOverlay.classList.add("asDropdownOverlay","hidden"),b.dropdown=document.createElement("div"),a.dropdownID&&b.dropdown.setAttribute("id",a.dropdownID),b.dropdown.classList.add("asDropdown","hidden"),b.globalContainer.appendChild(b.dropdownOverlay),b.globalContainer.appendChild(b.dropdown),b},createFieldView=function(){var a={};return a.container=document.createElement("div"),a.container.classList.add("asField"),a.userIcon=document.createElement("div"),a.userIcon.classList.add("asUserIcon"),a.header=document.createElement("div"),a.header.classList.add("asHeader"),a.description=document.createElement("div"),a.description.classList.add("asDescription"),a.labels=document.createElement("div"),a.labels.classList.add("asLabels"),a.labels.appendChild(a.header),a.labels.appendChild(a.description),a.dropdownIcon=document.createElement("div"),a.dropdownIcon.classList.add("asDropdownIcon","hidden"),a.container.appendChild(a.userIcon),a.container.appendChild(a.labels),a.container.appendChild(a.dropdownIcon),a};