| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- // version: 2014-06-26
- /**
- * o--------------------------------------------------------------------------------o
- * | This file is part of the RGraph package. RGraph is Free Software, licensed |
- * | under the MIT license - so it's free to use for all purposes. If you want to |
- * | donate to help keep the project going then you can do so here: |
- * | |
- * | http://www.rgraph.net/donate |
- * o--------------------------------------------------------------------------------o
- */
- RGraph = window.RGraph || {isRGraph: true};
- // Module pattern
- (function (win, doc, undefined)
- {
- var RG = RGraph,
- ua = navigator.userAgent,
- ma = Math;
- /**
- * This gunction shows a context menu containing the parameters
- * provided to it
- *
- * @param object canvas The canvas object
- * @param array menuitems The context menu menuitems
- * @param object e The event object
- */
- RG.contextmenu =
- RG.Contextmenu = function (obj, menuitems, e)
- {
- var canvas = obj.canvas;
- e = RG.FixEventObject(e);
- /**
- * Fire the custom RGraph event onbeforecontextmenu
- */
- RG.FireCustomEvent(obj, 'onbeforecontextmenu');
- /**
- * Hide any existing menu
- */
- if (RG.Registry.Get('chart.contextmenu')) {
- RG.HideContext();
- }
-
- // Hide any zoomed canvas
- RG.HideZoomedCanvas();
- /**
- * Hide the palette if necessary
- */
- RG.HidePalette();
-
- /**
- * This is here to ensure annotating is OFF
- */
- obj.Set('chart.mousedown', false);
- var x = e.pageX;
- var y = e.pageY;
- var div = document.createElement('div');
- var bg = document.createElement('div');
- div.className = 'RGraph_contextmenu';
- div.__canvas__ = canvas; /* Store a reference to the canvas on the contextmenu object */
- div.style.position = 'absolute';
- div.style.left = 0;
- div.style.top = 0;
- div.style.border = '1px solid black';
- div.style.backgroundColor = 'white';
- div.style.boxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- div.style.MozBoxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- div.style.WebkitBoxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- div.style.filter = 'progid:DXImageTransform.Microsoft.Shadow(color=#aaaaaa,direction=135)';
- div.style.opacity = 0;
- bg.className = 'RGraph_contextmenu_background';
- bg.style.position = 'absolute';
- bg.style.backgroundColor = '#ccc';
- bg.style.borderRight = '1px solid #aaa';
- bg.style.top = 0;
- bg.style.left = 0;
- bg.style.width = '18px';
- bg.style.height = '100%';
- bg.style.opacity = 0;
- div = document.body.appendChild(div);
- bg = div.appendChild(bg);
- /**
- * Now add the context menu items
- */
- for (i=0; i<menuitems.length; ++i) {
-
- var menuitem = document.createElement('div');
-
- menuitem.__object__ = obj;
- menuitem.__canvas__ = canvas;
- menuitem.__contextmenu__ = div;
- menuitem.className = 'RGraph_contextmenu_item';
- if (menuitems[i]) {
- menuitem.style.padding = '2px 5px 2px 23px';
- menuitem.style.fontFamily = 'Arial';
- menuitem.style.fontSize = '10pt';
- menuitem.style.fontWeight = 'normal';
- menuitem.innerHTML = menuitems[i][0];
- if (RG.is_array(menuitems[i][1])) {
- menuitem.style.backgroundImage = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAQUlEQVQImY3NoQ2AMABE0ZewABMyGQ6mqWODzlAclBSFO8HZl8uf0FFxCHtwYkt4Y6ChYE44cGH9/fyae2p2LAleW9oVTQuVf6gAAAAASUVORK5CYII=)';
- menuitem.style.backgroundRepeat = 'no-repeat';
- menuitem.style.backgroundPosition = '97% center';
- }
- // Add the mouseover event
- if (menuitems[i][1]) {
- if (menuitem.addEventListener) {
- menuitem.addEventListener("mouseover", function (e) {RG.HideContextSubmenu(); e.target.style.backgroundColor = 'rgba(0,0,0,0.2)'; e.target.style.cursor = 'pointer';}, false);
- menuitem.addEventListener("mouseout", function (e) {e.target.style.backgroundColor = 'inherit'; e.target.style.cursor = 'default';}, false);
- } else {
- menuitem.attachEvent("onmouseover", function () {RG.HideContextSubmenu();event.srcElement.style.backgroundColor = '#eee';event.srcElement.style.cursor = 'pointer';}
- , false);
- menuitem.attachEvent("onmouseout", function () {event.srcElement.style.backgroundColor = 'inherit'; event.srcElement.style.cursor = 'default';}, false);
- }
- } else {
- if (menuitem.addEventListener) {
- menuitem.addEventListener("mouseover", function (e) {e.target.style.cursor = 'default';}, false);
- menuitem.addEventListener("mouseout", function (e) {e.target.style.cursor = 'default';}, false);
- } else {
- menuitem.attachEvent("onmouseover", function () {event.srcElement.style.cursor = 'default'}, false);
- menuitem.attachEvent("onmouseout", function () {event.srcElement.style.cursor = 'default';}, false);
- }
- }
- } else {
- menuitem.style.borderBottom = '1px solid #ddd';
- menuitem.style.marginLeft = '25px';
- }
- div.appendChild(menuitem);
- /**
- * Install the event handler that calls the menuitem
- */
- if (menuitems[i] && menuitems[i][1] && typeof(menuitems[i][1]) == 'function') {
-
- menuitem.addEventListener('click', menuitems[i][1], false);
-
- // Submenu
- } else if (menuitems[i] && menuitems[i][1] && RG.is_array(menuitems[i][1])) {
- (function ()
- {
- var tmp = menuitems[i][1]; // This is here because of "references vs primitives" and how they're passed around in Javascript
-
- // TODO This may need attention
- menuitem.addEventListener('mouseover', function (e) {RG.Contextmenu_submenu(obj, tmp, e.target);}, false);
- })();
- }
- }
- /**
- * Now all the menu items have been added, set the shadow width
- * Shadow now handled by CSS3
- */
- div.style.width = (div.offsetWidth + 10) + 'px';
- div.style.height = (div.offsetHeight - 2) + 'px';
- // Show the menu to the left or the right (normal) of the cursor?
- if (x + div.offsetWidth > document.body.offsetWidth) {
- x -= div.offsetWidth;
- }
-
- // Reposition the menu (now we have the real offsetWidth)
- div.style.left = x + 'px';
- div.style.top = y + 'px';
- /**
- * Do a little fade in effect
- */
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu')) obj.style.opacity = 0.2", 50);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu')) obj.style.opacity = 0.4", 100);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu')) obj.style.opacity = 0.6", 150);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu')) obj.style.opacity = 0.8", 200);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu')) obj.style.opacity = 1", 250);
- // The fade in effect on the left gray bar
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu.bg')) obj.style.opacity = 0.2", 50);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu.bg')) obj.style.opacity = 0.4", 100);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu.bg')) obj.style.opacity = 0.6", 150);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu.bg')) obj.style.opacity = 0.8", 200);
- setTimeout("if (obj = RGraph.Registry.Get('chart.contextmenu.bg')) obj.style.opacity = 1", 250);
- // Store the context menu in the registry
- RG.Registry.Set('chart.contextmenu', div);
- RG.Registry.Set('chart.contextmenu.bg', bg);
- RG.Registry.Get('chart.contextmenu').oncontextmenu = function () {return false;};
- RG.Registry.Get('chart.contextmenu.bg').oncontextmenu = function () {return false;};
- /**
- * Install the event handlers that hide the context menu
- */
- canvas.addEventListener('click', function () {RG.HideContext();}, false);
- window.addEventListener('click', function ()
- {
- RG.HideContext();
- }, false);
- window.addEventListener('resize', function ()
- {
- RG.HideContext();
- }, false);
-
- /**
- * Add the __shape__ object to the context menu
- */
-
- /**
- * Set the shape coords from the .getShape() method
- */
- if (typeof(obj.getShape) == 'function') {
- RG.Registry.Get('chart.contextmenu').__shape__ = obj.getShape(e);
- }
- e.stopPropagation();
- /**
- * Fire the (RGraph) oncontextmenu event
- */
- RG.FireCustomEvent(obj, 'oncontextmenu');
- return false;
- };
- /**
- * Hides the context menu if it's currently visible
- */
- RG.hideContext =
- RG.HideContext = function ()
- {
- var cm = RG.Registry.Get('chart.contextmenu');
- var cmbg = RG.Registry.Get('chart.contextmenu.bg');
-
- //Hide any submenu currently being displayed
- RG.HideContextSubmenu();
- if (cm) {
- cm.parentNode.removeChild(cm);
- cmbg.parentNode.removeChild(cmbg);
- cm.style.visibility = 'hidden';
- cm.style.display = 'none';
- RG.Registry.Set('chart.contextmenu', null);
-
- cmbg.style.visibility = 'hidden';
- cmbg.style.display = 'none';
- RG.Registry.Set('chart.contextmenu.bg', null);
- }
- };
- /**
- * Hides the context menus SUBMENU if it's currently visible
- */
- RG.hideContextSubmenu =
- RG.HideContextSubmenu = function ()
- {
- var sub = RG.Registry.Get('chart.contextmenu.submenu');
- if (sub) {
- sub.style.visibility = 'none';
- sub.style.display = 'none';
- RG.Registry.Set('chart.contextmenu.submenu', null);
- }
- };
- /**
- * Shows the context menu after making a few checks - not opera (doesn't support oncontextmenu,
- * not safari (tempermentality), not chrome (hmmm)
- */
- RG.showContext =
- RG.ShowContext = function (obj)
- {
- RG.HidePalette();
- if (obj.Get('chart.contextmenu') && obj.Get('chart.contextmenu').length) {
- var isOpera = navigator.userAgent.indexOf('Opera') >= 0;
- var isSafari = navigator.userAgent.indexOf('Safari') >= 0;
- var isChrome = navigator.userAgent.indexOf('Chrome') >= 0;
- var isMacFirefox = navigator.userAgent.indexOf('Firefox') > 0 && navigator.userAgent.indexOf('Mac') > 0;
- var isIE9 = navigator.userAgent.indexOf('MSIE 9') >= 0;
- if (((!isOpera && !isSafari) || isChrome) && !isMacFirefox) {
- obj.canvas.oncontextmenu = function (e)
- {
- e = RG.FixEventObject(e);
- if (e.ctrlKey) return true;
- RG.Contextmenu(obj, obj.Get('chart.contextmenu'), e);
- return false;
- }
- // Accomodate Opera and Safari - use double click event
- } else {
- obj.canvas.addEventListener('dblclick', function (e)
- {
- if (e.ctrlKey) return true;
- if (!RG.Registry.Get('chart.contextmenu')) {
- RG.Contextmenu(obj, obj.Get('chart.contextmenu'), e);
- }
- }, false);
- }
- }
- };
- /**
- * This draws a submenu should it be necessary
- *
- * @param object obj The graph object
- * @param object menu The context menu
- */
- RG.contextmenu_submenu =
- RG.Contextmenu_submenu = function (obj, menuitems, parentMenuItem)
- {
- RG.HideContextSubmenu();
- var canvas = obj.canvas;
- var context = obj.context;
- var menu = parentMenuItem.parentNode;
- var subMenu = document.createElement('DIV');
- subMenu.style.position = 'absolute';
- subMenu.style.width = '100px';
- subMenu.style.top = menu.offsetTop + parentMenuItem.offsetTop + 'px';
- subMenu.style.left = (menu.offsetLeft + menu.offsetWidth - (RG.ISOLD ? 9 : 0)) + 'px';
- subMenu.style.backgroundColor = 'white';
- subMenu.style.border = '1px solid black';
- subMenu.className = 'RGraph_contextmenu';
- subMenu.__contextmenu__ = menu;
- subMenu.style.boxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- subMenu.style.MozBoxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- subMenu.style.WebkitBoxShadow = '3px 3px 3px rgba(96,96,96,0.5)';
- subMenu.style.filter = 'progid:DXImageTransform.Microsoft.Shadow(color=#aaaaaa,direction=135)';
- document.body.appendChild(subMenu);
-
- for (var i=0; i<menuitems.length; ++i) {
-
- var menuitem = document.createElement('DIV');
-
- menuitem.__canvas__ = canvas;
- menuitem.__contextmenu__ = menu;
- menuitem.className = 'RGraph_contextmenu_item';
-
- if (menuitems[i]) {
- menuitem.style.padding = '2px 5px 2px 23px';
- menuitem.style.fontFamily = 'Arial';
- menuitem.style.fontSize = '10pt';
- menuitem.style.fontWeight = 'normal';
- menuitem.innerHTML = menuitems[i][0];
-
- if (menuitems[i][1]) {
- if (menuitem.addEventListener) {
- menuitem.addEventListener("mouseover", function (e) {e.target.style.backgroundColor = 'rgba(0,0,0,0.2)'; e.target.style.cursor = 'pointer';}, false);
- menuitem.addEventListener("mouseout", function (e) {e.target.style.backgroundColor = 'inherit'; e.target.style.cursor = 'default';}, false);
- } else {
- menuitem.attachEvent("onmouseover", function () {event.srcElement.style.backgroundColor = 'rgba(0,0,0,0.2)'; event.srcElement.style.cursor = 'pointer'}, false);
- menuitem.attachEvent("onmouseout", function () {event.srcElement.style.backgroundColor = 'inherit'; event.srcElement.style.cursor = 'default';}, false);
- }
- } else {
- if (menuitem.addEventListener) {
- menuitem.addEventListener("mouseover", function (e) {e.target.style.cursor = 'default';}, false);
- menuitem.addEventListener("mouseout", function (e) {e.target.style.cursor = 'default';}, false);
- } else {
- menuitem.attachEvent("onmouseover", function () {event.srcElement.style.cursor = 'default'}, false);
- menuitem.attachEvent("onmouseout", function () {event.srcElement.style.cursor = 'default';}, false);
- }
- }
- } else {
- menuitem.style.borderBottom = '1px solid #ddd';
- menuitem.style.marginLeft = '25px';
- }
-
- subMenu.appendChild(menuitem);
-
- if (menuitems[i] && menuitems[i][1]) {
- if (document.all) {
- menuitem.attachEvent('onclick', menuitems[i][1]);
- } else {
- menuitem.addEventListener('click', menuitems[i][1], false);
- }
- }
- }
- var bg = document.createElement('DIV');
- bg.className = 'RGraph_contextmenu_background';
- bg.style.position = 'absolute';
- bg.style.backgroundColor = '#ccc';
- bg.style.borderRight = '1px solid #aaa';
- bg.style.top = 0;
- bg.style.left = 0;
- bg.style.width = '18px';
- bg.style.height = '100%';
- bg = subMenu.appendChild(bg);
- RG.Registry.Set('chart.contextmenu.submenu', subMenu);
- };
- /**
- * A function designed to be used in conjunction with thed context menu
- * to allow people to get image versions of canvases.
- *
- * @param canvas Optionally you can pass in the canvas, which will be used
- */
- RG.showPNG = function ()
- {
- if (RG.ISIE8) {
- alert('[RGRAPH PNG] Sorry, showing a PNG is not supported on MSIE8.');
- return;
- }
- if (arguments[0] && arguments[0].id) {
- var canvas = arguments[0];
- var event = arguments[1];
-
- } else if (RG.Registry.Get('chart.contextmenu')) {
- var canvas = RG.Registry.Get('chart.contextmenu').__canvas__;
-
- } else {
- alert('[RGRAPH SHOWPNG] Could not find canvas!');
- }
- var obj = canvas.__object__;
- /**
- * Create the gray background DIV to cover the page
- */
- var bg = document.createElement('DIV');
- bg.id = '__rgraph_image_bg__';
- bg.style.position = 'fixed';
- bg.style.top = '-10px';
- bg.style.left = '-10px';
- bg.style.width = '5000px';
- bg.style.height = '5000px';
- bg.style.backgroundColor = 'rgb(204,204,204)';
- bg.style.opacity = 0;
- document.body.appendChild(bg);
-
-
- /**
- * Create the div that the graph sits in
- */
- var div = document.createElement('DIV');
- div.style.backgroundColor = 'white';
- div.style.opacity = 0;
- div.style.border = '1px solid black';
- div.style.position = 'fixed';
- div.style.top = '20%';
- div.style.width = canvas.width + 'px';
- div.style.height = canvas.height + 35 + 'px';
- div.style.left = (document.body.clientWidth / 2) - (canvas.width / 2) + 'px';
- div.style.padding = '5px';
- div.style.borderRadius = '10px';
- div.style.MozBorderRadius = '10px';
- div.style.WebkitBorderRadius = '10px';
- div.style.boxShadow = '0 0 15px rgba(96,96,96,0.5)';
- div.style.MozBoxShadow = '0 0 15px rgba(96,96,96,0.5)';
- div.style.WebkitBoxShadow = 'rgba(96,96,96,0.5) 0 0 15px';
- div.__canvas__ = canvas;
- div.__object__ = obj;
- div.id = '__rgraph_image_div__';
- document.body.appendChild(div);
-
- /**
- * Add the HTML text inputs
- */
- div.innerHTML += '<div style="position: absolute; margin-left: 10px; top: ' + canvas.height + 'px; width: ' + (canvas.width - 50) + 'px; height: 25px"><span style="font-size: 12pt;display: inline; display: inline-block; width: 65px; text-align: right">URL:</span><textarea style="float: right; overflow: hidden; height: 20px; width: ' + (canvas.width - obj.gutterLeft - obj.gutterRight - 80) + 'px" onclick="this.select()" readonly="readonly" id="__rgraph_dataurl__">' + canvas.toDataURL() + '</textarea></div>';
- div.innerHTML += '<div style="position: absolute; top: ' + (canvas.height + 25) + 'px; left: ' + (obj.gutterLeft - 65 + (canvas.width / 2)) + 'px; width: ' + (canvas.width - obj.gutterRight) + 'px; font-size: 65%">A link using the URL: <a href="' + canvas.toDataURL() + '">View</a></div>'
-
-
- /**
- * Create the image rendition of the graph
- */
- var img = document.createElement('IMG');
- RG.Registry.Set('chart.png', img);
- img.__canvas__ = canvas;
- img.__object__ = obj;
- img.id = '__rgraph_image_img__';
- img.className = 'RGraph_png';
- img.src = canvas.toDataURL();
- div.appendChild(img);
-
- setTimeout(function () {document.getElementById("__rgraph_dataurl__").select();}, 50);
-
- window.addEventListener('resize', function (e){var img = RG.Registry.Get('chart.png');img.style.left = (document.body.clientWidth / 2) - (img.width / 2) + 'px';}, false);
-
- bg.onclick = function (e)
- {
- var div = document.getElementById("__rgraph_image_div__");
- var bg = document.getElementById("__rgraph_image_bg__");
- if (div) {
- div.style.opacity = 0;
- div.parentNode.removeChild(div);
- div.id = '';
- div.style.display = 'none';
- div = null;
- }
- if (bg) {
- bg.style.opacity = 0;
- bg.id = '';
- bg.style.display = 'none';
- bg = null;
- }
- }
-
- window.addEventListener('resize', function (e) {bg.onclick(e);}, false)
-
- /**
- * This sets the image BG and the DIV as global variables, circumventing repeated calls to document.getElementById()
- */
- RG.showpng_image_bg = bg;
- RG.showpng_image_div = div;
- setTimeout('RGraph.showpng_image_div.style.opacity = 0.2', 50);
- setTimeout('RGraph.showpng_image_div.style.opacity = 0.4', 100);
- setTimeout('RGraph.showpng_image_div.style.opacity = 0.6', 150);
- setTimeout('RGraph.showpng_image_div.style.opacity = 0.8', 200);
- setTimeout('RGraph.showpng_image_div.style.opacity = 1', 250);
- setTimeout('RGraph.showpng_image_bg.style.opacity = 0.1', 50);
- setTimeout('RGraph.showpng_image_bg.style.opacity = 0.2', 100);
- setTimeout('RGraph.showpng_image_bg.style.opacity = 0.3', 150);
- setTimeout('RGraph.showpng_image_bg.style.opacity = 0.4', 200);
- setTimeout('RGraph.showpng_image_bg.style.opacity = 0.5', 250);
-
- img.onclick = function (e)
- {
- if (e.stopPropagation) e.stopPropagation();
- else event.cancelBubble = true;
- }
-
- if (event && event.stopPropagation) {
- event.stopPropagation();
- }
- };
- // End module pattern
- })(window, document);
|