text: (function(){const component={name:"teambuild",version:[3,0,1],ccm:"https://ccmjs.github.io/ccm/versions/ccm-20.0.0.js",config:{html:{main:{id:"main",inner:[{id:"user"},{id:"teams"}]},team:{class:"team",inner:[{class:"header",inner:[{class:"name",inner:"%name%"},{class:"button"}]},{class:"members"}]},button:{tag:"button",inner:"%caption%",onclick:"%onclick%"},member:{class:"member",inner:{class:"name",inner:"%name%"}}},css:["ccm.load","https://ccmjs.github.io/akless-components/teambuild/resources/default.css"],data:{},text:{team:"Team",leave:"leave",join:"join",free:"free",message:"Nothing to display."},icon:{},editable:true},Instance:function(){let $;this.init=(async()=>{$=this.ccm.helper;if(this.user)this.user.onchange=this.start});this.ready=(async()=>{this.logger&&this.logger.log("ready",$.privatize(this,true))});this.start=(async()=>{const dataset=await $.dataset(this.data);this.logger&&this.logger.log("start",$.clone(dataset));if(!dataset.teams)dataset.teams=[];$.setContent(this.element,$.html(this.html.main));const teams_elem=this.element.querySelector("#teams");if(this.user){await this.user.start();$.setContent(this.element.querySelector("#user"),this.user.root)}const self=this;addTeams();function addTeams(){dataset.teams.map(addTeam);if(self.max_teams){const needed=self.max_teams-dataset.teams.length;for(let i=0;i<needed;i++)addEmptyTeam()}else if((dataset.teams.length===0||!isEmptyTeam(dataset.teams[dataset.teams.length-1]))&&joinableTeams())addEmptyTeam();if(dataset.teams.length===0)$.setContent(teams_elem,self.text.message);function addTeam(team,i){if(i===undefined)i=dataset.teams.length-1;const team_elem=$.html(self.html.team,{icon:self.icon.team,name:team.name?team.name:self.names&&self.names[i]?self.names[i]:self.text.team+" "+(i+1)});const max_members=team.max_members||self.max_members;const user=self.user&&self.user.isLoggedIn()&&self.user.data().user;if($.isObject(self.data)&&$.isDatastore(self.data.store)&&user){const user_team=user&&getUserTeam();if(isMember(team)){if(isEditable(i,"rename"))makeEditable();if(isEditable(i,"leave"))addLeaveButton()}else if(isJoinable())addJoinButton();function isMember(team){return!!team.members[user]}function isEditable(team,action){if(team.editable===undefined)return!(self.editable===false||$.isObject(self.editable)&&self.editable[action]===false);else return!(team.editable===false||$.isObject(team.editable)&&team.editable[action]===false)}function makeEditable(){const name_elem=team_elem.querySelector(".name");name_elem.setAttribute("contenteditable",true);name_elem.addEventListener("input",async()=>{const value=name_elem.textContent.trim();team.name=$.protect(value);self.logger&&self.logger.log("rename",$.clone({team:team.key,name:team.name}));await self.data.store.set(dataset)})}function addLeaveButton(){$.setContent(team_elem.querySelector(".button"),$.html(self.html.button,{icon:self.icon.leave,caption:self.text.leave,onclick:async()=>{delete team.members[user];if(!self.max_teams&&isEmptyTeam(team))dataset.teams.splice(i,1);await self.data.store.set(dataset);const event_data={leaved:team.key};self.logger&&self.logger.log("leave",$.clone(event_data));self.onchange&&self.onchange(self,$.clone(event_data));await self.start()}}))}function isJoinable(){if(!isEditable(i,"join"))return false;if(user_team&&!isEditable(user_team-1,"leave"))return false;if(!self.max_teams&&i===dataset.teams.length-1&&i>0){if(isOnlyMember(dataset.teams[i-1]))return false;function isOnlyMember(team){return team.members[user]&&Object.keys(team.members).length===1}}return!max_members||Object.keys(team.members).length<max_members}function addJoinButton(){$.setContent(team_elem.querySelector(".button"),$.html(self.html.button,{icon:self.icon.join,caption:self.text.join,onclick:async()=>{const leaving_team=user_team&&dataset.teams[user_team-1];if(user_team){delete leaving_team.members[user];if(!self.max_teams&&isEmptyTeam(leaving_team))dataset.teams.splice(user_team-1,1)}team.members[user]=true;await self.data.store.set(dataset);const event_data={joined:team.key,leaved:leaving_team&&leaving_team.key};self.logger&&self.logger.log("join",$.clone(event_data));self.onchange&&self.onchange(self,$.clone(event_data));await self.start()}}))}}addMembers();teams_elem.appendChild(team_elem);function getUserTeam(){for(let i=0;i<dataset.teams.length;i++)if(dataset.teams[i].members[user])return i+1}function addMembers(){const members_elem=team_elem.querySelector(".members");for(const member in team.members)addMember(member);for(let i=0;i<max_members-Object.keys(team.members).length;i++)addMember();function addMember(member){const member_elem=$.html(self.html.member,{icon:self.icon.member,name:member?member:self.text.free});const name_elem=member_elem.querySelector(".name");if(user&&member&&user===member)name_elem.classList.add("user");if(!member)name_elem.classList.add("free");members_elem.appendChild(member_elem)}}}function isEmptyTeam(team){return Object.keys(team.members).length===0}function addEmptyTeam(){const team={key:$.generateKey(),members:{}};dataset.teams.push(team);addTeam(team)}function joinableTeams(){return!(self.editable===false||self.editable&&self.editable.join===false)}}})}};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*)/)||["latest"])[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(){window.ccm[c].component(component);document.head.removeChild(a)};a.src=component.ccm.url}})();