{
    "type": "Ext.app.Controller",
    "reference": {
        "name": "items",
        "type": "array"
    },
    "codeClass": null,
    "userConfig": {
        "designer|userClassName": "AddRemoveRoleController"
    },
    "designerId": "23f2daf9-be85-4bc7-9a07-873b66269159",
    "cn": [
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "mainTreeGrid",
                "selector": "#treegrid"
            },
            "designerId": "4ba00840-75a8-40f4-9b5a-6c8d21680286"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "removeRoleButton",
                "selector": "#removeRoleButton"
            },
            "designerId": "1c116e95-e174-48a9-9868-2fa319e1d655"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addRoleButton",
                "selector": "#addRoleButton"
            },
            "designerId": "bbc5260a-c6d5-49ee-b308-ce4c9af91d15"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "autoCreate": true,
                "ref": "addRoleToRuleWindowClass",
                "selector": "#addRoleToRuleWindow",
                "xtype": "addRoleToRuleWindowAlias"
            },
            "designerId": "7dcd0aef-7a61-4a58-b7fd-791dc42205d2"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addToRuleCancelButton",
                "selector": "#addToRuleCancelButton",
                "xtype": null
            },
            "designerId": "63fbae25-8d71-44b1-b570-3f9259386eb4"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addToRuleClearButton",
                "selector": "#addToRuleClearButton"
            },
            "designerId": "6b450ede-5611-4a16-aa9b-a27b73cf3774"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addToRuleAddButton",
                "selector": "#addToRuleAddButton"
            },
            "designerId": "aa25669b-1649-4b1f-b86d-27909658e40f"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addToRuleTreePanel",
                "selector": "#addRoleToRuleTreePanel"
            },
            "designerId": "fb5e1a6a-5b53-417e-8fe4-d453432aeae0"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "addToRuleSearchField",
                "selector": "#addToRuleSearchField"
            },
            "designerId": "d9d0b96f-a9c6-41e2-ac95-3b5dbacd4a09"
        },
        {
            "type": "fixedfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "application"
                ],
                "fn": "init",
                "implHandler": [
                    "//main GUI buttons\r",
                    "this.control({\r",
                    "    '#removeRoleButton': {\r",
                    "        click: function() {\r",
                    "            this.removeRole();\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "this.control({\r",
                    "    '#addRoleButton': {\r",
                    "        click: function() {\r",
                    "            this.getAddRoleToRuleWindowClass().show(); //open window\r",
                    "\t\t\tthis.getMainTreeGrid().getSelectionModel().selected.getAt(0).expand(); //expand selected rule on tree\r",
                    "\t\t\t//add trigger listener on search trigger\r",
                    "            var me = this;\r",
                    "            this.getAddToRuleSearchField().onTriggerClick = function(event) {\r",
                    "                me.search();\r",
                    "            };\r",
                    "\t\t\t//set params in search text and focus field\r",
                    "\t\t\tthis.getAddToRuleSearchField().setValue(this.getAddToRuleTreePanel().getStore().getProxy().extraParams.rolefilter);\r",
                    "            this.getAddToRuleSearchField().focus();\r",
                    "\t\t\t\r",
                    "\t\t\tthis.rule = this.getMainTreeGrid().getSelectionModel().selected.getAt(0);\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "//close window on cancel\r",
                    "this.control({\r",
                    "    '#addToRuleCancelButton': {\r",
                    "        click: function() {\r",
                    "            this.getAddRoleToRuleWindowClass().destroy();\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "//clear selection\r",
                    "this.control({\r",
                    "    '#addToRuleClearButton': {\r",
                    "        click: function() {\r",
                    "            this.getAddToRuleTreePanel().getSelectionModel().deselectAll();\r",
                    "            this.getAddToRuleClearButton().blur();\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "//add selection\r",
                    "this.control({\r",
                    "    '#addToRuleAddButton': {\r",
                    "        click: function() {\r",
                    "            this.addSelection();\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "//search\r",
                    "this.control({\r",
                    "    '#addToRuleSearchField': {\r",
                    "        specialkey: function(field, e) {\r",
                    "            if (e.getKey() == e.ENTER) {\r",
                    "                this.search();\r",
                    "            }\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "\r",
                    "//add listener to treegrid\r",
                    "this.control({\r",
                    "    '#addRoleToRuleTreePanel': {\r",
                    "        selectionchange: function(model, selected, eOpts) {\r",
                    "            this.selectionHasChanged(model, selected, eOpts);\r",
                    "        }\r",
                    "    }\r",
                    "});\r",
                    "//add doubleclick listener to view\r",
                    "this.control({\r",
                    "\t'#addRoleToRiskView': {\r",
                    "\tbeforeitemdblclick: function(dataview, record, index, e, eOpts) {\r",
                    "\t\t\tvar selectionmodel = dataview.getSelectionModel();\r",
                    "\t\t\tif(selectionmodel.isSelected(record)) {\r",
                    "\t\t\t\tselectionmodel.deselect(record);\r",
                    "\t\t\t\trecord.eachChild(function (n) {\r",
                    "\t\t\t\t\tselectionmodel.deselect(n, true); //true in order to keep existing selections\r",
                    "\t\t\t\t});\r",
                    "\t\t\t\trecord.collapse();\r",
                    "\t\t\t} else {\r",
                    "\t\t\t\tselectionmodel.select(record, true);\r",
                    "\t\t\t\trecord.eachChild(function (n) {\r",
                    "\t\t\t\t\tselectionmodel.select(n, true); //true in order to keep existing selections\r",
                    "\t\t\t\t});\r",
                    "\t\t\t\trecord.expand();\r",
                    "\t\t\t}\r",
                    "\r",
                    "\t\t\treturn false;    //return false in order to NOT to collapse tree on dblClick\r",
                    "\t\t}\r",
                    "\t}\r",
                    "});\r",
                    ""
                ]
            },
            "designerId": "825629eb-8299-4957-b65d-2c637b8a8133"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "removeRole",
                "implHandler": [
                    "var node = this.getMainTreeGrid().getSelectionModel().selected.getAt(0);\r",
                    "var role = node.data.name;\r",
                    "var rule = node.parentNode.data.name;\r",
                    "var question = CCS.Translator.getText(\"Are you sure you want to remove {1} from {2}?\", [role, rule]);\r",
                    "var store = this.getMainTreeGrid().getStore();\r",
                    "Ext.MessageBox.confirm(CCS.Translator.getText('Remove role'), question, function(btn){\r",
                    "    if(btn === 'yes'){\r",
                    "\r",
                    "\t\tstore.suspendAutoSync();\r",
                    "\r",
                    "        node.set(\"ruleId\", \"remove\");\r",
                    "        node.set(\"origin\", node.parentNode.data.id);\r",
                    "        node.remove(false);\r",
                    "\r",
                    "\t\tstore.resumeAutoSync();\r",
                    "\t\tstore.sync();\r",
                    "    }\r",
                    "});"
                ]
            },
            "designerId": "97cec152-cd7a-4a0e-8189-cc5b33966d47"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "addSelection",
                "implHandler": [
                    "this.getAddToRuleAddButton().blur();\r",
                    "this.getAddToRuleAddButton().disable();\r",
                    "this.getAddRoleToRuleWindowClass().disable();\r",
                    "this.getMainTreeGrid().suspendLayouts();\r",
                    "this.getMainTreeGrid().suspendLayout = false;\r",
                    "\r",
                    "var selection = this.getAddToRuleTreePanel().getSelectionModel().getSelection();\r",
                    "this.getAddToRuleTreePanel().getSelectionModel().deselectAll();\r",
                    "\r",
                    "//var rule\t= this.getMainTreeGrid().getSelectionModel().selected.getAt(0);\r",
                    "var myModel\t= this.getMainTreeGrid().getStore().model;\r",
                    "\r",
                    "var l = selection.length;\r",
                    "\r",
                    "this.portionSize = 15;\r",
                    "this.portions = Math.floor(l / this.portionSize);\r",
                    "this.rest = l-(this.portions * this.portionSize);\r",
                    "this.newRecords = [];\r",
                    "this.currentPortion = 0;\r",
                    "\r",
                    "for (var i = 0; i < l; i++) {\r",
                    "    var newRecord = new myModel({\r",
                    "        id: selection[i].data.roleid,\r",
                    "        name: selection[i].data.technicalname,\r",
                    "        response: selection[i].data.rolename,\r",
                    "        ruleId: \"add\",\r",
                    "        origin: this.rule.get('id'),\r",
                    "        leaf: true,\r",
                    "\t\ticon: selection[i].data.icon\r",
                    "    });\r",
                    "\tthis.newRecords[i] = newRecord;\r",
                    "}\r",
                    "if(l > 20) {\r",
                    "\tthis.makePortion(this.rule);\r",
                    "} else {\r",
                    "\tthis.addPortion(this.rule, this.newRecords, true);\r",
                    "}"
                ]
            },
            "designerId": "fadb226c-d554-4b2f-bd92-ac2c1ea84d40"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "rule"
                ],
                "fn": "makePortion",
                "implHandler": [
                    "var interval = (this.currentPortion < this.portions)?\t(this.currentPortion*this.portionSize+1) + \" - \"+(this.currentPortion*this.portionSize+this.portionSize) :\r",
                    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t(this.portions*this.portionSize+1) + \" - \" + (this.portions*this.portionSize+this.rest);\r",
                    "var portion = [];\r",
                    "if(this.currentPortion < this.portions) {\r",
                    "\tfor(var i = 0; i < this.portionSize; i++) {\r",
                    "\t\tportion[i] = this.newRecords[i+(this.currentPortion*this.portionSize)];\r",
                    "\t}\r",
                    "\tthis.currentPortion++;\r",
                    "\tvar last = (this.rest === 0 && this.currentPortion == this.portions);\r",
                    "} else {\r",
                    "\tfor(var i = this.portions*this.portionSize; i < (this.portions*this.portionSize)+this.rest; i++) {\r",
                    "\t\tportion.push(this.newRecords[i]);\r",
                    "\t}\r",
                    "\tvar last = true;\r",
                    "}\r",
                    "MyApp.app.message(CCS.Translator.getText('Adding roles to risk definition'), CCS.Translator.getText('Now adding {1} of the {2} selected roles', [interval, this.newRecords.length]));\r",
                    "this.addPortion(rule, portion, last);"
                ]
            },
            "designerId": "b46de74f-619c-47ed-8eb4-ba91e1911568"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "rule",
                    "newRecords",
                    "last"
                ],
                "fn": "addPortion",
                "implHandler": [
                    "var store\t= this.getMainTreeGrid().getStore();\r",
                    "store.suspendAutoSync();\r",
                    "\r",
                    "var l = newRecords.length;\r",
                    "for (var i = 0; i < l; i++) {\r",
                    "\trule.appendChild(newRecords[i]);\r",
                    "}\r",
                    "\r",
                    "var scope = this;\r",
                    "store.resumeAutoSync();\r",
                    "if(last === true) {\r",
                    "\tstore.sync({\r",
                    "\t\tcallback: function() {\r",
                    "\t\t\tMyApp.app.message(CCS.Translator.getText('Adding roles to risk definition'), CCS.Translator.getText('Done!'));\r",
                    "\t\t\tdelete(store.getProxy().extraParams.rule_id);\r",
                    "\t\t\tstore.load({\r",
                    "\t\t\t\tcallback: function() {\r",
                    "\t\t\t\t\tscope.rule = scope.getMainTreeGrid().getStore().getById(rule.get('tree_id'));\r",
                    "\r",
                    "\t\t\t\t\tscope.getAddRoleToRuleWindowClass().enable();\r",
                    "\t\t\t\t\tscope.getMainTreeGrid().suspendLayout = false;\r",
                    "\t\t\t\t\tscope.getMainTreeGrid().resumeLayouts();\r",
                    "\t\t\t\t\tscope.getMainTreeGrid().doLayout();\r",
                    "\t\t\t\t\tscope.getMainTreeGrid().getSelectionModel().deselectAll();\r",
                    "\t\t\t\t\tscope.getMainTreeGrid().getSelectionModel().doSelect(scope.rule);\r",
                    "\t\t\t\t}\r",
                    "\t\t\t});\r",
                    "\t\t}\r",
                    "\t});\r",
                    "} else {\r",
                    "\tstore.sync({\r",
                    "\t\tcallback: function() {\r",
                    "\t\t\tscope.makePortion(rule);\r",
                    "\t\t}\r",
                    "\t});\r",
                    "}"
                ]
            },
            "designerId": "e69829db-12a5-49c4-bdf4-fafdc6da2bb1"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "search",
                "implHandler": [
                    "this.getAddToRuleTreePanel().getSelectionModel().deselectAll();\r",
                    "var store = this.getAddToRuleTreePanel().getStore();\r",
                    "var criteria = this.getAddToRuleSearchField().getValue();\r",
                    "\r",
                    "store.proxy.extraParams.rolefilter = criteria;\r",
                    "store.load();"
                ]
            },
            "designerId": "dd253313-f02c-4a6a-a409-7933d435b01e"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "model",
                    "selected",
                    "eOpts"
                ],
                "fn": "selectionHasChanged",
                "implHandler": [
                    "this.getAddToRuleClearButton().disable();\r",
                    "this.getAddToRuleAddButton().disable();\r",
                    "if(!Ext.isEmpty(selected)) {\r",
                    "    this.getAddToRuleClearButton().enable();\r",
                    "    this.getAddToRuleAddButton().enable();\r",
                    "}"
                ]
            },
            "designerId": "aa2c8b50-6bda-434c-8276-b5983cfebd00"
        }
    ]
}