| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\FormElements; |
||
| 4 | |||
| 5 | use SilverStripe\Forms\TextField; |
||
| 6 | use SilverStripe\View\Requirements; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Format input using cleave.js |
||
| 10 | * |
||
| 11 | * @link https://nosir.github.io/cleave.js/ |
||
| 12 | * @link https://github.com/lekoala/cleave-es6 |
||
| 13 | */ |
||
| 14 | class CleaveField extends TextField implements MaskableField |
||
| 15 | { |
||
| 16 | const TYPE_TIME = "time"; |
||
| 17 | const TYPE_DATE = "date"; |
||
| 18 | const TYPE_DATETIME = "datetime"; |
||
| 19 | const TYPE_NUMERAL = "numeral"; |
||
| 20 | |||
| 21 | use BaseElement; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @config |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private static $default_config = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 28 | "swapHiddenInput" => true, |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @config |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | private static $enable_requirements = true; |
||
|
0 ignored issues
–
show
|
|||
| 36 | |||
| 37 | public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) |
||
| 38 | { |
||
| 39 | parent::__construct($name, $title, $value, $maxLength, $form); |
||
| 40 | $this->mergeDefaultConfig(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function Type() |
||
| 44 | { |
||
| 45 | return 'cleave'; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function extraClass() |
||
| 49 | { |
||
| 50 | return 'text ' . parent::extraClass(); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function Field($properties = array()) |
||
| 54 | { |
||
| 55 | return $this->wrapInElement('cleave-input', $properties); |
||
| 56 | } |
||
| 57 | |||
| 58 | public static function requirements() |
||
| 59 | { |
||
| 60 | Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/cleave-input.min.js"); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the value of cleaveType |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getCleaveType() |
||
| 68 | { |
||
| 69 | return $this->getElementAttribute('type'); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set the value of inputType |
||
| 74 | * |
||
| 75 | * @param string $cleaveType date,time,datetime,numeral |
||
| 76 | * @return $this |
||
| 77 | */ |
||
| 78 | public function setCleaveType($cleaveType) |
||
| 79 | { |
||
| 80 | return $this->setElementAttribute('type', $cleaveType); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function getDigits() |
||
| 84 | { |
||
| 85 | return $this->getConfig('numeralDecimalScale'); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function setDigits($v) |
||
| 89 | { |
||
| 90 | return $this->setConfig('numeralDecimalScale', $v); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getRadixPoint() |
||
| 94 | { |
||
| 95 | return $this->getConfig('numeralDecimalMark'); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setRadixPoint($v) |
||
| 99 | { |
||
| 100 | return $this->setConfig('numeralDecimalMark', $v); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getGroupSeparator() |
||
| 104 | { |
||
| 105 | return $this->getConfig('delimiter'); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function setGroupSeparator($value) |
||
| 109 | { |
||
| 110 | return $this->setConfig('delimiter', $value); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getEnforceDigitsOnBlur() |
||
| 114 | { |
||
| 115 | return $this->getConfig('numeralDecimalPadding'); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function setEnforceDigitsOnBlur($value) |
||
| 119 | { |
||
| 120 | return $this->setConfig('numeralDecimalPadding ', $value); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getPrefix() |
||
| 124 | { |
||
| 125 | return $this->getConfig('prefix'); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function setPrefix($value) |
||
| 129 | { |
||
| 130 | return $this->setConfig('prefix', $value); |
||
| 131 | } |
||
| 132 | } |
||
| 133 |