|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ariadne Component Library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Muze <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace arc; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This component implements an event system very similar to events in modern browsers. Events have |
|
16
|
|
|
* a seperate capture and listen phase. Events are fired and listened to on a 'path' - like a |
|
17
|
|
|
* filesystem path - instead of an object in the DOM. In the capture phase listeners are called in |
|
18
|
|
|
* order starting with listeners on the root path '/'. Then - if the event has not been cancelled - |
|
19
|
|
|
* in the listen phase listeners are called in the reverse order - with the root path being called |
|
20
|
|
|
* last. |
|
21
|
|
|
* If the context stack is available you can change the default path events are fired on and listened |
|
22
|
|
|
* to by changing the 'path' entry in the context stack. |
|
23
|
|
|
* |
|
24
|
|
|
* @requires \arc\path |
|
25
|
|
|
* @requires \arc\tree |
|
26
|
|
|
* @requires \arc\context |
|
27
|
|
|
*/ |
|
28
|
|
|
class events |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Factory method for the static stack. Returns the shared stack only. Use new \arc\events\Stack |
|
32
|
|
|
* or your own factory method to create a seperate Stack instance. |
|
33
|
|
|
*/ |
|
34
|
12 |
|
public static function getEventsTree() |
|
35
|
|
|
{ |
|
36
|
12 |
|
$context = \arc\context::$context; |
|
37
|
12 |
|
if (!$context->arcEvents) { |
|
38
|
2 |
|
$context->arcEvents = new events\EventsTree( \arc\tree::expand()->cd( $context->arcPath) ); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
12 |
|
return $context->arcEvents; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns an IncompleteListener for the given event, objectType and path. |
|
46
|
|
|
* |
|
47
|
|
|
* Usage: |
|
48
|
|
|
* \arc\events::listen( 'onsave' )->call( function ($event) { |
|
49
|
|
|
* $path = $event->data['arc.path']; |
|
50
|
|
|
* if ($path == '/foo/bar/') { |
|
51
|
|
|
* $event->preventDefault(); |
|
52
|
|
|
* return false; // cancel all other listeners |
|
53
|
|
|
* } |
|
54
|
|
|
* }); |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $eventName The name of the event to listen for. |
|
57
|
|
|
* @return IncompleteListener |
|
58
|
|
|
*/ |
|
59
|
10 |
|
public static function listen($eventName, $callback) |
|
60
|
|
|
{ |
|
61
|
10 |
|
return self::getEventsTree()->listen( $eventName, $callback ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns an IncompleteListener for the given event, objectType and path. The listener |
|
66
|
|
|
* will trigger in the capture phase - before any listeners in the listen phase. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $eventName The name of the event to listen for. |
|
69
|
|
|
* @return IncompleteListener |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public static function capture($eventName, $callback) |
|
72
|
|
|
{ |
|
73
|
4 |
|
return self::getEventsTree()->capture( $eventName, $callback ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Fires an event. If the event objects preventDefault() method has been called it |
|
78
|
|
|
* will return false, otherwise the - potentially changed - eventData will be returned. |
|
79
|
|
|
* |
|
80
|
|
|
* Usage: |
|
81
|
|
|
* $eventData = \arc\events::fire( 'onbeforesave', array( 'your' => 'data' ) ); |
|
82
|
|
|
* if ($eventData) { |
|
83
|
|
|
* // now save it |
|
84
|
|
|
* } |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $eventName The name of the event to fire. |
|
87
|
|
|
* @param mixed $eventData Optional. Data passed to each handler through the event object. |
|
88
|
|
|
* @return false or $eventData - which may have been modified |
|
89
|
|
|
*/ |
|
90
|
6 |
|
public static function fire( $eventName, $eventData = array() ) |
|
91
|
|
|
{ |
|
92
|
6 |
|
return self::getEventsTree()->fire( $eventName, $eventData ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns a new IncompleteListener with the given path. |
|
97
|
|
|
* @param string $path The path to change to, may be a relative path. |
|
98
|
|
|
* @return IncompleteListener |
|
99
|
|
|
*/ |
|
100
|
8 |
|
public static function cd($path) |
|
101
|
|
|
{ |
|
102
|
8 |
|
return self::getEventsTree()->cd( $path ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|