snippets / proto.menu.0.6.js

Language: Javascript - First posted by willowdan on 2008-01-22 04:48 (8 months, 3 weeks)
Link to the snippet: http://www.friendsnippets.org/snippet/163/

proto.menu.0.6.js

  1 /**
2 * @description prototype.js based context menu
3 * @author Juriy Zaytsev; kangax [at] gmail [dot] com; http://thinkweb2.com/projects/prototype/
4 * @version 0.6
5 * @date 12/03/07
6 * @requires prototype.js 1.6
7 */
8
9 if (Object.isUndefined(Proto)) { var Proto = { } }
10
11 Proto.Menu = Class.create({
12 initialize: function() {
13 var e = Prototype.emptyFunction;
14 this.ie = Prototype.Browser.IE;
15 this.options = Object.extend({
16 selector: '.contextmenu',
17 className: 'protoMenu',
18 pageOffset: 25,
19 fade: false,
20 zIndex: 100,
21 beforeShow: e,
22 beforeHide: e,
23 beforeSelect: e
24 }, arguments[0] || { });
25
26 this.shim = new Element('iframe', {
27 style: 'position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);display:none',
28 src: 'javascript:false;',
29 frameborder: 0
30 });
31
32 this.options.fade = this.options.fade && !Object.isUndefined(Effect);
33 this.container = new Element('div', {className: this.options.className, style: 'display:none'});
34 var list = new Element('ul');
35 this.options.menuItems.each(function(item) {
36 list.insert(
37 new Element('li', {className: item.separator ? 'separator' : ''}).insert(
38 item.separator
39 ? ''
40 : Object.extend(new Element('a', {
41 href: '#',
42 title: item.name,
43 id: item.name+'_rc',/*dcj added id*/
44 className: (item.className || '') + (item.disabled ? ' disabled' : ' enabled')
45 }), { _callback: item.callback })
46 .observe('click', this.onClick.bind(this))
47 .observe('contextmenu', Event.stop)
48 .update(item.name)
49 )
50 )
51 }.bind(this));
52 $(document.body).insert(this.container.insert(list).observe('contextmenu', Event.stop));
53 if (this.ie) { $(document.body).insert(this.shim) }
54
55 document.observe('click', function(e) {
56 if (this.container.visible() && !e.isRightClick()) {
57 this.options.beforeHide(e);
58 if (this.ie) this.shim.hide();
59 this.container.hide();
60 }
61 }.bind(this));
62
63 $$(this.options.selector).invoke('observe', Prototype.Browser.Opera ? 'click' : 'contextmenu', function(e){
64 if (Prototype.Browser.Opera && !e.ctrlKey) {
65 return;
66 }
67 this.show(e);
68 }.bind(this));
69 },
70 show: function(e) {
71 e.stop();
72
73 /*dcj added next 5 lines*/
74 if ($('Shout!')) {
75 $('Shout_rc').className = 'disabled';
76 } else {
77 $('Shout_rc').className = 'enabled';
78 }
79
80 this.options.beforeShow(e);
81 var x = Event.pointer(e).x,
82 y = Event.pointer(e).y,
83 vpDim = document.viewport.getDimensions(),
84 vpOff = document.viewport.getScrollOffsets(),
85 elDim = this.container.getDimensions(),
86 elOff = {
87 left: ((x + elDim.width + this.options.pageOffset) > vpDim.width
88 ? (vpDim.width - elDim.width - this.options.pageOffset) : x) + 'px',
89 top: ((y - vpOff.top + elDim.height) > vpDim.height && (y - vpOff.top) > elDim.height
90 ? (y - elDim.height) : y) + 'px'
91 };
92 this.container.setStyle(elOff).setStyle({zIndex: this.options.zIndex});
93 if (this.ie) {
94 this.shim.setStyle(Object.extend(Object.extend(elDim, elOff), {zIndex: this.options.zIndex - 1})).show();
95 }
96 this.options.fade ? Effect.Appear(this.container, {duration: 0.25}) : this.container.show();
97 this.event = e;
98 },
99 onClick: function(e) {
100 e.stop();
101 if (e.target._callback && !e.target.hasClassName('disabled')) {
102 this.options.beforeSelect(e);
103 if (this.ie) this.shim.hide();
104 this.container.hide();
105 e.target._callback(this.event);
106 }
107 }
108 })
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Jan '08

Common Tags



snippet History

Jan '08