!function(e){let o=function(o,i,s){let r=this;r.oldName=i,r.isInitialized=!1,r.rawHostsInGroup=s||[],r.id=o,r.name=ko.observable(i),r.editing=ko.observable(!i).extend({throttle:200}),r.hostsInGroup=ko.observableArray(),r.availableHosts=ko.observableArray(),r.hostsInGroupSelected=ko.observableArray(),r.availableHostsSelected=ko.observableArray(),r.initialize=function(){if(!r.isInitialized){r.isInitialized=!0;let e=n.hosts.slice();e.forEach((function(e){e.disable=!1})),e.forEach((function(e){for(let o of r.rawHostsInGroup)if(o.id==e.id)return void(e.disable=1==o.origin)})),r.availableHosts(e);let o=r.availableHosts.remove((function(e){for(let o of r.rawHostsInGroup)if(o.id==e.id)return!0;return!1}));r.hostsInGroup.push.apply(r.hostsInGroup,o)}},r.setOptionDisable=function(e,o){ko.applyBindingsToNode(e,{disable:ko.observable(!!o.disable&&o.disable)},o)},r.updateName=function(o,t){t.stopImmediatePropagation();let i=r.name();!i||i.trim().length<=0?r.editing(!0):(r.editing(!1),r.oldName!==r.name()&&(r.oldName=r.name(),r.id?e.post("../../admin/groups/update",{id:r.id,name:r.name()}).fail(MMONIT.error.json):e.post("../../admin/groups/create",{name:r.name()},(function(e){r.id=e,null===n.expandedRow()&&n.toggleExpand(r)})).fail(MMONIT.error.json)))},r.edit=function(e,o){r.oldName=r.name(),r.editing(!0),o.stopPropagation()},r.stopEdit=function(){r.name().trim().length>0&&r.editing(!1)},r.move=function(e,o,n){n.push.apply(n,e),o.removeAll(e),n.sort(t),o.sort(t)},r.addHostsToGroup=function(){if(r.name()&&r.availableHostsSelected().length){let o="id="+r.id+a("hostid",r.availableHostsSelected());e.post("../../admin/groups/add",o,(function(e){r.move(r.availableHostsSelected(),r.availableHosts,r.hostsInGroup),r.availableHostsSelected.removeAll()})).fail(MMONIT.error.json)}},r.removeHostsFromGroup=function(){if(r.name()&&r.hostsInGroupSelected().length){let o="id="+r.id+a("hostid",r.hostsInGroupSelected());e.post("../../admin/groups/remove",o,(function(e){r.move(r.hostsInGroupSelected(),r.hostsInGroup,r.availableHosts),r.hostsInGroupSelected.removeAll()})).fail(MMONIT.error.json)}}},n=new function(){let n=this;n.group=ko.observable(),n.hosts=[],n.expandedRow=ko.observable(),n.groups=ko.observableArray(),n.shouldPushDown=function(){return!(ISPHONE||ISTABLET)},n.confirmDelete=function(e){n.group(e)},n.toggleExpand=function(e){let o=n.expandedRow()!==e;o&&e.initialize(),n.expandedRow(o?e:null)},n.isExpanded=function(e){return e===n.expandedRow()},n.remove=function(o){e.post("../../admin/groups/delete",{id:o.id},(function(e){n.groups.remove(o)})).fail(MMONIT.error.json)},n.add=function(e,t){t.stopImmediatePropagation(),n.expandedRow(null),n.groups.push(new o)},e.getJSON("../../admin/groups/list",(function(e){n.hosts=e.hosts.sort(t);for(let t of e.groups)n.groups.push(new o(t.id,t.name,t.hosts));n.groups.sort(i)})).fail(MMONIT.error.json)};ko.applyBindings(n),ko.bindingHandlers.onEnter={init:function(e,o,n,t){ko.utils.registerEventHandler(e,"keydown",(function(e){13===e.keyCode&&o().call(t)}))}};let t=function(e,o){let n=e.name.toLowerCase().localeCompare(o.name.toLowerCase());return n||(e.name>o.name?1:e.name<o.name?-1:0)},i=function(e,o){let n=e.name().toLowerCase().localeCompare(o.name().toLowerCase());return n||(e.name>o.name?1:e.name<o.name?-1:0)},a=function(e,o){let n=[];for(let t of o)n.push("&"),n.push(e),n.push("="),n.push(t.id);return n.join("")}}(jQuery);