Esempio n. 1
0
 /**
  * @param string $methodName
  * @return void
  */
 public static function register($methodName = 'addDynamic')
 {
     if (self::$registered) {
         Nette\Forms\Container::extensionMethod(self::$registered, function () {
             throw new Nette\MemberAccessException();
         });
     }
     Nette\Forms\Container::extensionMethod($methodName, function (Nette\Forms\Container $_this, $name, $factory, $createDefault = 0, $forceDefault = FALSE) {
         $control = new Container($factory, $createDefault, $forceDefault);
         $control->currentGroup = $_this->currentGroup;
         return $_this[$name] = $control;
     });
     if (self::$registered) {
         return;
     }
     SubmitButton::extensionMethod('addRemoveOnClick', function (SubmitButton $_this, $callback = NULL) {
         $_this->setValidationScope(FALSE);
         $_this->onClick[] = function (SubmitButton $button) use($callback) {
             $replicator = $button->lookup(__NAMESPACE__ . '\\Container');
             /** @var Container $replicator */
             if (is_callable($callback)) {
                 Callback::invoke($callback, $replicator, $button->parent);
             }
             if ($form = $button->getForm(FALSE)) {
                 $form->onSuccess = array();
             }
             $replicator->remove($button->parent);
         };
         return $_this;
     });
     SubmitButton::extensionMethod('addCreateOnClick', function (SubmitButton $_this, $allowEmpty = FALSE, $callback = NULL) {
         $_this->onClick[] = function (SubmitButton $button) use($allowEmpty, $callback) {
             $replicator = $button->lookup(__NAMESPACE__ . '\\Container');
             /** @var Container $replicator */
             if (!is_bool($allowEmpty)) {
                 $callback = Callback::closure($allowEmpty);
                 $allowEmpty = FALSE;
             }
             if ($allowEmpty === TRUE || $replicator->isAllFilled() === TRUE) {
                 $newContainer = $replicator->createOne();
                 if (is_callable($callback)) {
                     Callback::invoke($callback, $replicator, $newContainer);
                 }
             }
             $button->getForm()->onSuccess = array();
         };
         return $_this;
     });
     self::$registered = $methodName;
 }