Пример #1
0
 /**
  * Enter description here...
  *
  * @param \PhpQuery\PhpQueryObject $self
  * @param                          $arg1
  * @return null|\PhpQuery\PhpQueryObject
  */
 public static function script($self, $arg1)
 {
     $params = func_get_args();
     $params = array_slice($params, 2);
     $return = null;
     $config = self::$config;
     if (\PhpQuery\Plugin\UtilScripts::$scriptMethods[$arg1]) {
         PhpQuery::callbackRun(\PhpQuery\Plugin\UtilScripts::$scriptMethods[$arg1], array($self, $params, &$return, $config));
     } else {
         if ($arg1 != '__config' && file_exists(dirname(__FILE__) . "/Scripts/{$arg1}.php")) {
             PhpQuery::debug("Loading script '{$arg1}'");
             require dirname(__FILE__) . "/Scripts/{$arg1}.php";
         } else {
             PhpQuery::debug("Requested script '{$arg1}' doesn't exist");
         }
     }
     return $return ? $return : $self;
 }
Пример #2
0
 /**
  * Trigger a type of event on every matched element.
  *
  *
  * @TODO exclusive events (with !)
  * @TODO global events (test)
  * @TODO support more than event in $type (space-separated)
  * @param       $document
  * @param       $type
  * @param array $data
  * @param null  $node
  */
 public static function trigger($document, $type, $data = array(), $node = null)
 {
     // trigger: function(type, data, elem, donative, extra) {
     $documentID = PhpQuery::getDocumentID($document);
     $namespace = null;
     if (strpos($type, '.') !== false) {
         list($name, $namespace) = explode('.', $type);
     } else {
         $name = $type;
     }
     if (!$node) {
         if (self::issetGlobal($documentID, $type)) {
             $pq = PhpQuery::getDocument($documentID);
             // TODO check add($pq->document)
             $pq->find('*')->add($pq->document)->trigger($type, $data);
         }
     } else {
         if (isset($data[0]) && $data[0] instanceof DOMEvent) {
             $event = $data[0];
             $event->relatedTarget = $event->target;
             $event->target = $node;
             $data = array_slice($data, 1);
         } else {
             $event = new DOMEvent(array('type' => $type, 'target' => $node, 'timeStamp' => time()));
         }
         $i = 0;
         while ($node) {
             // TODO whois
             PhpQuery::debug("Triggering " . ($i ? "bubbled " : '') . "event '{$type}' on " . "node \n");
             //.PhpQueryObject::whois($node)."\n");
             $event->currentTarget = $node;
             $eventNode = self::getNode($documentID, $node);
             if (isset($eventNode->eventHandlers)) {
                 foreach ($eventNode->eventHandlers as $eventType => $handlers) {
                     $eventNamespace = null;
                     if (strpos($type, '.') !== false) {
                         list($eventName, $eventNamespace) = explode('.', $eventType);
                     } else {
                         $eventName = $eventType;
                     }
                     if ($name != $eventName) {
                         continue;
                     }
                     if ($namespace && $eventNamespace && $namespace != $eventNamespace) {
                         continue;
                     }
                     foreach ($handlers as $handler) {
                         PhpQuery::debug("Calling event handler\n");
                         $event->data = $handler['data'] ? $handler['data'] : null;
                         $params = array_merge(array($event), $data);
                         $return = PhpQuery::callbackRun($handler['callback'], $params);
                         if ($return === false) {
                             $event->bubbles = false;
                         }
                     }
                 }
             }
             // to bubble or not to bubble...
             if (!$event->bubbles) {
                 break;
             }
             $node = $node->parentNode;
             $i++;
         }
     }
 }
Пример #3
0
 /**
  * Run callback on actual object.
  *
  * @param      $callback
  * @param null $param1
  * @param null $param2
  * @param null $param3
  * @return PhpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery
  */
 public function callback($callback, $param1 = null, $param2 = null, $param3 = null)
 {
     $params = func_get_args();
     $params[0] = $this;
     PhpQuery::callbackRun($callback, $params);
     return $this;
 }
Пример #4
0
 /**
  * Enter description here...
  *
  * @TODO trigger submit for form after form's  submit button has a click event
  * @param      $e
  * @param null $callback
  */
 public static function handleSubmit($e, $callback = null)
 {
     $node = PhpQuery::pq($e->target);
     if (!$node->is('form') || !$node->is('[action]')) {
         return;
     }
     // TODO document.location
     $xhr = isset($node->document->xhr) ? $node->document->xhr : null;
     $submit = pq($e->relatedTarget)->is(':submit') ? $e->relatedTarget : $node->find('*:submit:first')->get(0);
     $data = array();
     foreach ($node->serializeArray($submit) as $r) {
         // XXXt.c maybe $node->not(':submit')->add($sumit) would be better ?
         //		foreach($node->serializeArray($submit) as $r)
         $data[$r['name']] = $r['value'];
     }
     $options = array('type' => $node->attr('method') ? $node->attr('method') : 'GET', 'url' => resolve_url($e->data[0], $node->attr('action')), 'data' => $data, 'referer' => $node->document->location);
     if ($node->attr('enctype')) {
         $options['contentType'] = $node->attr('enctype');
     }
     $xhr = PhpQuery::ajax($options, $xhr);
     if ((!$callback || !$callback instanceof Callback) && $e->data[1]) {
         $callback = $e->data[1];
     }
     if ($xhr->getLastResponse()->isSuccessful() && $callback) {
         PhpQuery::callbackRun($callback, array(self::browserReceive($xhr)));
     }
 }