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