/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

function createMenu(menuItemList, isContextMenu, pageItem, page) {
    var parent = page;
    var menuItem = "ContextMenu";
    if((isContextMenu == undefined) || !isContextMenu)
        menuItem = "Menu";
    var strMenu = "import QtQuick 1.1;\n"
        + "import com.nokia.meego 1.0;\n"
        + "import WebFeeds 1.0;\n"
        + "import '/menu_items' as MenuItems;\n"
        + "Component {\n"
        + menuItem + "{\n"
        + "id:menu;\n"
        + "property variant componentObject;\n"
        + "property bool finished: false;\n"
        + "property bool isContextMenu: false;\n"
        + "property CommonPage page;\n"
        + "property PageItem pageItem: null\n"
        + "signal menuDestroyed\n"
        + "onFinishedChanged: {\n"
        + "if(finished && (status == DialogStatus.Closed)) menu.destroy();}\n"
        + "onStatusChanged: {\n"
        + "if(finished && (status == DialogStatus.Closed)) menu.destroy();\n"
        + "else if(status == DialogStatus.Opening) finished = true;}\n"
        + "MenuLayout {\n";
    for(var i = 0; i < menuItemList.length; ++i) {
        strMenu += "MenuItems." + menuItemList[i] + "{property Menu parentMenu:menu;}\n";
    }
    strMenu += "} Component.onDestruction: {menu.componentObject.destroy(); menu.menuDestroyed();}\n";
    strMenu += "}}\n";
    var menuComponent = Qt.createQmlObject(strMenu, parent);
    var menu = menuComponent.createObject(parent);
    menu.componentObject = menuComponent;
    menu.isContextMenu = isContextMenu;
    menu.page = page;
    menu.pageItem = pageItem;
    return menu;
}
