|
1
|
|
|
<?php namespace EvolutionCMS\Support; |
|
2
|
|
|
|
|
3
|
|
|
use EvolutionCMS\Interfaces\ContextMenuInterface; |
|
4
|
|
|
|
|
5
|
|
|
class ContextMenu implements ContextMenuInterface{ |
|
6
|
|
|
public $id; |
|
7
|
|
|
/** |
|
8
|
|
|
* @var string |
|
9
|
|
|
*/ |
|
10
|
|
|
public $html = ''; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var bool |
|
13
|
|
|
*/ |
|
14
|
|
|
public $visible = false; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
public $width = 120; |
|
19
|
|
|
|
|
20
|
|
|
public static $cnt; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($id = '', $width = 120, $visible = false) { |
|
23
|
|
|
self::$cnt++; |
|
24
|
|
|
$this->html = ""; |
|
25
|
|
|
$this->visible = $visible ? $visible : false; |
|
26
|
|
|
$this->width = is_numeric($width) ? (int)$width : 120; |
|
27
|
|
|
$this->id = $id ? $id : "cntxMnu" . self::$cnt; // set id |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function addItem($text, $action = "", $img = "", $disabled = 0) { |
|
31
|
|
|
global $base_url, $_style; |
|
|
|
|
|
|
32
|
|
|
if($disabled) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
if(!$img) { |
|
36
|
|
|
$img = $base_url . $_style['tx']; |
|
37
|
|
|
} |
|
38
|
|
View Code Duplication |
if(substr($action, 0, 3) == "js:") { |
|
39
|
|
|
$action = substr($action, 3); |
|
40
|
|
|
} else if(substr($action, 0, 3) == "hl:") { |
|
41
|
|
|
$action = "window.location.href='" . substr($action, 3) . "'"; |
|
42
|
|
|
} else { |
|
43
|
|
|
$action = "window.location.href='" . $action . "'"; |
|
44
|
|
|
} |
|
45
|
|
|
$action = " onmouseover=\"this.className='cntxMnuItemOver';\" onmouseout=\"this.className='cntxMnuItem';\" onclick=\"$action; hideCntxMenu('" . $this->id . "');\""; |
|
46
|
|
|
$this->html .= "<div class='" . ($disabled ? "cntxMnuItemDisabled" : "cntxMnuItem") . "' $action>"; |
|
47
|
|
|
if(substr($img, 0, 5) == 'fa fa') { |
|
48
|
|
|
$img = '<i class="' . $img . '"></i>'; |
|
49
|
|
|
} else if(substr($img, 0, 1) != '<') { |
|
50
|
|
|
$img = '<img src="' . $img . '" />'; |
|
51
|
|
|
} |
|
52
|
|
|
$this->html .= $img . ' ' . $text . '</div>'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function addSeparator() { |
|
56
|
|
|
$this->html .= " |
|
57
|
|
|
<div class='cntxMnuSeparator'></div> |
|
58
|
|
|
"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function render() { |
|
62
|
|
|
$ContextMenuScript = <<<BLOCK |
|
63
|
|
|
<script> |
|
64
|
|
|
function getCntxMenu(id) { |
|
65
|
|
|
return document.getElementById(id); |
|
66
|
|
|
} |
|
67
|
|
|
function hideCntxMenu(id){ |
|
68
|
|
|
var cm = getCntxMenu(id); |
|
69
|
|
|
cm.style.visibility = 'hidden'; |
|
70
|
|
|
} |
|
71
|
|
|
</script> |
|
72
|
|
|
BLOCK; |
|
73
|
|
|
|
|
74
|
|
|
$html = $ContextMenuScript . "<div id='" . $this->id . "' class='contextMenu' style='width:" . $this->width . "px; visibility:" . ($this->visible ? 'visible' : 'hidden') . "'>" . $this->html . "</div>"; |
|
75
|
|
|
$ContextMenuScript = ""; // reset css |
|
|
|
|
|
|
76
|
|
|
return $html; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getClientScriptObject() { |
|
80
|
|
|
return "getCntxMenu('" . $this->id . "')"; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state