├── .github ├── CONTRIBUTING.md ├── FUNDING.yml ├── GIT-WORKFLOW.md ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── stale.yml ├── CHANGE.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── NumberControl.php ├── NumberControlAsset.php └── assets └── js ├── number.js └── number.min.js /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing to yii2-number 2 | =========================== 3 | Looking to contribute something to yii2-number? **Here's how you can help.** 4 | 5 | Using the issue tracker 6 | ----------------------- 7 | When [reporting bugs][reporting-bugs] or 8 | [requesting features][requesting-features], the 9 | [issue tracker on GitHub][issue-tracker] is the recommended channel to use. 10 | 11 | The issue tracker **is not** a place for support requests. Refer the 12 | [extension documentation and demos](http://demos.krajee.com/number) and/or refer to the 13 | [webtips Q & A forum](http://webtips.krajee.com/questions) which are the better places to get help. 14 | 15 | How to contribute via a pull request? 16 | ------------------------------------- 17 | Refer this [git workflow for contributors](.github/GIT-WORKFLOW.md). 18 | 19 | Reporting bugs with yii2-number 20 | ------------------------------- 21 | We really appreciate clear bug reports that _consistently_ show an issue 22 | within _yii2-number_. 23 | 24 | The ideal bug report follows these guidelines: 25 | 26 | 1. **Use the [GitHub issue search][issue-search]** — Check if the issue 27 | has already been reported. 28 | 2. **Check if the issue has been fixed** — Try to reproduce the problem 29 | using the code in the `master` branch. 30 | 3. **Isolate the problem** — Try to share a demo or a test case that 31 | consistently reproduces the problem. 32 | 33 | Please try to be as detailed as possible in your bug report, especially if an 34 | isolated test case cannot be made. Some useful questions to include the answer 35 | to are: 36 | 37 | - What steps can be used to reproduce the issue? 38 | - What is the bug and what is the expected outcome? 39 | - What browser(s) and Operating System have you tested with? 40 | - Does the bug happen consistently across all tested browsers? 41 | - What version of jQuery are you using? And what version of yii2-number? 42 | - Are you using yii2-number with other plugins? 43 | 44 | All of these questions will help others fix and identify any potential bugs. 45 | 46 | Requesting features in yii2-number 47 | ---------------------------------- 48 | Before starting work on a major feature for yii2-number, **read the 49 | [documentation](http://demos.krajee.com/number) first** or you may risk spending a considerable amount of 50 | time on something which the project developers are not interested in bringing into the project. 51 | 52 | Licensing 53 | --------- 54 | 55 | It should also be made clear that **all code contributed to yii2-number** must be 56 | licensable under the [BSD-3 license][licensing]. Code that cannot be released 57 | under this license **cannot be accepted** into the project. 58 | 59 | [issue-search]: https://github.com/kartik-v/yii2-number/search?q=&type=Issues 60 | [issue-tracker]: https://github.com/kartik-v/yii2-number/issues 61 | [licensing]: https://github.com/kartik-v/yii2-number/blob/master/LICENSE.md 62 | [reporting-bugs]: #reporting-bugs-with-yii2-number 63 | [requesting-features]: #requesting-features-in-yii2-number -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | # github: [kartik-v] 4 | open_collective: kartik-v 5 | -------------------------------------------------------------------------------- /.github/GIT-WORKFLOW.md: -------------------------------------------------------------------------------- 1 | Git workflow for yii2-number contributors 2 | ========================================= 3 | 4 | So you want to contribute to yii2-number? Great! But to increase the chances of your changes being accepted quickly, please 5 | follow the following steps. If you are new to Git and GitHub, you might want to first check out [GitHub help](http://help.github.com/), [try Git](https://try.github.com) 6 | or learn something about [Git internal data model](http://nfarina.com/post/9868516270/git-is-simpler). 7 | 8 | Setup the development environment 9 | --------------------------------- 10 | 11 | Assuming you already have a yii2 development environment, carry out the following steps to create a development environment for the repo. 12 | 13 | ### 1. [Fork](http://help.github.com/fork-a-repo/) the yii2-number repository on GitHub and clone your fork to your development environment 14 | 15 | ``` 16 | git clone git@github.com:YOUR-GITHUB-USERNAME/yii2-number.git 17 | ``` 18 | 19 | If you have trouble setting up Git with GitHub in Linux, or are getting errors like "Permission Denied (publickey)", 20 | then you must [setup your Git installation to work with GitHub](http://help.github.com/linux-set-up-git/) 21 | 22 | > Tip: if you're not fluent with Git, we recommend reading excellent free [Pro Git book](https://git-scm.com/book/en/v2). 23 | 24 | ### 2. Add the main yii2-number repository as an additional git remote called "upstream" 25 | 26 | Change to the directory where you cloned yii2-number, normally, "yii2-number". Then enter the following command: 27 | 28 | ``` 29 | git remote add upstream git://github.com/kartik-v/yii2-number.git 30 | ``` 31 | 32 | ### 3. Prepare the testing environment 33 | 34 | - You should have a working yii 2 development environment in which you have already installed `yii2-number` and includes latest and updated `yii2-number` fork from source. 35 | - Ensure you have the latest `dev-master` releases of all dependent extensions via your composer updates 36 | - Ensure you use the above cloned latest `yii2-number` code in your testing environment 37 | 38 | **Now you have a working playground for hacking on yii2-number.** 39 | 40 | Working on bugs and features 41 | ---------------------------- 42 | 43 | Having prepared your development environment as explained above you can now start working on the feature or bugfix. 44 | 45 | ### 1. Make sure there is an issue created for the thing you are working on if it requires significant effort to fix 46 | 47 | All new features and bug fixes should have an associated issue to provide a single point of reference for discussion 48 | and documentation. Take a few minutes to look through the existing issue list for one that matches the contribution you 49 | intend to make. If you find one already on the issue list, then please leave a comment on that issue indicating you 50 | intend to work on that item. If you do not find an existing issue matching what you intend to work on, please 51 | open a new issue or create a pull request directly if it is straightforward fix. This will allow the team to 52 | review your suggestion, and provide appropriate feedback along the way. 53 | 54 | > For small changes or documentation issues or straightforward fixes, you don't need to create an issue, a pull request is enough in this case. 55 | 56 | ### 2. Fetch the latest code from the main yii2-number branch 57 | 58 | ``` 59 | git fetch upstream 60 | ``` 61 | 62 | You should start at this point for every new contribution to make sure you are working on the latest code. 63 | 64 | ### 3. Create a new branch for your feature based on the current yii2-number master branch 65 | 66 | > That's very important since you will not be able to submit more than one pull request from your account if you'll 67 | use master. 68 | 69 | Each separate bug fix or change should go in its own branch. Branch names should be descriptive and start with 70 | the number of the issue that your code relates to. If you aren't fixing any particular issue, just skip number. 71 | For example: 72 | 73 | ``` 74 | git checkout upstream/master 75 | git checkout -b 999-name-of-your-branch-goes-here 76 | ``` 77 | 78 | ### 4. Do your magic, write your code 79 | 80 | Make sure you have first updated the testing environment as mentioned in [prepare-the-testing-environment][prepare-the-testing-environment]. 81 | 82 | Then make sure you have the updated code with your change and it works :). 83 | 84 | Unit tests are always welcome. Tested and well covered code greatly simplifies the task of checking your contributions. 85 | Failing unit tests as issue description are also accepted. 86 | 87 | ### 5. Update the CHANGE log 88 | 89 | Edit the `CHANGE.md` file to include your change, you should insert this at the top of the file under the 90 | first heading (the version that is currently under development), the line in the change log should look like one of the following: 91 | 92 | ``` 93 | Bug #999: a description of the bug fix (Your Name) 94 | Enh #999: a description of the enhancement (Your Name) 95 | ``` 96 | 97 | `#999` is the issue number that the `Bug` or `Enh` is referring to. 98 | The changelog should be grouped by type (`Bug`,`Enh`) and ordered by issue number. 99 | 100 | For very small fixes, e.g. typos and documentation changes, there is no need to update the `CHANGE.md`. 101 | 102 | ### 6. Commit your changes 103 | 104 | add the files/changes you want to commit to the [staging area](http://gitref.org/basic/#add) with 105 | 106 | ``` 107 | git add path/to/my/file.php 108 | ``` 109 | 110 | You can use the `-p` option to select the changes you want to have in your commit. 111 | 112 | Commit your changes with a descriptive commit message. Make sure to mention the ticket number with `#XXX` so GitHub will 113 | automatically link your commit with the ticket: 114 | 115 | ``` 116 | git commit -m "A brief description of this change which fixes #999 goes here" 117 | ``` 118 | 119 | ### 7. Pull the latest yii2-number code from upstream into your branch 120 | 121 | ``` 122 | git pull upstream master 123 | ``` 124 | 125 | This ensures you have the latest code in your branch before you open your pull request. If there are any merge conflicts, 126 | you should fix them now and commit the changes again. This ensures that it's easy for the yii2-number team to merge your changes 127 | with one click. 128 | 129 | ### 8. Having resolved any conflicts, push your code to GitHub 130 | 131 | ``` 132 | git push -u origin 999-name-of-your-branch-goes-here 133 | ``` 134 | 135 | The `-u` parameter ensures that your branch will now automatically push and pull from the GitHub branch. That means 136 | if you type `git push` the next time it will know where to push to. This is useful if you want to later add more commits 137 | to the pull request. 138 | 139 | ### 9. Open a [pull request](http://help.github.com/send-pull-requests/) against upstream. 140 | 141 | Go to your repository on GitHub and click "Pull Request", choose your branch on the right and enter some more details 142 | in the comment box. To link the pull request to the issue put anywhere in the pull comment `#999` where 999 is the 143 | issue number. 144 | 145 | > Note that each pull-request should fix a single change. For multiple, unrelated changes, please open multiple pull requests. 146 | 147 | ### 10. Someone will review your code 148 | 149 | Someone will review your code, and you might be asked to make some changes, if so go to step #6 (you don't need to open 150 | another pull request if your current one is still open). If your code is accepted it will be merged into the main branch 151 | and become part of the next yii2-number release. If not, don't be disheartened, different people need different features and yii2-number 152 | can't be everything to everyone, your code will still be available on GitHub as a reference for people who need it. 153 | 154 | ### 11. Cleaning it up 155 | 156 | After your code was either accepted or declined you can delete branches you've worked with from your local repository 157 | and `origin`. 158 | 159 | ``` 160 | git checkout master 161 | git branch -D 999-name-of-your-branch-goes-here 162 | git push origin --delete 999-name-of-your-branch-goes-here 163 | ``` 164 | 165 | ### Command overview (for advanced contributors) 166 | 167 | ``` 168 | git clone git@github.com:YOUR-GITHUB-USERNAME/yii2-number.git 169 | git remote add upstream git://github.com/kartik-v/yii2-number.git 170 | ``` 171 | 172 | ``` 173 | git fetch upstream 174 | git checkout upstream/master 175 | git checkout -b 999-name-of-your-branch-goes-here 176 | 177 | /* do your magic, update changelog if needed */ 178 | 179 | git add path/to/my/file.php 180 | git commit -m "A brief description of this change which fixes #999 goes here" 181 | git pull upstream master 182 | git push -u origin 999-name-of-your-branch-goes-here 183 | ``` 184 | 185 | [prepare-the-testing-environment]: #3-prepare-the-testing-environment -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Prerequisites 2 | 3 | - [ ] I have searched for similar issues in both open and closed tickets and cannot find a duplicate. 4 | - [ ] The issue still exists against the latest `master` branch of yii2-number. 5 | - [ ] This is not an usage question. I confirm having gone through and read the [documentation and demos](http://demos.krajee.com/number). 6 | - [ ] This is not a general programming / coding question. (Those should be directed to the [webtips Q & A forum](http://webtips.krajee.com/questions)). 7 | - [ ] I have attempted to find the simplest possible steps to reproduce the issue. 8 | - [ ] I have included a failing test as a pull request (Optional). 9 | 10 | ## Steps to reproduce the issue 11 | 12 | 1. 13 | 2. 14 | 3. 15 | 16 | ## Expected behavior and actual behavior 17 | 18 | When I follow those steps, I see... 19 | 20 | I was expecting... 21 | 22 | ## Environment 23 | 24 | #### Browsers 25 | 26 | - [ ] Google Chrome 27 | - [ ] Mozilla Firefox 28 | - [ ] Internet Explorer 29 | - [ ] Safari 30 | 31 | #### Operating System 32 | 33 | - [ ] Windows 34 | - [ ] Mac OS X 35 | - [ ] Linux 36 | - [ ] Mobile 37 | 38 | #### Libraries 39 | 40 | - jQuery version: 41 | - yii2-number version: 42 | 43 | ## Isolating the problem 44 | 45 | - [ ] This bug happens [on the docs and demos page](https://demos.krajee.com/number) 46 | - [ ] The bug happens consistently across all tested browsers 47 | - [ ] This bug happens when using yii2-number without other plugins. -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Scope 2 | This pull request includes a 3 | 4 | - [ ] Bug fix 5 | - [ ] New feature 6 | - [ ] Translation 7 | 8 | ## Changes 9 | The following changes were made (this change is also documented in the [change log](https://github.com/kartik-v/yii2-number/blob/master/CHANGE.md)): 10 | 11 | - 12 | - 13 | - 14 | 15 | ## Related Issues 16 | If this is related to an existing ticket, include a link to it as well. -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 60 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - bug 8 | - enhancement 9 | - pinned 10 | - security 11 | # Label to use when marking an issue as stale 12 | staleLabel: wontfix 13 | # Comment to post when marking an issue as stale. Set to `false` to disable 14 | markComment: > 15 | This issue has been automatically marked as stale because it has not had 16 | recent activity. It will be closed if no further activity occurs. Thank you 17 | for your contributions. 18 | # Comment to post when closing a stale issue. Set to `false` to disable 19 | closeComment: false -------------------------------------------------------------------------------- /CHANGE.md: -------------------------------------------------------------------------------- 1 | Change Log: `yii2-number` 2 | ========================= 3 | 4 | ## Version 1.0.6 5 | 6 | **Date:** 19-Jun-2019 7 | 8 | - (enh #13): Set default save input type to text for correct form reset. 9 | - Add sponsorship configuration. 10 | 11 | ## Version 1.0.5 12 | 13 | **Date:** 07-Dec-2018 14 | 15 | - (bug #10, #11)- Pressing the "enter" key does not change the input value. 16 | - Update README. 17 | 18 | ## Version 1.0.4 19 | 20 | **Date:** 09-Oct-2018 21 | 22 | - Update composer dependencies. 23 | - (enh #9): Detect mask change on ENTER key press. 24 | - (enh #8): New property `displayAttribute` to allow read/write of masked format data to model. 25 | 26 | ## Version 1.0.3 27 | 28 | **Date:** 26-Jul-2018 29 | 30 | - (bug #6, #7): Correct Yii namespace. 31 | - (enh #5): Default `groupSeparator` and `radixPoint` from locale. 32 | 33 | ## Version 1.0.2 34 | 35 | **Date:** 07-Jun-2018 36 | 37 | - Reorganize source code in `src` folder. 38 | - (bug #4): Correct source plugin incorrect unmasking for non standard `radixPoint`. 39 | 40 | ## Version 1.0.1 41 | 42 | **Date:** 01-May-2018 43 | 44 | - (bug #3): Fix `val.replace` error due to inputmask auto unmask. 45 | 46 | ## Version 1.0.0 47 | 48 | **Date:** 30-Jan-2018 49 | 50 | - Initial release. -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 - 2019, Kartik Visweswaran 2 | Krajee.com 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, 6 | are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, this 12 | list of conditions and the following disclaimer in the documentation and/or 13 | other materials provided with the distribution. 14 | 15 | * Neither the names of Kartik Visweswaran or Krajee nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Krajee Logo 4 | 5 |
6 | yii2-number 7 |
8 | 10 | Donate 11 | 12 |

13 | 14 | [![Latest Stable Version](https://poser.pugx.org/kartik-v/yii2-number/v/stable)](https://packagist.org/packages/kartik-v/yii2-number) 15 | [![Latest Unstable Version](https://poser.pugx.org/kartik-v/yii2-number/v/unstable)](https://packagist.org/packages/kartik-v/yii2-number) 16 | [![License](https://poser.pugx.org/kartik-v/yii2-number/license)](https://packagist.org/packages/kartik-v/yii2-number) 17 | [![Total Downloads](https://poser.pugx.org/kartik-v/yii2-number/downloads)](https://packagist.org/packages/kartik-v/yii2-number) 18 | [![Monthly Downloads](https://poser.pugx.org/kartik-v/yii2-number/d/monthly)](https://packagist.org/packages/kartik-v/yii2-number) 19 | [![Daily Downloads](https://poser.pugx.org/kartik-v/yii2-number/d/daily)](https://packagist.org/packages/kartik-v/yii2-number) 20 | 21 | > ### Note 22 | > This extension replaces the [yii2-money](https://github.com/kartik-v/yii2-money) extension since Jan 2018. The [yii2-money](https://github.com/kartik-v/yii2-money) extension will not be enhanced further or supported. 23 | 24 | A number control input for Yii2 Framework that uses the [jQuery input mask plugin](https://github.com/RobinHerbots/Inputmask) (available also via `yii/widgets/MaskedInputAsset`) to render number input masks. This extension is similar to the [DateControl extension](http://demos.krajee.com/datecontrol) for dates, and allows one to control the display and save formats for numbers. The extension thus allows one to setup a number format display mask, use currency prefixes if needed, and modify the decimals and thousand separators. It lastly allow the display fields to be auto calculated as numbers when stored into the database. 25 | 26 | Refer [detailed documentation and demos](http://demos.krajee.com/number). 27 | 28 | ## Release Updates 29 | Refer the [CHANGE LOG](https://github.com/kartik-v/yii2-number/blob/master/CHANGE.md) for updates and changes to various releases. 30 | 31 | ### Install 32 | 33 | Either run 34 | 35 | ``` 36 | $ php composer.phar require kartik-v/yii2-number "@dev" 37 | ``` 38 | 39 | or add 40 | 41 | ``` 42 | "kartik-v/yii2-number": "@dev" 43 | ``` 44 | 45 | to the ```require``` section of your `composer.json` file. 46 | 47 | ## Usage 48 | ```php 49 | use kartik\number\NumberControl; 50 | 51 | // Normal decimal 52 | echo NumberControl::widget([ 53 | 'name' => 'normal-decimal', 54 | 'value' => 43829.39, 55 | ]); 56 | 57 | // Integer only 58 | echo NumberControl::widget([ 59 | 'name' => 'integer-only', 60 | 'value' => 32892, 61 | 'maskedInputOptions' => ['digits' => 0], 62 | ]); 63 | 64 | // Currency style with prefix and suffix 65 | echo NumberControl::widget([ 66 | 'name' => 'currency-num', 67 | 'value' => 2018032.22, 68 | 'maskedInputOptions' => ['prefix' => '$ ', 'suffix' => ' c'], 69 | ]); 70 | 71 | // Usage with model 72 | $model->currency = 1298132.23; 73 | echo NumberControl::widget([ 74 | 'model' => $model, 75 | 'attribute' => 'currency', 76 | 'maskedInputOptions' => ['prefix' => '$ ', 'suffix' => ' c'], 77 | ]); 78 | ``` 79 | 80 | ## License 81 | 82 | **yii2-number** is released under the BSD-3-Clause License. See the bundled `LICENSE.md` for details. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kartik-v/yii2-number", 3 | "description": "Number control and format mask input for Yii2 Framework.", 4 | "keywords": [ 5 | "yii2", 6 | "extension", 7 | "widget", 8 | "number", 9 | "currency", 10 | "format", 11 | "mask", 12 | "number control" 13 | ], 14 | "homepage": "https://github.com/kartik-v/yii2-number", 15 | "type": "yii2-extension", 16 | "license": "BSD-3-Clause", 17 | "authors": [ 18 | { 19 | "name": "Kartik Visweswaran", 20 | "email": "kartikv2@gmail.com", 21 | "homepage": "http://www.krajee.com/" 22 | } 23 | ], 24 | "require": { 25 | "kartik-v/yii2-krajee-base": ">=2.0.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "kartik\\number\\": "src" 30 | } 31 | }, 32 | "extra": { 33 | "branch-alias": { 34 | "dev-master": "1.0.x-dev" 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/NumberControl.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2018 - 2019 7 | * @version 1.0.6 8 | */ 9 | 10 | namespace kartik\number; 11 | 12 | use Yii; 13 | use yii\helpers\ArrayHelper; 14 | use yii\helpers\Html; 15 | use kartik\base\InputWidget; 16 | 17 | /** 18 | * Number control widget 19 | * 20 | * @author Kartik Visweswaran 21 | * @since 1.0 22 | */ 23 | class NumberControl extends InputWidget 24 | { 25 | /** 26 | * @var array masked input plugin options 27 | */ 28 | public $maskedInputOptions = []; 29 | 30 | /** 31 | * @inheritdoc 32 | */ 33 | public $pluginName = 'numberControl'; 34 | 35 | /** 36 | * @var array the HTML attributes for the base model input that will be saved typically to database. The following 37 | * special options are recognized: 38 | * - `type`: _string_, HTML input type. Defaults to 'text'. Note that the `saveInputContainer` settings will control 39 | * the display of the saved input. 40 | * - `label`: _string_, any label to be placed before the input. 41 | */ 42 | public $options = []; 43 | 44 | /** 45 | * @var string the name of the model attribute to read/write the masked number format data and is applicable only 46 | * when using with a model. If not set a normal native HTML text control will be generated as the display input. 47 | */ 48 | public $displayAttribute; 49 | 50 | /** 51 | * @var array the HTML attributes for the displayed masked input 52 | */ 53 | public $displayOptions = []; 54 | 55 | /** 56 | * @var array the HTML attributes for the container in which the saved input will be rendered. This container 57 | * is set to hidden display by default via the `style` configuration. 58 | */ 59 | public $saveInputContainer = ['style' => 'display:none']; 60 | 61 | /** 62 | * @inheritdoc 63 | */ 64 | public function run() 65 | { 66 | $this->initWidget(); 67 | $this->registerAssets(); 68 | echo $this->getDisplayInput() . "\n" . $this->getSaveInput(); 69 | } 70 | 71 | /** 72 | * Registers the needed assets 73 | */ 74 | public function registerAssets() 75 | { 76 | $view = $this->getView(); 77 | NumberControlAsset::register($view); 78 | $id = $this->options['id']; 79 | $this->pluginOptions = [ 80 | 'displayId' => $this->displayOptions['id'], 81 | 'maskedInputOptions' => $this->maskedInputOptions, 82 | ]; 83 | $this->registerPlugin($this->pluginName, "jQuery('#{$id}')"); 84 | } 85 | 86 | /** 87 | * Initializes the widget 88 | */ 89 | protected function initWidget() 90 | { 91 | if (!isset($this->displayOptions['id'])) { 92 | $this->displayOptions['id'] = $this->options['id'] . '-disp'; 93 | } 94 | if (!isset($this->displayOptions['class'])) { 95 | $this->displayOptions['class'] = 'form-control'; 96 | } 97 | if (isset($this->disabled) && $this->disabled) { 98 | $this->displayOptions['disabled'] = true; 99 | } 100 | if (isset($this->readonly) && $this->readonly) { 101 | $this->displayOptions['readonly'] = true; 102 | } 103 | $formatter = Yii::$app->formatter; 104 | $defaultOptions = [ 105 | 'alias' => 'numeric', 106 | 'digits' => 2, 107 | 'groupSeparator' => isset($formatter->thousandSeparator) ? $formatter->thousandSeparator : ',', 108 | 'radixPoint' => isset($formatter->decimalSeparator) ? $formatter->decimalSeparator : '.', 109 | 'autoGroup' => true, 110 | 'autoUnmask' => false, 111 | ]; 112 | $this->maskedInputOptions = array_replace_recursive($defaultOptions, $this->maskedInputOptions); 113 | } 114 | 115 | /** 116 | * Generates the display input. 117 | * 118 | * @return string 119 | */ 120 | protected function getDisplayInput() 121 | { 122 | if (!empty($this->displayAttribute) && $this->hasModel()) { 123 | return Html::activeTextInput($this->model, $this->displayAttribute, $this->displayOptions); 124 | } 125 | $name = ArrayHelper::remove($this->displayOptions, 'name', $this->displayOptions['id']); 126 | return Html::textInput($name, $this->value, $this->displayOptions); 127 | } 128 | 129 | /** 130 | * Generates the save input. 131 | * 132 | * @return string 133 | */ 134 | protected function getSaveInput() 135 | { 136 | $type = ArrayHelper::remove($this->options, 'type', 'text'); 137 | if (!isset($this->options['tabindex'])) { 138 | $this->options['tabindex'] = 10000; 139 | } 140 | $label = ArrayHelper::remove($this->options, 'label', '') ; 141 | if ($this->hasModel()) { 142 | $out = Html::activeInput($type, $this->model, $this->attribute, $this->options); 143 | } else { 144 | $out = Html::input($type, $this->name, $this->value, $this->options); 145 | } 146 | $out = $label . $out; 147 | return Html::tag('div', $out, $this->saveInputContainer); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/NumberControlAsset.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2018 - 2019 7 | * @version 1.0.6 8 | */ 9 | 10 | namespace kartik\number; 11 | 12 | use kartik\base\AssetBundle; 13 | 14 | /** 15 | * Asset bundle for the [[NumberControl]] widget. 16 | * 17 | * @author Kartik Visweswaran 18 | * @since 1.0 19 | */ 20 | class NumberControlAsset extends AssetBundle 21 | { 22 | /** 23 | * @inheritdoc 24 | */ 25 | public function init() 26 | { 27 | $this->depends = array_merge($this->depends, ['yii\widgets\MaskedInputAsset']); 28 | $this->setSourcePath(__DIR__ . '/assets'); 29 | $this->setupAssets('js', ['js/number']); 30 | parent::init(); 31 | } 32 | } -------------------------------------------------------------------------------- /src/assets/js/number.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * @package yii2-number 3 | * v1.0.6 4 | * 5 | * Krajee number control jQuery plugin 6 | * 7 | * http://demos.krajee.com/number 8 | * 9 | * Author: Kartik Visweswaran 10 | * Copyright: 2018 - 2019, Kartik Visweswaran, Krajee.com 11 | */ 12 | (function (factory) { 13 | "use strict"; 14 | //noinspection JSUnresolvedVariable 15 | if (typeof define === 'function' && define.amd) { // jshint ignore:line 16 | // AMD. Register as an anonymous module. 17 | define(['jquery'], factory); // jshint ignore:line 18 | } else { // noinspection JSUnresolvedVariable 19 | if (typeof module === 'object' && module.exports) { // jshint ignore:line 20 | // Node/CommonJS 21 | // noinspection JSUnresolvedVariable 22 | module.exports = factory(require('jquery')); // jshint ignore:line 23 | } else { 24 | // Browser globals 25 | factory(window.jQuery); 26 | } 27 | } 28 | }(function ($) { 29 | "use strict"; 30 | 31 | var NumberControl = function (element, options) { 32 | var self = this; 33 | self.$elSave = $(element); 34 | self.$elDisp = $('#' + options.displayId); 35 | self.options = options; 36 | self.init(); 37 | }; 38 | 39 | NumberControl.prototype = { 40 | constructor: NumberControl, 41 | init: function () { 42 | var self = this, $elDisp = self.$elDisp, $elSave = self.$elSave, opts = self.options.maskedInputOptions, 43 | NS = '.numberControl', events = ['change', 'blur', 'keypress', 'keydown'].join(NS + ' ') + NS, 44 | originalValue = $elDisp.inputmask('unmaskedvalue'), radixPre = opts.radixPoint || '.'; 45 | if (radixPre !== '.') { 46 | originalValue = (originalValue + '').replace('.', radixPre); 47 | } 48 | $elDisp.val(originalValue); 49 | $elDisp.off(NS).on(events, function (e) { 50 | var event = e.type, key = e.keyCode || e.which, enterKeyPressed = key && parseInt(key) === 13; 51 | if (event === 'keypress' && !enterKeyPressed) { 52 | return; 53 | } 54 | if (event !== 'keydown' || enterKeyPressed) { 55 | var num = $elDisp.inputmask('unmaskedvalue'), radix = opts.radixPoint || '.'; 56 | if (radix !== '.') { 57 | num = (num + '').replace(radix, '.'); 58 | } 59 | $elSave.val(num).trigger('change'); 60 | } 61 | }).inputmask(opts); 62 | }, 63 | destroy: function () { 64 | var self = this, $elDisp = self.$elDisp, $elSave = self.$elSave; 65 | $elDisp.off('.numberControl').removeData('inputmask'); 66 | $elSave.removeData('numberControl'); 67 | } 68 | }; 69 | 70 | $.fn.numberControl = function (option) { 71 | var args = Array.apply(null, arguments), retvals = []; 72 | args.shift(); 73 | this.each(function () { 74 | var self = $(this), opts, data = self.data('numberControl'), options = typeof option === 'object' && option; 75 | if (!data) { 76 | opts = $.extend(true, {}, $.fn.numberControl.defaults, options, self.data()); 77 | data = new NumberControl(this, opts); 78 | self.data('numberControl', data); 79 | } 80 | 81 | if (typeof option === 'string') { 82 | retvals.push(data[option].apply(data, args)); 83 | } 84 | }); 85 | switch (retvals.length) { 86 | case 0: 87 | return this; 88 | case 1: 89 | return retvals[0]; 90 | default: 91 | return retvals; 92 | } 93 | }; 94 | 95 | $.fn.numberControl.defaults = { 96 | displayId: '', 97 | maskedInputOptions: {} 98 | }; 99 | })); -------------------------------------------------------------------------------- /src/assets/js/number.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * @package yii2-number 3 | * v1.0.6 4 | * 5 | * Krajee number control jQuery plugin 6 | * 7 | * http://demos.krajee.com/number 8 | * 9 | * Author: Kartik Visweswaran 10 | * Copyright: 2018 - 2019, Kartik Visweswaran, Krajee.com 11 | */ 12 | !function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof module&&module.exports?module.exports=e(require("jquery")):e(window.jQuery)}(function(e){"use strict";var t=function(t,n){var o=this;o.$elSave=e(t),o.$elDisp=e("#"+n.displayId),o.options=n,o.init()};t.prototype={constructor:t,init:function(){var e=this,t=e.$elDisp,n=e.$elSave,o=e.options.maskedInputOptions,r=".numberControl",a=["change","blur","keypress","keydown"].join(r+" ")+r,i=t.inputmask("unmaskedvalue"),u=o.radixPoint||".";"."!==u&&(i=(i+"").replace(".",u)),t.val(i),t.off(r).on(a,function(e){var r=e.type,a=e.keyCode||e.which,i=a&&13===parseInt(a);if(("keypress"!==r||i)&&("keydown"!==r||i)){var u=t.inputmask("unmaskedvalue"),s=o.radixPoint||".";"."!==s&&(u=(u+"").replace(s,".")),n.val(u).trigger("change")}}).inputmask(o)},destroy:function(){var e=this,t=e.$elDisp,n=e.$elSave;t.off(".numberControl").removeData("inputmask"),n.removeData("numberControl")}},e.fn.numberControl=function(n){var o=Array.apply(null,arguments),r=[];switch(o.shift(),this.each(function(){var a,i=e(this),u=i.data("numberControl"),s="object"==typeof n&&n;u||(a=e.extend(!0,{},e.fn.numberControl.defaults,s,i.data()),u=new t(this,a),i.data("numberControl",u)),"string"==typeof n&&r.push(u[n].apply(u,o))}),r.length){case 0:return this;case 1:return r[0];default:return r}},e.fn.numberControl.defaults={displayId:"",maskedInputOptions:{}}}); --------------------------------------------------------------------------------