Index
ht.widget.Menu can be added to the standard menu for the page, composed of a list of ul and ContextMenu, with the help of ContextMenu can implement any level submenu, picture ICON, Check multiple selection, Radio single selection, disabling menu items and customizing menu styles can refer to their documentation for a detailed description of ContextMenu.
Before you formally use API, you should introduce relevant js files on your page:
<script src="ht.js"></script> <!-- Intorduce ht.js first -->
<script src="key.js"></script> <!-- Shortcut key constants, you can introduce-on demand -->
<script src="ht-contextmenu.js"></script> <!-- Relay on contextmenu-->
<script src="ht-menu.js"></script>
Menu provides the API as follows:
setItems(json) Sets menu item, parameter is JSON objectsetLayout(layout) Sets layout mode, largeicons represents large icon mode, smallicons represents small icon mode, iconsonly represents only icon modesetHeaderItemHGap() Sets the horizontal interval of the root menu item, the defaults is 0, changes the layout to achieve the best display effectsetHeaderItemVGap() Sets the vertical interval of the root menu item, the defaults is 0, changes the layout to achieve the best display effectsetHeaderItemAlign() Sets the alignment of the root menu item, the default is left, changes layout to adjust this parameter to achieve the best display effectaddTo(dom) Parameter is HTML element, the menu as its childdispose() Destroy this menuenableGlobalKey() Enables global shortcut keys, and once this option is enabled, you need to explicitly call dispose() to destroy menu when the menu is no longer in usedisableGlobalKey() Disables global shortcut keyssetHoverBackground(color) Sets the background when the mouse hover the root menusetHoverColor(color) Sets the text color when the mouse hover the root menushowDropdownMenu(index) Shows the drop-down menu, the parameter is the index of the root menu item, to show the drop-down menu of the first root menu item, you can call showDropdownMenu(0)hideDropdownMenu() Hide drop-down menugetItemByProperty(property, value) Looks for a menu item with the attribute name properties and with value value, returning only the first lookup result, note: If the menu is displayed to modify this lookup, the menu interface will be updated the next time displaygetShowingMenuIndex() Gets the index of the root menu item that is currently displaying the drop-down menu, and if no drop-down menu returns -1The contents of the menu are described by a fixed format JSON object that includes the text of the menu item, the picture, and the corresponding action, and the following JSON describes a typical menu:
var json = [
{
label: "File", //Menu label
items: [
{
label: "New...",
icon: iconSrc, //Menu ICON
action: function(item) { //Callback function
alert(item.label);
}
},
{
label: "Open...",
icon: iconSrc,
suffix: "Ctrl+O", //Tooltip displays in menu
key: [Key.ctrl, Key.o], //Shortcut key for actual response
action: function(item) {
alert("you clicked:" + item.label + ",this=" + this);
},
scope: "hello" //Specify this in the callback function
},
{
label: "Disabled",
icon: iconSrc,
disabled: true //Disable menu item
},
"separator", //Split Line
{
label: "More...",
icon: iconSrc,
type: "check", //Multiple-choice menu item
items: [
{
label: "AAAA"
},
{
label: "BBBB"
},
{
label: "CCCC"
}
]
}
]
},
{
label: "Edit",
items:[
{
label: "Copy",
icon: iconSrc
},
{
label: "Paste",
icon: iconSrc
}
]
},
{
label: "CheckMenuItems",
items: [
{
label: "Check1",
icon: iconSrc,
type: "check"
},
{
label: "Check2",
icon: iconSrc,
type: "check"
},
{
label: "Check3",
icon: iconSrc,
type: "check",
items: [
{
label: "AAAA"
},
{
label: "BBBB"
},
{
label: "CCCC"
}
]
}
],
action: function(item) {
alert("you clicked:" + item.label + ",this=" + this);
}
},
{
label: "RadioMenuItems",
action: function(item,event) {
alert("you clicked:" + item.label);
},
items: [
{
label: "Radio1",
icon: iconSrc,
type: "radio",//Radio menu item
groupId: 1 //Menu grouping
},
{
label: "Radio2",
icon: iconSrc,
type: "radio",
groupId: 1
},
{
label: "Radio3",
icon: iconSrc,
type: "radio",
groupId: 1,
items: [
{
label: "AAAA"
},
{
label: "BBBB"
},
{
label: "CCCC"
}
]
}
]
},
{
label: "TestMenu",
icon: iconSrc,
action: function(item) {
alert(item.label);
},
items: [
{
label: "Homepage",
href: "http://www.hightopo.com", //Hyperlink to a URL
linkTarget: "_blank", //Hyperlink target, default _self
key: [Key.meta, Key.enter], //Shortcut key for the actual response
suffix: "Command+Enter", //Tooltop displayed on the menu
preventDefault: false //Whether to prevent shortcut key default behavior, default is true
},
{
label: "submenu",
action: function(item) {
alert(item.label);
}
}
]
}
];
This JSON object renders the menu, see the following example:
Similar to the right-click menu, the menu supports the global htconfig configuration style and configurable parameters as follows:
menuLabelFont Menu label font, the default is (isTouchable ? '16' : '13') + 'px arial, sans-serif' menuLabelColor Menu text color, the default is #000,menuBackground Menu background, the default is #F0EFEEmenuHoverBackground Background color when mouse hover the menu item, the default is #648BFEmenuHoverLabelColor Label color when mouse hover the menu item, the default is #fffmenuSeparatorWidth The width of the bottom menu split line, the default is 1menuSeparatorColor The color of the bottom menu split line, the default is #999In addition to the htconfig object, you can also use css to override the default style, refer to the following example:
Usually the drop-down menu is clicked to trigger the display, in addition, Menu also supports the display and hide drop-down menu through API control:
<input type="button" value="Show" onclick="window.menu.showDropdownMenu(document.getElementById('txt').value);">
<input type="button" value="Hide" onclick="window.menu.hideDropdownMenu();">
<input type="button" value="PrintIndex" onmousedown="alert(window.menu.getShowingMenuIndex());">
In common, a single-page application allows the user to complete all the operations on a single page, and its introduction can be referenced here: Single-Page Application, for an application that comes with a standard menu, you usually need to place the menu at the top of the page and then fill the rest of the area with other components, and we'll simulate the following features: