def add_context_menu(self, header_div):
    from .menu_wdg import Menu, MenuItem
    menu = Menu(width=180)
    menu_item = MenuItem(type='title', label='Actions')
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Reload Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n\n            var header = activator;\n            var class_name = header.getAttribute("spt_class_name");\n            var kwargs_str = header.getAttribute("spt_kwargs");\n            var kwargs;\n            if (kwargs_str != \'\') {\n                kwargs_str = kwargs_str.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs_str);\n            }\n            else {\n                kwargs = {};\n            }\n\n\n            var values = null;\n            var force = true;\n            spt.tab.load_class(header, class_name, kwargs, values, force);\n\n            \n            '})
    menu.add(menu_item)
    menu_item = MenuItem(type='separator')
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Rename Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var class_name = \'tactic.ui.container.TabRenameWdg\';\n            var kwargs = {};\n\n            var activator = spt.smenu.get_activator(bvr);\n            var label = activator.getElement(".spt_tab_header_label");\n            name = label.innerHTML;\n\n            title = "Raname Tab ["+name+"]";\n            var popup = spt.panel.load_popup(title, class_name, kwargs);\n            popup.activator = activator;\n\n\n            '})
    menu.add(menu_item)
    '\n        menu_item = MenuItem(type=\'action\', label=\'New Tab\')\n        menu_item.add_behavior( {\n            \'cbjs_action\': \'\'\'\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n            spt.tab.add_new();\n            \'\'\'\n        } )\n        menu.add(menu_item)\n        '
    menu_item = MenuItem(type='action', label='Tear Off')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n\n            // add new if this is the last oni\n            var headers = spt.tab.get_headers();\n            if (headers.length == 1) {\n                spt.tab.add_new();\n            }\n            \n            spt.tab.top = top;\n\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            spt.behavior.destroy_element(header);\n\n            var contents = spt.tab.get_contents();\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("spt_element_name") == element_name) {\n                    spt.panel.load_popup_with_html( element_name, content.innerHTML );\n                    spt.behavior.destroy_element(content);\n                }\n            } \n\n            '})
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Copy To Main Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n\n            var html;\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            var title = header.getAttribute("spt_title");\n\n            var class_name = header.getAttribute("spt_class_name");\n            var kwargs_str = header.getAttribute("spt_kwargs");\n            var kwargs = {};\n            if (kwargs_str) {\n                kwargs_str = kwargs_str.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs_str);\n            }\n            var contents = spt.tab.get_contents();\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("spt_element_name") == element_name) {\n                    html = content.innerHTML;\n                    break;\n                }\n            }\n            spt.tab.set_main_body_tab();\n            spt.tab.add_new(element_name, title, class_name, kwargs);\n            '})
    menu.add(menu_item)
    if (self.kwargs.get('show_remove') not in ['false', False]):
        menu_item = MenuItem(type='separator')
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Close Tab')
        menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            spt.behavior.destroy_element(header);\n\n            var contents = top.getElements(".spt_tab_content");\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("element_name") == element_name) {\n                    spt.behavior.destroy_element(content);\n                }\n            }\n            '})
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Close All Except This Tab')
        menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n            var headers = spt.tab.get_headers();\n            for (var i=0; i < headers.length; i++) {\n                var element_name = headers[i].getAttribute("spt_element_name");\n                if (activator.getAttribute(\'spt_element_name\') != element_name) {\n                    spt.tab.close(headers[i]);\n                }\n\n            }\n\n            var element_name = activator.getAttribute("spt_element_name");\n            spt.tab.select(element_name);\n\n            '})
        menu.add(menu_item)
    security = Environment.get_security()
    if security.check_access('builtin', 'view_site_admin', 'allow'):
        menu_item = MenuItem(type='separator')
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='View Definition')
        menu_item.add_behavior({'cbjs_action': 'spt.tab.view_definition(bvr);'})
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Add to Side Bar')
        menu_item.add_behavior({'cbjs_action': '\n                var activator = spt.smenu.get_activator(bvr);\n                var top = activator.getParent(".spt_tab_top");\n                spt.tab.top = top;\n\n                var header = activator;\n                var element_name = header.getAttribute("spt_element_name");\n                var title = header.getAttribute("spt_title");\n\n                var kwargs = header.getAttribute("spt_kwargs");\n                kwargs = kwargs.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs);\n\n                var view = element_name;\n                var element_name = element_name.replace(/ /g, "_");\n                element_name = element_name.replace(/\\//g, "_");\n\n                var kwargs = {\n                    class_name: \'LinkWdg\',\n                    display_options: kwargs,\n                    element_attrs: {\n                        title: title\n                    }\n                }\n\n                try {\n                    var server = TacticServerStub.get();\n                    var info = server.add_config_element("SideBarWdg", "definition", element_name, kwargs);\n                    var info = server.add_config_element("SideBarWdg", "project_view", element_name, kwargs);\n\n                    spt.panel.refresh("side_bar");\n                }\n                catch(e) {\n                    alert(e);\n                    throw(e);\n                }\n\n                '})
        menu.add(menu_item)
    has_my_views = True
    if has_my_views:
        menu_item = MenuItem(type='action', label='Add to My Views')
        menu_item.add_behavior({'cbjs_action': '\n                var activator = spt.smenu.get_activator(bvr);\n                var top = activator.getParent(".spt_tab_top");\n                spt.tab.top = top;\n\n                var header = activator;\n                var element_name = header.getAttribute("spt_element_name");\n                var title = header.getAttribute("spt_title");\n\n                var kwargs = header.getAttribute("spt_kwargs");\n                kwargs = kwargs.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs);\n\n\n                var login = \'admin\';\n\n                var class_name = kwargs.class_name;\n                if (!class_name) {\n                    class_name = "tactic.ui.panel.CustomLayoutWdg";\n                }\n\n\n                var view = element_name;\n                var element_name = element_name.replace(/ /g, "_");\n                element_name = element_name.replace(/\\//g, "_");\n\n                element_name = login + "." + element_name;\n\n\n                var kwargs = {\n                    class_name: class_name,\n                    display_options: kwargs,\n                    element_attrs: {\n                        title: title\n                    },\n                    login: login,\n                    unique: false,\n                }\n\n\n\n                var view = "self_view_" + login;\n\n                try {\n\n                    var server = TacticServerStub.get();\n                    var info = server.add_config_element("SideBarWdg", "definition", element_name, kwargs);\n                    var info = server.add_config_element("SideBarWdg", view, element_name, kwargs);\n\n                    spt.panel.refresh("side_bar");\n                }\n                catch(e) {\n                    alert(e);\n                    throw(e);\n                }\n\n                '})
        menu.add(menu_item)
    if (self.kwargs.get('show_context_menu') not in ['false', False]):
        menus = [menu.get_data()]
        menus_in = {'DG_HEADER_CTX': menus}
        from .smart_menu_wdg import SmartMenu
        SmartMenu.attach_smart_context_menu(header_div, menus_in, False)
