(function(){"use strict";function e(e,i){this.MobileWidget(e,i);this._$list=this.$element.find(".u8-participants");if(u8.tools.isInstanceOfClass(this.parent,"Expand")&&!u8.parseRecordUid(this.parentUid).recordId){var r=this;this.parent.bind("Change",function(e,i){if(i.reason!="links"){return}var n=t(e.getLinks());if(!n){return}r._updateParentParentUid(n)});var n=t(this.parent.getLinks());if(n){this.parentParentUid=n;this._add({uid:n})}}}function t(e){var t=undefined;for(var i=0;i<e.length;i++){t=u8.parseLinkRecordUid(e[i]);if(t.infoAreaId=="FI"||t.infoAreaId=="KP"||t.infoAreaId=="PE"){return u8.recordUidToString(t)}}return null}e.prototype={"Selector.canSelect":function(e){return e.type=="record"},"Selector.select":function(e){this._add(e)},"Expand.save":function(e){e.children.push({type:"LinkParticipants",fieldGroupName:this.fieldGroupName,items:this._createModel()})},remove:function(e){var t=$(e.srcElement).closest("li");var r=i(t[0]);if(r){this._remove(r.uid)}},edit:function(e){var t=$(e.srcElement).closest("li");var r=i(t[0]);if(!r){return}var n={uid:r.uid,parentUid:this.parentUid,acceptance:r.acceptance,requirement:r.requirement,fieldGroupName:this.fieldGroupName,canRequire:e.type=="requirement",canAccept:e.type=="acceptance"};var a=new u8.Base.Uri("~/meetings/editparticipant");a.params=n;u8.application.execute("popup",{url:a.toString_(),data:{},method:"GET",srcElement:e.srcElement})},updateParticipant:function(e,t){var r=e.operation||"add";delete e.operation;if(r=="remove"){this._remove(e.uid);return}var n=$(t.srcElement).closest("li");if(!n.length){return}var a=i(n[0]);this._updateListElement($.extend({requirement:a.requirement,acceptance:a.acceptance},e),n)},_updateListElement:function(e,t){u8.rest({url:"~/meetings/participant",data:$.extend({},e,{fieldGroupName:this.fieldGroupName,parentUid:this.parentUid,parentParentUid:this.parentParentUid})}).done(this._done.bind(this,t))},_add:function(e){var t=e.uid||u8.recordUidToString(e.links[0]);var i=this._keyTo$(t);if(i.length){return}u8.rest({url:"~/meetings/participant",headers:{Accept:"application/json,text/html"},data:{uid:t,parentUid:this.parentUid,parentParentUid:this.parentParentUid,fieldGroupName:this.fieldGroupName,removedParticipantUids:this._removedParticipantUids},traditional:true}).done(this._done.bind(this,null)).fail(function(e){u8.application.execute(e.data)})},_done:function(e,t){if(e){e.replaceWith($(t))}else{if(typeof t!="string"){return u8.application.execute(t)}var r=this._$list.children(".item:last");var n=$(t);if(r.length){r.after(n)}else{n.prependTo(this._$list)}if(this._removedParticipantUids){this._removedParticipantUids.remove(i(n[0]).uid);if(this._removedParticipantUids.length==0){delete this._removedParticipantUids}}}},_remove:function(e){var t=this._keyTo$(e);if(t.length){t.remove();if(!this._removedParticipantUids){this._removedParticipantUids=[]}this._removedParticipantUids.push(e)}},_createModel:function(){return this._$list.children("[data-model]").toArray().map(i)},_updateParentParentUid:function(e){if(this.parentParentUid==e){return}var t=this._$list.children("[data-model]");var r=null,n=null,a=null;for(var d=0;d<t.length;d++){var s=t[d],o=i(s);if(o.uid==e){a=o;n=s}else if(o.uid==this.parentParentUid){r=s}}this.parentParentUid=e;if(r){$(r).remove()}if(n){this._updateListElement(a,$(n))}else{this._add({uid:e})}},_keyTo$:function(e){return this._$list.children("[data-key='"+e+"']")}};e.makeClass({name:"LinkParticipantsEditor",deriveFrom:u8.Mobile.Widgets.MobileWidget});function i(e){var t=e&&e.getAttribute("data-model");return t&&JSON.parse(t)}})();