Splitbutton with medialize - jQuery contextMenu
Very basic splitbutton with all the features of the medialize contextMenu.
Thanks again to Rodney for his help
See http://medialize.github.com/jQuery-contextMenu/
HTML
<script src="https://raw.github.com/medialize/jQuery-contextMenu/gh-pages/src/jquery.contextMenu.js"></script>
<h2 id="headline">Poor mans Splitbutton with context menu</h2>
<div>
<p>This is the basic setup for a simple splitbutton. On the dark grey area the contextmenu will be triggered. On the lighter grey the normal button action can be triggered. For instance adding the number of items from the textbox into the shopping cart. If the user picks the splitbutton side he can pick any other quantity without typing it into the textbox</p>
</div>
<div class="c">
Asus Transformer Prime <input type=text class=qty value=4>
<a id="a1" class="Btn"><span>buy</span><span class="splBtn">^^</span></a>
<br />
Apple iRon <input type=text class=qty value=0>
<a id="a2" class="Btn"><span>buy</span><span class="splBtn">^^</span></a>
</div>
<div><p>To retrieve the value of checkbox after the user has changed the value you can use the build in function var input = $.contextMenu.getInputValues(options);</p></div>
<h2>Clicked item</h2>
<div id="out"></div>
CSS
h2 { margin: 2em 1em 1em 1em; font-size: 24px;}
div { color: black; padding: 0 2em; }
p { margin-top: 2em;}
input { width: 2em;}
.c {line-height: 1.85em; text-align: right; margin: 2em 14em 0 0;}
a { width: 200px;text-decoration: none; padding:0; color: blue;}
.splBtn { background: #BDBDBD; padding:0 0.5em;}
.Btn{background: #D8D8D8}
.Btn span { padding:0 0.5em}
.b2 {background: #F2F2F2; padding:0; }
/*
Standard Contextmenu Styles
*/
.context-menu-list {
margin:0;
padding:0;
min-width: 120px;
max-width: 250px;
display: inline-block;
position: absolute;
list-style-type: none;
border: 1px solid #DDD;
background: #EEE;
-webkit-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
-ms-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
-o-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
}
.context-menu-item {
padding: 2px 2px 2px 24px;
background-color: #EEE;
position: relative;
-moz-user-select: -moz-none;
}
.context-menu-separator {
padding-bottom:0;
border-bottom: 1px solid #DDD;
}
.context-menu-item > label {
-moz-user-select: text;
}
.context-menu-item.hover {
cursor: pointer;
background-color: #39F;
}
.context-menu-item.disabled {
color: #666;
}
.context-menu-input.hover,
.context-menu-item.disabled.hover {
cursor: default;
background-color: #EEE;
}
.context-menu-submenu:after {
content: ">";
color: #666;
position: absolute;
right: 3px;
}
/* icons
#protip:
In case you want to use sprites for icons (which I would suggest you do) have a look at
http://css-tricks.com/13224-pseudo-spriting/ to get an idea of how to implement
.context-menu-item.icon:before {}
*/
.context-menu-item.icon { height: 18px; background-repeat: no-repeat;...
JavaScript
/*
* Simple Splitbutton with jQuery contextMenu (by medialize)
*
* trying to get the callback working with the changed checkbox value
*/
function log(log_txt) {
if (window.console != undefined) {
console.log(log_txt);
return false;
}
}
function cmCallback(key, opt){
var items = $.contextMenu.getInputValues(opt);
var id = opt.$trigger.context.id;
log("key: "); log(key);
log("opt: "); log(opt);
log("id : "); log(id);
$("#out").text("button clicked: " + id + " item clicked " + key);
}
function getItems(){
var items = {
productQty1: {name: "buy 1 item", callback: function(key, opt){ cmCallback(key, opt) }},
productQty2: { name: "buy 2 items", callback: function(key, opt){ cmCallback(key, opt) }},
productQty4: { name: "buy 4 items" },
productQty8: { name: "buy 8 items" },
yesnoWarranty: { name: "extended warranty", type: 'checkbox', selected: true },
yesnoOff: { name: "gift wrap", type: 'checkbox', selected: false },
key: { name: "Close", callback: $.noop }
};
return items;}
function getMenuOptions(s){
var o = { selector: s, items: getItems(), autoHide: true,
callback: function(key, options) {
var input = $.contextMenu.getInputValues(options);
alert("extra warranty = " + input.yesnoWarranty);
}
};
return o; }
function getPosition(o){
var p = o.position();
var pos ={};
pos.x = p.left;
pos.y = p.top + o.innerHeight();
return pos;}
$(function(){
$('a.Btn').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
alert("Add 4 / 0 items to shopping cart");
})
// make the split part of the button open the menu
$('span.splBtn').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var btnLink = $(this).parent();
var s = "#" +...