def add_context_menu(self, header_div):
    from .menu_wdg import Menu, MenuItem
    menu = Menu(width=180)
    menu_item = MenuItem(type='title', label='Actions')
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Reload Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n\n            var header = activator;\n            var class_name = header.getAttribute("spt_class_name");\n            var kwargs_str = header.getAttribute("spt_kwargs");\n            var kwargs;\n            if (kwargs_str != \'\') {\n                kwargs_str = kwargs_str.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs_str);\n            }\n            else {\n                kwargs = {};\n            }\n\n\n            var values = null;\n            var force = true;\n            spt.tab.load_class(header, class_name, kwargs, values, force);\n\n            \n            '})
    menu.add(menu_item)
    menu_item = MenuItem(type='separator')
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Rename Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var class_name = \'tactic.ui.container.TabRenameWdg\';\n            var kwargs = {};\n\n            var activator = spt.smenu.get_activator(bvr);\n            var label = activator.getElement(".spt_tab_header_label");\n            name = label.innerHTML;\n\n            title = "Raname Tab ["+name+"]";\n            var popup = spt.panel.load_popup(title, class_name, kwargs);\n            popup.activator = activator;\n\n\n            '})
    menu.add(menu_item)
    '\n        menu_item = MenuItem(type=\'action\', label=\'New Tab\')\n        menu_item.add_behavior( {\n            \'cbjs_action\': \'\'\'\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n            spt.tab.add_new();\n            \'\'\'\n        } )\n        menu.add(menu_item)\n        '
    menu_item = MenuItem(type='action', label='Tear Off')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n\n            // add new if this is the last oni\n            var headers = spt.tab.get_headers();\n            if (headers.length == 1) {\n                spt.tab.add_new();\n            }\n            \n            spt.tab.top = top;\n\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            spt.behavior.destroy_element(header);\n\n            var contents = spt.tab.get_contents();\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("spt_element_name") == element_name) {\n                    spt.panel.load_popup_with_html( element_name, content.innerHTML );\n                    spt.behavior.destroy_element(content);\n                }\n            } \n\n            '})
    menu.add(menu_item)
    menu_item = MenuItem(type='action', label='Copy To Main Tab')
    menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n\n            var html;\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            var title = header.getAttribute("spt_title");\n\n            var class_name = header.getAttribute("spt_class_name");\n            var kwargs_str = header.getAttribute("spt_kwargs");\n            var kwargs = {};\n            if (kwargs_str) {\n                kwargs_str = kwargs_str.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs_str);\n            }\n            var contents = spt.tab.get_contents();\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("spt_element_name") == element_name) {\n                    html = content.innerHTML;\n                    break;\n                }\n            }\n            spt.tab.set_main_body_tab();\n            spt.tab.add_new(element_name, title, class_name, kwargs);\n            '})
    menu.add(menu_item)
    if (self.kwargs.get('show_remove') not in ['false', False]):
        menu_item = MenuItem(type='separator')
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Close Tab')
        menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n            var header = activator;\n            var element_name = header.getAttribute("spt_element_name");\n            spt.behavior.destroy_element(header);\n\n            var contents = top.getElements(".spt_tab_content");\n            for (var i=0; i<contents.length; i++) {\n                var content = contents[i];\n                if (content.getAttribute("element_name") == element_name) {\n                    spt.behavior.destroy_element(content);\n                }\n            }\n            '})
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Close All Except This Tab')
        menu_item.add_behavior({'cbjs_action': '\n            var activator = spt.smenu.get_activator(bvr);\n            var top = activator.getParent(".spt_tab_top");\n            spt.tab.top = top;\n\n            var headers = spt.tab.get_headers();\n            for (var i=0; i < headers.length; i++) {\n                var element_name = headers[i].getAttribute("spt_element_name");\n                if (activator.getAttribute(\'spt_element_name\') != element_name) {\n                    spt.tab.close(headers[i]);\n                }\n\n            }\n\n            var element_name = activator.getAttribute("spt_element_name");\n            spt.tab.select(element_name);\n\n            '})
        menu.add(menu_item)
    security = Environment.get_security()
    if security.check_access('builtin', 'view_site_admin', 'allow'):
        menu_item = MenuItem(type='separator')
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='View Definition')
        menu_item.add_behavior({'cbjs_action': 'spt.tab.view_definition(bvr);'})
        menu.add(menu_item)
        menu_item = MenuItem(type='action', label='Add to Side Bar')
        menu_item.add_behavior({'cbjs_action': '\n                var activator = spt.smenu.get_activator(bvr);\n                var top = activator.getParent(".spt_tab_top");\n                spt.tab.top = top;\n\n                var header = activator;\n                var element_name = header.getAttribute("spt_element_name");\n                var title = header.getAttribute("spt_title");\n\n                var kwargs = header.getAttribute("spt_kwargs");\n                kwargs = kwargs.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs);\n\n                var view = element_name;\n                var element_name = element_name.replace(/ /g, "_");\n                element_name = element_name.replace(/\\//g, "_");\n\n                var kwargs = {\n                    class_name: \'LinkWdg\',\n                    display_options: kwargs,\n                    element_attrs: {\n                        title: title\n                    }\n                }\n\n                try {\n                    var server = TacticServerStub.get();\n                    var info = server.add_config_element("SideBarWdg", "definition", element_name, kwargs);\n                    var info = server.add_config_element("SideBarWdg", "project_view", element_name, kwargs);\n\n                    spt.panel.refresh("side_bar");\n                }\n                catch(e) {\n                    alert(e);\n                    throw(e);\n                }\n\n                '})
        menu.add(menu_item)
    has_my_views = True
    if has_my_views:
        menu_item = MenuItem(type='action', label='Add to My Views')
        menu_item.add_behavior({'cbjs_action': '\n                var activator = spt.smenu.get_activator(bvr);\n                var top = activator.getParent(".spt_tab_top");\n                spt.tab.top = top;\n\n                var header = activator;\n                var element_name = header.getAttribute("spt_element_name");\n                var title = header.getAttribute("spt_title");\n\n                var kwargs = header.getAttribute("spt_kwargs");\n                kwargs = kwargs.replace(/\\&amp;quot\\;/g, \'"\');\n                kwargs = JSON.parse(kwargs);\n\n\n                var login = \'admin\';\n\n                var class_name = kwargs.class_name;\n                if (!class_name) {\n                    class_name = "tactic.ui.panel.CustomLayoutWdg";\n                }\n\n\n                var view = element_name;\n                var element_name = element_name.replace(/ /g, "_");\n                element_name = element_name.replace(/\\//g, "_");\n\n                element_name = login + "." + element_name;\n\n\n                var kwargs = {\n                    class_name: class_name,\n                    display_options: kwargs,\n                    element_attrs: {\n                        title: title\n                    },\n                    login: login,\n                    unique: false,\n                }\n\n\n\n                var view = "self_view_" + login;\n\n                try {\n\n                    var server = TacticServerStub.get();\n                    var info = server.add_config_element("SideBarWdg", "definition", element_name, kwargs);\n                    var info = server.add_config_element("SideBarWdg", view, element_name, kwargs);\n\n                    spt.panel.refresh("side_bar");\n                }\n                catch(e) {\n                    alert(e);\n                    throw(e);\n                }\n\n                '})
        menu.add(menu_item)
    if (self.kwargs.get('show_context_menu') not in ['false', False]):
        menus = [menu.get_data()]
        menus_in = {'DG_HEADER_CTX': menus}
        from .smart_menu_wdg import SmartMenu
        SmartMenu.attach_smart_context_menu(header_div, menus_in, False)