text: (()=>{const component={name:"teambuild",version:[5,1,0],ccm:"https://ccmjs.github.io/ccm/versions/ccm-26.2.1.js",config:{css:["ccm.load","https://ccmjs.github.io/akless-components/teambuild/resources/default.css"],data:{store:["ccm.store"]},editable:{join:true,leave:true,rename:true},helper:["ccm.load","https://ccmjs.github.io/akless-components/modules/versions/helper-7.1.0.mjs"],html:["ccm.load","https://ccmjs.github.io/akless-components/teambuild/resources/templates.html"],icon:{},text:{team:"Team",leave:"leave",join:"join",free:"free"}},Instance:function(){let $,app_data,main_elem,user_key;this.init=(async()=>{$=Object.assign({},this.ccm.helper,this.helper);$.use(this.ccm);if(this.user)this.user.onchange=this.start;this.data.store.onchange=(async priodata=>{if(priodata.key!==this.data.key)return;await this.refresh(priodata);this.onchange&&this.onchange({instance:this,extern:true})})});this.ready=(async()=>{this.logger&&this.logger.log("ready",$.privatize(this,true))});this.start=(async()=>{app_data=await $.dataset(this.data);user_key=this.user&&this.user.isLoggedIn()&&this.user.getValue().key;this.logger&&this.logger.log("start",$.clone(app_data));if(!app_data.teams)app_data.teams=[];main_elem=$.html(this.html.main,this.start);if(!this.reload)$.remove(main_elem.querySelector("#reload"));this.refresh();if(this.user){$.append(main_elem.querySelector("#top"),this.user.root);this.user.start()}$.setContent(this.element,main_elem)});this.checkAction=((team_nr,action)=>{let team_data=getTeam(team_nr);team_data=team_data.editable===undefined?this:team_data;return!(team_data.editable===false||$.isObject(team_data.editable)&&team_data.editable[action]===false)});this.getMember=((team_nr,user=user_key)=>getTeam(team_nr).members[user]);this.getTeamData=(team_nr=>$.clone(getTeam(team_nr)));this.getTeamElement=(team_nr=>main_elem.querySelectorAll(".team")[team_nr-1]);this.getUserTeam=((user=user_key)=>{for(let i=0;i<app_data.teams.length;i++)if(this.getMember(i+1,user))return i+1});this.getValue=(()=>$.clone(app_data));this.isEmptyTeam=(team_nr=>!Object.keys(getTeam(team_nr).members).length);this.isJoinable=((team_nr,user=user_key)=>{if(!$.isDatastore(this.data.store)||!user)return false;if(!$.hasPermission(app_data,this.user,"set"))return false;if(this.getMember(team_nr,user))return false;if(!this.checkAction(team_nr,"join"))return false;const user_team=this.getUserTeam(user);if(user_team&&!this.checkAction(user_team,"leave"))return false;if(!this.max_teams&&team_nr===app_data.teams.length&&team_nr>1)if(this.isOnlyMember(team_nr-1,user))return false;return!this.max_members||Object.keys(getTeam(team_nr).members).length<this.max_members});this.isLeavable=((team_nr,user=user_key)=>$.isDatastore(this.data.store)&&user&&$.hasPermission(app_data,this.user,"set")&&this.getMember(team_nr,user)&&this.checkAction(team_nr,"leave"));this.isRenamable=((team_nr,user=user_key)=>$.isDatastore(this.data.store)&&user&&$.hasPermission(app_data,this.user,"set")&&this.getMember(team_nr,user)&&this.checkAction(team_nr,"rename"));this.isOnlyMember=((team_nr,user=user_key)=>this.getMember(team_nr,user)&&Object.keys(getTeam(team_nr).members).length===1);this.joinableTeams=(()=>!(this.editable===false||this.editable&&this.editable.join===false));this.joinTeam=(async team_nr=>{if(!this.isJoinable(team_nr))return;const joined_team=getTeam(team_nr);const leaved_team_nr=this.getUserTeam();const leaved_team=getTeam(leaved_team_nr);if(leaved_team_nr){await this.leaveTeam(leaved_team_nr);if(!this.max_teams&&this.isEmptyTeam(leaved_team_nr))app_data.teams.splice(leaved_team_nr-1,1)}joined_team.members[user_key]=this.user.getUsername();await this.data.store.set(app_data);this.refresh();this.logger&&this.logger.log("join",{team:$.clone(joined_team),nr:team_nr,leaved:leaved_team_nr});this.onchange&&this.onchange({event:"join",team:$.clone(joined_team),nr:team_nr,leaved:leaved_team_nr,element:this.getTeamElement(team_nr),instance:this})});this.leaveTeam=(async team_nr=>{if(!this.isLeavable(team_nr))return;const team_data=getTeam(team_nr);delete team_data.members[user_key];if(!this.max_teams&&this.isEmptyTeam(team_nr))app_data.teams.splice(team_nr-1,1);await this.data.store.set(app_data);this.refresh();this.logger&&this.logger.log("leave",{team:$.clone(team_data),nr:team_nr});this.onchange&&this.onchange({event:"leave",team:$.clone(team_data),nr:team_nr,element:this.getTeamElement(team_nr),instance:this})});this.refresh=((dataset=app_data)=>renderTeams(app_data=dataset));this.renameTeam=(async(team_nr,name)=>{if(!this.isRenamable(team_nr))return;const team_data=getTeam(team_nr);team_data.name=$.protect(name.trim());await this.data.store.set(app_data);this.logger&&this.logger.log("rename",{team:$.clone(team_data)});this.onchange&&this.onchange({event:"rename",team:$.clone(team_data),nr:team_nr,instance:this})});const renderTeams=()=>{$.setContent(main_elem.querySelector("#teams"),"");app_data.teams.forEach(appendTeam);if(this.max_teams){const needed=this.max_teams-app_data.teams.length;for(let i=0;i<needed;i++)appendEmptyTeam()}else if(app_data.teams.length===0||!this.isEmptyTeam(app_data.teams.length)&&this.joinableTeams())appendEmptyTeam()};const appendTeam=(team_data,i=app_data.teams.length-1)=>{const team_nr=i+1;if(!team_data.name||/^\w+ \d+$/.test(team_data.name))team_data.name=this.names&&this.names[i]?this.names[i]:this.text.team+" "+team_nr;const team_elem=$.html(this.html.team,{icon:this.icon.team,name:team_data.name});main_elem.querySelector("#teams").appendChild(team_elem);renderMembers(team_nr);this.isJoinable(team_nr)&&$.setContent(this.getTeamElement(team_nr).querySelector(".button"),$.html(this.html.button,{icon:this.icon.join,caption:this.text.join,onclick:()=>this.joinTeam(team_nr)}));this.isLeavable(team_nr)&&$.setContent(this.getTeamElement(team_nr).querySelector(".button"),$.html(this.html.button,{icon:this.icon.leave,caption:this.text.leave,onclick:()=>this.leaveTeam(team_nr)}));if(!this.isRenamable(team_nr))return;const name_elem=this.getTeamElement(team_nr).querySelector(".name");name_elem.setAttribute("contenteditable",true);name_elem.addEventListener("input",async()=>this.renameTeam(team_nr,name_elem.textContent))};const appendEmptyTeam=()=>{const team={key:$.generateKey(),members:{}};app_data.teams.push(team);appendTeam(team)};const getTeam=team_nr=>app_data.teams[team_nr-1];const renderMembers=team_nr=>{const team_data=getTeam(team_nr),team_elem=this.getTeamElement(team_nr);$.setContent(team_elem.querySelector(".members"),"");for(const key in team_data.members)appendMember(team_nr,key,team_data.members[key]);const free_slots=this.max_members-Object.keys(team_data.members).length;for(let i=0;i<free_slots;i++)appendMember(team_nr)};const appendMember=(team_nr,user,username=user)=>{const member_elem=$.html(this.html.member,{icon:this.icon.member,name:user?username===true?user:username:this.text.free});const name_elem=member_elem.querySelector(".name");user&&user_key&&user===user_key&&name_elem.classList.add("user");!user&&name_elem.classList.add("free");this.getTeamElement(team_nr).querySelector(".members").appendChild(member_elem)}}};let b="ccm."+component.name+(component.version?"-"+component.version.join("."):"")+".js";if(window.ccm&&null===window.ccm.files[b])return window.ccm.files[b]=component;(b=window.ccm&&window.ccm.components[component.name])&&b.ccm&&(component.ccm=b.ccm);"string"===typeof component.ccm&&(component.ccm={url:component.ccm});let c=(component.ccm.url.match(/(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/)||[""])[0];if(window.ccm&&window.ccm[c])window.ccm[c].component(component);else{var a=document.createElement("script");document.head.appendChild(a);component.ccm.integrity&&a.setAttribute("integrity",component.ccm.integrity);component.ccm.crossorigin&&a.setAttribute("crossorigin",component.ccm.crossorigin);a.onload=function(){(c="latest"?window.ccm:window.ccm[c]).component(component);document.head.removeChild(a)};a.src=component.ccm.url}})();