| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form\Field;
- class SwitchField extends Field
- {
- public static $js = '@switchery';
- public static $css = '@switchery';
- public function primary()
- {
- return $this->color(Admin::color()->primary());
- }
- public function green()
- {
- return $this->color(Admin::color()->success());
- }
- public function custom()
- {
- return $this->color(Admin::color()->custom());
- }
- public function yellow()
- {
- return $this->color(Admin::color()->warning());
- }
- public function red()
- {
- return $this->color(Admin::color()->danger());
- }
- public function purple()
- {
- return $this->color(Admin::color()->purple());
- }
- public function blue()
- {
- return $this->color(Admin::color()->blue());
- }
- /**
- * Set color of the switcher.
- *
- * @param string $color
- *
- * @return $this
- */
- public function color($color)
- {
- return $this->attribute('data-color', $color);
- }
- /**
- * @param string $color
- *
- * @return $this
- */
- public function secondary($color)
- {
- return $this->attribute('data-secondary-color', $color);
- }
- /**
- * @return $this
- */
- public function small()
- {
- return $this->attribute('data-size', 'small');
- }
- /**
- * @return $this
- */
- public function large()
- {
- return $this->attribute('data-size', 'large');
- }
- /**
- * @param mixed $value
- *
- * @return int
- */
- protected function prepareToSave($value)
- {
- return $value ? 1 : 0;
- }
- public function render()
- {
- if (empty($this->attributes['data-size'])) {
- $this->small();
- }
- if (empty($this->attributes['data-color'])) {
- $this->primary();
- }
- $this->attribute('name', $this->getElementName());
- $this->attribute('value', 1);
- $this->attribute('type', 'checkbox');
- $this->attribute('data-plugin', $this->getFormElementId().'switchery');
- Admin::script(
- <<<JS
- function swty(){\$('[data-plugin="{$this->getFormElementId()}switchery"]').each(function(){new Switchery($(this)[0],$(this).data())})} swty();
- JS
- );
- return parent::render();
- }
- }
|