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