Exemplo n.º 1
0
 public function __construct($action = NULL)
 {
     if (!$action) {
         $action = WFAction::JSAction();
     }
     if (!$action instanceof WFAction) {
         throw new WFException("Must pass a WFAction to WFEvent constructor.");
     }
     $this->action = $action;
     $this->action->setEvent($this);
     $this->name = NULL;
     $this->widget = NULL;
 }
Exemplo n.º 2
0
 /**
  * All WFView objects support our powerful onEvent syntax. This makes it very easy to attach javascript operations to any DOM events of your choosing.
  *
  * You can specify these programmatically or in the YAML file, like:
  *
  * onEvent: <eventName> do <processingOption>:<processingInfo>
  *
  * You can choose from three processing options, "j" (execute javascript code), "s" (execute a server action via a form submit), or "a" (execute a server action via ajax).
  *
  * For "j" events, it looks like:
  *
  * onEvent: click do j:alert("Somthing was clicked")
  *
  * Your code is actually processed inside of an object function callback; "this" will be the WFAction, and it has a parameter "event". By default, the event is NOT stopped
  * when using "j" events, so if you don't want the default behavior to occur, be sure to use this.stopEvent(event) in your j:javascript.
  *
  * NOTE: the 's' and 'a' processing options also accept a '!' modifier which turns on runsIfInvalid mode. This is useful in many cases where you need to
  * run server-side processing of form data before you even expect the form to be valid (for instance chained WFSelect widgets).
  */
 public function setOnEvent($str)
 {
     // clean up script & gracefully handle empty scripts
     $str = trim($str);
     if (empty($str)) {
         return;
     }
     $this->originalOnEvent = $str;
     $matches = array();
     // statement syntax: <event> do <j|s|a>:[action]
     // repeat by adding "onEvent:" and another statement
     $pieces = preg_split('/\\W\\bonEvent\\b:\\W*/', $str);
     foreach ($pieces as $statement) {
         if (preg_match('/^\\W*([A-z]*)\\W*do\\W*([jsa])(!)?:?((#(page|module)#[^:]*):)?(.*)$/', $statement, $matches)) {
             list(, $eventName, $actionType, $runsIfInvalid, , $target, , $actionArgument) = $matches;
             if (empty($runsIfInvalid)) {
                 $runsIfInvalid = false;
             } else {
                 $runsIfInvalid = true;
             }
             if (empty($actionArgument)) {
                 $actionArgument = NULL;
             }
             // normalize to null
             if (empty($target)) {
                 $target = NULL;
             }
             // normalize to null
             if ($actionType === 'j') {
                 $action = WFAction::JSAction();
                 if ($actionArgument !== NULL) {
                     $action->setJsEventHandler("function(event) { " . $actionArgument . " }.bind(action)");
                 }
             } else {
                 if ($actionType === 's') {
                     $action = WFAction::ServerAction();
                     if ($actionArgument !== NULL) {
                         if ($target !== NULL) {
                             $action->setTarget($target);
                         }
                         $action->setAction($actionArgument);
                     }
                     if ($runsIfInvalid) {
                         $action->rpc()->setRunsIfInvalid(true);
                     }
                 } else {
                     if ($actionType === 'a') {
                         $action = WFAction::AjaxAction();
                         if ($actionArgument !== NULL) {
                             if ($target !== NULL) {
                                 $action->setTarget($target);
                             }
                             $action->setAction($actionArgument);
                         }
                         if ($runsIfInvalid) {
                             $action->rpc()->setRunsIfInvalid(true);
                         }
                     }
                 }
             }
             $this->setListener(WFEvent::factory($eventName, $action));
         } else {
             throw new WFException("Couldn't parse onEvent statement: " . $statement);
         }
     }
 }