00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 var SimpleContextMenu = SimpleContextMenu || {};
00037
00038 if (typeof Debug == 'undefined')
00039 alert('ERROR: Debug is undefined! Must include Debug.js before SimpleContextMenu.js');
00040 if (typeof Globals == 'undefined')
00041 alert('ERROR: Globals is undefined! Must include Globals.js before SimpleContextMenu.js');
00042 if (typeof DesktopContent == 'undefined' &&
00043 typeof Desktop == 'undefined')
00044 alert('ERROR: DesktopContent is undefined! Must include DesktopContent.js before SimpleContextMenu.js');
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 SimpleContextMenu._popUpEl = 0;
00058 SimpleContextMenu._menuItemHandlers = [];
00059 SimpleContextMenu._primaryColor = "";
00060 SimpleContextMenu._secondaryColor = "";
00061
00062
00063
00064
00065 SimpleContextMenu.createMenu = function(menuItems,menuItemHandlers,
00066 popupID,topLeftX,topLeftY, primaryColor, secondaryColor) {
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 SimpleContextMenu._menuItemHandlers = menuItemHandlers;
00078 SimpleContextMenu._primaryColor = primaryColor;
00079 SimpleContextMenu._secondaryColor = secondaryColor;
00080
00081 var body = document.getElementsByTagName("BODY")[0];
00082 var el = SimpleContextMenu._popUpEl;
00083 if(el)
00084 {
00085 Debug.log("Can not create SimpleContextMenu if one already exists",
00086 Debug.MED_PRIORITY);
00087 return;
00088 }
00089
00091
00092 el = document.createElement("div");
00093 el.setAttribute("id", popupID);
00094 el.style.display = "none";
00095 el.onmousemove = function(e){e.cancelBubble = true;};
00096 body.appendChild(el);
00097
00098
00100
00101 var css = "";
00102
00103 css += "#clearDiv {" +
00104 "clear: both;" +
00105 "}\n\n";
00106
00107
00108 css += "#" + popupID + "" +
00109 "{" +
00110 "position:absolute;" +
00111 "left:" + topLeftX + "px;" +
00112 "top:" + topLeftY + "px;" +
00113 "z-index: 1000000;" +
00114 "background-color: " + primaryColor + ";" +
00115 "border: 1px solid " + secondaryColor + ";" +
00116 "padding: 5px;" +
00117 "}\n\n";
00118 css += "#" + popupID + " div" +
00119 "{" +
00120 "color: " + secondaryColor + ";" +
00121 "-webkit-user-select: none;" +
00122 "-moz-user-select: none;" +
00123 "user-select: none;" +
00124 "}\n\n";
00125 css += "#" + popupID + " div:hover" +
00126 "{" +
00127
00128 "background-color: " + secondaryColor + ";" +
00129 "color: " + primaryColor + ";" +
00130 "cursor: pointer;" +
00131 "}\n\n";
00132
00133
00134 var style = document.createElement('style');
00135
00136 if (style.styleSheet) {
00137 style.styleSheet.cssText = css;
00138 } else {
00139 style.appendChild(document.createTextNode(css));
00140 }
00141
00142 document.getElementsByTagName('head')[0].appendChild(style);
00143
00144
00146
00147 var str = "";
00148 for(var i=0;i<menuItems.length;++i)
00149 {
00150 str +=
00151 "<div class='SimpleContextMenu-menuItem' " +
00152 "id='SimpleContextMenu-menuItem-" + i + "' " +
00153 "onmousemove='SimpleContextMenu.handleMouseOverMenuItem(event," + i + ");' " +
00154
00155
00156
00157
00158 "onmouseup='SimpleContextMenu.callMenuItemHandler(event," + i + ");' " +
00159 ">" +
00160 menuItems[i] +
00161 "</div>";
00162 str += "<div id='clearDiv'></div>";
00163 }
00164
00165 el.innerHTML = str;
00166 el.style.display = "block";
00167
00168 SimpleContextMenu._popUpEl = el;
00169 }
00170
00171
00172
00173
00174
00175
00176 SimpleContextMenu.createMenuCallAsString = function(menuItems,menuItemHandlers,
00177 popupID, primaryColor, secondaryColor) {
00178
00179 var str = "";
00180 str += "SimpleContextMenu.createMenu([";
00181
00182
00183 for(j=0;j<menuItems.length;++j)
00184 {
00185 if(j)
00186 str += ",";
00187 str += "\"" + menuItems[j] + "\"";
00188 }
00189
00190 str += "]," +
00191 "[";
00192
00193 for(j=0;j<menuItemHandlers.length;++j)
00194 {
00195
00196
00197 menuItemHandlers[j] = menuItemHandlers[j].replace(/\\\"/g, "AAAAA");
00198 menuItemHandlers[j] = menuItemHandlers[j].replace(/"/g, "\\\"");
00199 menuItemHandlers[j] = menuItemHandlers[j].replace(/AAAAA/g, "\\\\\\\"");
00200
00201
00202
00203 str += "\"" + menuItemHandlers[j] + "\"";
00204 if(j != menuItemHandlers.length-1)
00205 str += ",";
00206 }
00207 str += "]" +
00208 ",\"" + popupID + "\",event.pageX-1,event.pageY-1, " +
00209 "\"" + primaryColor +
00210 "\", \"" + secondaryColor + "\");";
00211
00212 return str;
00213 }
00214
00215
00216
00217
00218
00219 SimpleContextMenu.mouseMoveHandler = function(e) {
00220
00221
00222 if(SimpleContextMenu._popUpEl)
00223 {
00224 Debug.log("Removing SimpleContextMenu");
00225 SimpleContextMenu._popUpEl.parentNode.removeChild(SimpleContextMenu._popUpEl);
00226 SimpleContextMenu._popUpEl = 0;
00227 }
00228 }
00229
00230 if(typeof DesktopContent == 'undefined')
00231 Desktop.mouseMoveSubscriber(SimpleContextMenu.mouseMoveHandler);
00232 else
00233 DesktopContent.mouseMoveSubscriber(SimpleContextMenu.mouseMoveHandler);
00234
00235
00236
00237 SimpleContextMenu.callMenuItemHandler = function(event,index) {
00238 var handler = SimpleContextMenu._menuItemHandlers[index];
00239
00240 Debug.log("Removing SimpleContextMenu");
00241 SimpleContextMenu._popUpEl.parentNode.removeChild(SimpleContextMenu._popUpEl);
00242 SimpleContextMenu._popUpEl = 0;
00243
00244 event.cancelBubble = true;
00245 event.preventDefault();
00246
00247
00248 if(handler && (typeof handler) == "string")
00249 {
00250 Debug.log("evaluateJS = " + handler);
00251 eval(handler);
00252 return false;
00253 }
00254 else
00255 {
00256 handler(event, index);
00257 return false;
00258 }
00259 }
00260
00261
00262
00263 SimpleContextMenu.handleMouseOverMenuItem = function(event,index) {
00264 event.cancelBubble = true;
00265
00266
00267
00268
00269 var el;
00270 for(var i=0;i<SimpleContextMenu._menuItemHandlers.length;++i)
00271 {
00272 el = document.getElementById("SimpleContextMenu-menuItem-" + i);
00273 if(i == index)
00274 {
00275 el.style.backgroundColor = SimpleContextMenu._secondaryColor;
00276 el.style.color = SimpleContextMenu._primaryColor;
00277 }
00278 else
00279 {
00280 el.style.backgroundColor = SimpleContextMenu._primaryColor;
00281 el.style.color = SimpleContextMenu._secondaryColor;
00282 }
00283 }
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297