1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30: 31: 32:
33: abstract class AbstractAjaxBehaviour extends AbstractBehaviour implements BehaviourListener
34: {
35: private $callDecorator;
36:
37: public function __construct()
38: {
39: PiconApplication::get()->addComponentRenderHeadListener(new JQueryRenderHeadListener());
40: }
41:
42: public function bind(Component &$component)
43: {
44: parent::bind($component);
45: $component->setOutputMarkupId(true);
46: }
47:
48: public function renderHead(Component &$component, HeaderContainer $headerContainer, HeaderResponse $headerResponse)
49: {
50: parent::renderHead($component, $headerContainer, $headerResponse);
51: $headerResponse->renderJavaScriptResourceReference(new ResourceReference('ajax.js', self::getIdentifier()));
52: }
53:
54: public function isStateless()
55: {
56: return false;
57: }
58:
59: protected function generateCallbackScript()
60: {
61: $decorator = $this->getAjaxCallDecorator();
62:
63: $callScript = $this->callScript();
64: $successScript = $this->successScript();
65: $failScript = $this->failScript();
66:
67: if($decorator!=null && $decorator instanceof AjaxCallDecorator)
68: {
69: $callScript = $decorator->decorateScript($callScript);
70: $successScript = $decorator->decorateSuccessScript($successScript);
71: $failScript = $decorator->decorateFailScript($failScript);
72: }
73:
74: $successScript = sprintf('function() {%s}', $successScript);
75: $failScript = sprintf('function() {%s}', $failScript);
76:
77: $url = $this->getComponent()->urlForListener($this).'&ajax=ajax';
78: $script = $this->generateCallScript($url);
79: $script = sprintf('%s'.$script.', %s, %s);', $callScript, $successScript, $failScript);
80:
81: return $script;
82: }
83:
84: 85: 86: 87: 88:
89: protected function generateCallScript($url)
90: {
91: return sprintf('piconAjaxGet(\'%s\'', $url);
92: }
93:
94: public function onEvent()
95: {
96: $target = new AjaxRequestTarget();
97: $this->getComponent()->getRequestCycle()->addTarget($target);
98: $this->onEventCallback($target);
99: }
100:
101:
102:
103: public abstract function onEventCallback(AjaxRequestTarget $target);
104:
105: protected function getAjaxCallDecorator()
106: {
107: return $this->callDecorator;
108: }
109:
110: protected function successScript()
111: {
112: return "";
113: }
114:
115: protected function failScript()
116: {
117: return "";
118: }
119:
120: protected function callScript()
121: {
122: return "";
123: }
124:
125: public function setAjaxCallDecorator(AjaxCallDecorator &$decorator)
126: {
127: $this->callDecorator = $decorator;
128: }
129: }
130:
131: ?>
132: