101 lines
2.4 KiB
PHP
101 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace FML\Script;
|
|
|
|
use FML\Controls\Control;
|
|
use FML\Script\Sections\Constants;
|
|
use FML\Script\Sections\Labels;
|
|
use FML\Types\Scriptable;
|
|
|
|
/**
|
|
* ScriptFeature class offering menu behaviors
|
|
*
|
|
* @author steeffeen
|
|
*/
|
|
class Menus implements Constants, Labels, ScriptFeature {
|
|
/**
|
|
* Constants
|
|
*/
|
|
const C_MENUIDS = 'C_FML_MenuIds';
|
|
|
|
/**
|
|
* Protected properties
|
|
*/
|
|
protected $menus = array();
|
|
|
|
/**
|
|
* Add menu behavior defined by the given relationships
|
|
*
|
|
* @param array $menuRelationships
|
|
* @return \FML\Script\Menus
|
|
*/
|
|
public function add(array $menuRelationships) {
|
|
$menus = array();
|
|
$subMenus = array();
|
|
foreach ($menuRelationships as $relationship) {
|
|
$menuItemControl = $relationship[0];
|
|
$subMenuControl = $relationship[1];
|
|
|
|
if (!($menuItemControl instanceof Scriptable)) {
|
|
trigger_error('No Scriptable instance given as menu item.', E_USER_ERROR);
|
|
}
|
|
if (!($subMenuControl instanceof Control)) {
|
|
trigger_error('No Control instance given as sub menu.', E_USER_ERROR);
|
|
}
|
|
|
|
$menuItemControl->assignId();
|
|
$menuItemControl->setScriptEvents(true);
|
|
$subMenuControl->assignId();
|
|
|
|
array_push($menus, array($menuItemControl->getId(), $subMenuControl->getId()));
|
|
array_push($subMenus, $subMenuControl->getId());
|
|
}
|
|
array_push($this->menus, array($menus, $subMenus));
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @see \FML\Script\Sections\Constants::getConstants()
|
|
*/
|
|
public function getConstants() {
|
|
$constant = '[';
|
|
$index = 0;
|
|
foreach ($this->menus as $menu) {
|
|
$constant .= '[';
|
|
foreach ($menu[0] as $menuRel) {
|
|
$constant .= '"' . $menuRel[0] . '" => ["' . $menuRel[1] . '"], ';
|
|
}
|
|
$constant .= '"__FML__Sub__Menus__" => [';
|
|
$subIndex = 0;
|
|
foreach ($menu[1] as $subMenu) {
|
|
$constant .= '"' . $subMenu . '"';
|
|
if ($subIndex < count($menu[1]) - 1) {
|
|
$constant .= ', ';
|
|
}
|
|
$subIndex++;
|
|
}
|
|
$constant .= ']]';
|
|
if ($index < count($this->menus) - 1) {
|
|
$constant .= ', ';
|
|
}
|
|
$index++;
|
|
}
|
|
$constant .= ']';
|
|
$constants = array();
|
|
$constants[self::C_MENUIDS] = $constant;
|
|
return $constants;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @see \FML\Script\Sections\Labels::getLabels()
|
|
*/
|
|
public function getLabels() {
|
|
$labels = array();
|
|
$labelMouseClick = file_get_contents(__DIR__ . '/Templates/MenuMouseClick.txt');
|
|
$labels[Labels::MOUSECLICK] = $labelMouseClick;
|
|
return $labels;
|
|
}
|
|
}
|