| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2018-2023 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* Right-click (or long touch) context menu |
| 26 |
*/ |
| 27 |
class ContextMenu { |
| 28 |
|
| 29 |
private $graph; |
| 30 |
private $js; |
| 31 |
|
| 32 |
private $function_added = false; |
| 33 |
private $callback; |
| 34 |
private $use_structure = false; |
| 35 |
private $namespace = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor sets up options and root menu |
| 39 |
*/ |
| 40 |
public function __construct(&$graph, &$javascript) |
| 41 |
{ |
| 42 |
if(!$graph->getOption('show_context_menu')) |
| 43 |
return; |
| 44 |
$this->graph =& $graph; |
| 45 |
$this->js =& $javascript; |
| 46 |
|
| 47 |
$this->callback = $graph->getOption('context_callback'); |
| 48 |
$structure = $graph->getOption('structure'); |
| 49 |
if(is_array($structure) && isset($structure['context_menu'])) |
| 50 |
$this->use_structure = true; |
| 51 |
if($graph->getOption('namespace')) |
| 52 |
$this->namespace = 'svg:'; |
| 53 |
|
| 54 |
$global = $graph->getOption('context_global'); |
| 55 |
if($global !== false) { |
| 56 |
if($global === null) |
| 57 |
$global = [ [SVGGraph::VERSION, null] ]; |
| 58 |
|
| 59 |
$entries = ''; |
| 60 |
foreach($global as $entry) { |
| 61 |
$attr = ['name' => $entry[0], 'link' => $entry[1]]; |
| 62 |
$entries .= $graph->element('svggraph:menuitem', $attr); |
| 63 |
} |
| 64 |
$menu = $graph->element('svggraph:menu', null, null, $entries); |
| 65 |
$xml = $graph->element('svggraph:data', |
| 66 |
['xmlns:svggraph' => 'http://www.goat1000.com/svggraph'], null, $menu); |
| 67 |
$graph->defs->add($xml); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Adds the javascript function |
| 73 |
*/ |
| 74 |
public function addFunction() |
| 75 |
{ |
| 76 |
$this->js->addFuncs('getE', 'finditem', 'newel', 'newtext', |
| 77 |
'svgNode', 'setattr', 'getData', 'svgCursorCoords'); |
| 78 |
$this->js->addInitFunction('contextMenuInit'); |
| 79 |
|
| 80 |
$opts = ['link_target', 'link_underline', 'stroke_width', 'round', 'font', |
| 81 |
'font_weight', 'document_menu', 'spacing', 'min_width', |
| 82 |
'shadow_opacity', 'mouseleave']; |
| 83 |
$colours = ['colour', 'link_colour', 'link_hover_colour', 'back_colour']; |
| 84 |
$vars = []; |
| 85 |
foreach($opts as $opt) |
| 86 |
$vars[$opt] = $this->graph->getOption('context_' . $opt); |
| 87 |
$vars['font_size'] = Number::units($this->graph->getOption('context_font_size')); |
| 88 |
foreach($colours as $opt) |
| 89 |
$vars[$opt] = new Colour($this->graph, $this->graph->getOption('context_' . $opt)); |
| 90 |
|
| 91 |
$svg_text = new Text($this->graph, $vars['font']); |
| 92 |
list(, $text_height) = $svg_text->measure('Test', $vars['font_size']); |
| 93 |
$text_baseline = $svg_text->baseline($vars['font_size']); |
| 94 |
|
| 95 |
$vars['pad_x'] = $this->graph->getOption('context_padding_x', 'context_padding'); |
| 96 |
$vars['pad_y'] = $this->graph->getOption('context_padding_y', 'context_padding'); |
| 97 |
$vars['text_start'] = $vars['pad_y'] + $text_baseline; |
| 98 |
$vars['rect_start'] = $vars['pad_y'] - $vars['spacing'] / 2; |
| 99 |
$vars['spacing'] += $text_height; |
| 100 |
|
| 101 |
$vars['round_part'] = $vars['mouseleave'] = $vars['underline_part'] = ''; |
| 102 |
if($vars['link_underline']) |
| 103 |
$vars['underline_part'] = ", 'text-decoration': 'underline'"; |
| 104 |
if($vars['round']) { |
| 105 |
$rnum = new Number($vars['round']); |
| 106 |
$vars['round_part'] = ', rx:"' . $rnum . 'px", ry:"' . $rnum . 'px"'; |
| 107 |
} |
| 108 |
$cmoffs = 0; |
| 109 |
$half_stroke = $vars['stroke_width'] / 2; |
| 110 |
$vars['pad_x'] += $half_stroke; |
| 111 |
$vars['pad_y'] += $half_stroke; |
| 112 |
|
| 113 |
$vars['off_right'] = $vars['stroke_width']; |
| 114 |
$vars['off_bottom'] = $vars['stroke_width']; |
| 115 |
if(is_numeric($vars['shadow_opacity'])) { |
| 116 |
$cmoffs = 4; |
| 117 |
$vars['off_right'] += $cmoffs; |
| 118 |
$vars['off_bottom'] += $cmoffs; |
| 119 |
} |
| 120 |
$vars['cmoffs'] = $cmoffs; |
| 121 |
|
| 122 |
if($vars['document_menu']) { |
| 123 |
$this->js->insertFunction('rootContextMenu', |
| 124 |
"function rootContextMenu(){closeContextMenu();}\n"); |
| 125 |
} else { |
| 126 |
$this->js->insertTemplate('rootContextMenu'); |
| 127 |
} |
| 128 |
|
| 129 |
if((int)$vars['mouseleave'] > 0) { |
| 130 |
$mlnum = new Number($mouseleave); |
| 131 |
$vars['mouseleave'] = 'e[c].addEventListener("mouseleave",function(e) {' . |
| 132 |
'setTimeout(closeContextMenu,' . $mlnum . ');}, false);'; |
| 133 |
} |
| 134 |
|
| 135 |
$vars['namespace'] = $this->namespace; |
| 136 |
$this->js->insertTemplate('contextMenu', $vars); |
| 137 |
$this->function_added = true; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Adds context menu for item |
| 142 |
*/ |
| 143 |
public function setMenu(&$element, $dataset, &$item, $duplicate = false) |
| 144 |
{ |
| 145 |
$menu = null; |
| 146 |
if(is_callable($this->callback)) { |
| 147 |
$menu = call_user_func($this->callback, $dataset, $item->key, $item->value); |
| 148 |
} elseif($this->use_structure) { |
| 149 |
$menu = $item->context_menu; |
| 150 |
} |
| 151 |
|
| 152 |
if(is_array($menu)) { |
| 153 |
if(!isset($element['id'])) |
| 154 |
$element['id'] = $this->graph->newID(); |
| 155 |
$var = json_encode($menu); |
| 156 |
$this->js->insertVariable('menus', $element['id'], $var, false); |
| 157 |
if($duplicate) |
| 158 |
$this->js->addOverlay($element['id'], $this->graph->newID()); |
| 159 |
} else { |
| 160 |
// add a placeholder to make sure the variable exists |
| 161 |
$ignore_id = $this->graph->newID(); |
| 162 |
$this->js->insertVariable('menus', $ignore_id, "''", false); |
| 163 |
} |
| 164 |
|
| 165 |
// set up menus after duplication |
| 166 |
if(!$this->function_added) |
| 167 |
$this->addFunction(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|