UNPKG

15.2 kBMarkdownView Raw
1jQuery fontIconPicker `v3.1.1` [![Build Status](https://travis-ci.org/fontIconPicker/fontIconPicker.svg?branch=master)](https://travis-ci.org/fontIconPicker/fontIconPicker) [![Maintainability](https://api.codeclimate.com/v1/badges/701cba42caf2db204315/maintainability)](https://codeclimate.com/github/fontIconPicker/fontIconPicker/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/701cba42caf2db204315/test_coverage)](https://codeclimate.com/github/fontIconPicker/fontIconPicker/test_coverage) [![Cypress.io tests](https://img.shields.io/badge/cypress.io-tests-green.svg?style=flat-square)](https://dashboard.cypress.io/#/projects/agegn4/runs)
2==============================
3
4jQuery fontIconPicker is a small (`4.05KB` gzipped) jQuery plugin which allows you to include an elegant icon picker with categories, search and pagination inside your administration forms. The list of icons can be loaded manually using a `SELECT` field, an `Array` or `Object` of icons or directly from a Fontello `config.json` or IcoMoon `selection.json` file. Go to the [official plugin page](https://fonticonpicker.github.io) for examples and documentation.
5
6**fontIconPicker _v3.x_** supports jQuery `1.12.4` through `3.3.0`.
7
8> Originally developed at [micc83/fontIconPicker](https://github.com/micc83/fontIconPicker) and now moved into separate [organization](https://github.com/fontIconPicker) for better collaboration.
9
10![fontIconPickers](github-img.png)
11
12## Installation
13
14### With NPM/YARN
15
16fontIconPicker has been released over NPM. So you can either use NPM to install or download a [release](https://github.com/fontIconPicker/fontIconPicker/releases).
17
18```bash
19npm install jquery@1.12.4 @fonticonpicker/fonticonpicker --save
20```
21
22Now use with webpack or browserify.
23
24```js
25const jQuery = require( 'jquery' );
26const fip = require( '@fonticonpicker/fonticonpicker' )( jQuery );
27
28jQuery( '.selector' ).fontIconPicker( {
29 // Options
30} );
31```
32
33### Global `script` / CDN support
34
35You can put fontIconPicker dependencies directly in your `html` with `<script>`
36and `<link>` tags. Go to [fontIconPicker Releases](https://github.com/fontIconPicker/fontIconPicker/releases) and download the
37`fontIconPicker.zip` file from latest Assets. It will contain all scripts and
38styles built for production use.
39
40You can also use CDN from [unpkg.com](https://unpkg.com/#/).
41
42```html
43<!-- styles -->
44<!-- base | always include -->
45<link rel="stylesheet" type="text/css" href="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/css/base/jquery.fonticonpicker.min.css">
46
47<!-- default grey-theme -->
48<link rel="stylesheet" type="text/css" href="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/css/themes/grey-theme/jquery.fonticonpicker.grey.min.css">
49
50<!-- optional themes | no need to include default theme -->
51<link rel="stylesheet" type="text/css" href="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/css/themes/bootstrap-theme/jquery.fonticonpicker.bootstrap.min.css">
52<link rel="stylesheet" type="text/css" href="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/css/themes/dark-grey-theme/jquery.fonticonpicker.darkgrey.min.css">
53<link rel="stylesheet" type="text/css" href="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/css/themes/inverted-theme/jquery.fonticonpicker.inverted.min.css">
54
55<!-- scripts -->
56<script type="text/javascript" src="https://unpkg.com/@fonticonpicker/fonticonpicker/dist/js/jquery.fonticonpicker.min.js"></script>
57```
58
59### Using ES6 Module
60
61If you wish to use `ES6` module, then you have to initialize manually.
62
63```js
64import jQuery from 'jquery';
65import initFontIconPicker from '@fonticonpicker/fonticonpicker';
66
67// Initiate the jQuery plugin
68initFontIconPicker( jQuery );
69
70jQuery( '.selector' ).fontIconPicker( {
71 // Options
72} );
73```
74
75Right now, the only feasible way to properly initiate `fontIconPicker` through
76rollupjs is to pass jQuery directly in the `initFontIconPicker` function. If
77you know a better way, feel free to suggest.
78
79## How it works
80Just include a copy of jQuery, the fontIconPickers script, the fontIconPickers theme and your Font Icons. Now you can trigger it on a `SELECT` or `INPUT[type="text"]` element.
81
82### Include the JavaScript
83 ```html
84 <!-- jQuery -->
85<script type="text/javascript" src="jquery-1.12.4.min.js"></script>
86
87<!-- fontIconPicker JS -->
88<script type="text/javascript" src="js/jquery.fonticonpicker.min.js"></script>
89```
90
91### Include the CSS
92```html
93<!-- fontIconPicker core CSS -->
94<link rel="stylesheet" type="text/css" href="css/base/jquery.fonticonpicker.min.css" />
95
96<!-- required default theme -->
97<link rel="stylesheet" type="text/css" href="css/themes/grey-theme/jquery.fonticonpicker.grey.min.css" />
98
99<!-- optional themes -->
100<link rel="stylesheet" type="text/css" href="css/themes/dark-grey-theme/jquery.fonticonpicker.darkgrey.min.css" />
101<link rel="stylesheet" type="text/css" href="css/themes/bootstrap-theme/jquery.fonticonpicker.bootstrap.min.css" />
102<link rel="stylesheet" type="text/css" href="css/themes/inverted-theme/jquery.fonticonpicker.inverted.min.css" />
103```
104
105### Include Font Icons
106```html
107<!-- Font -->
108<link rel="stylesheet" type="text/css" href="fontello-7275ca86/css/fontello.css" />
109<link rel="stylesheet" type="text/css" href="icomoon/icomoon.css" />
110```
111
112### Initialize with jQuery
113Finally call the fontIconPicker on a `SELECT` or `INPUT[type="text"]` element.
114
115```html
116<!-- SELECT element -->
117<select id="myselect" name="myselect" class="myselect">
118 <option value="">No icon</option>
119 <option>icon-user</option>
120 <option>icon-search</option>
121 <option>icon-right-dir</option>
122 <option>icon-star</option>
123 <option>icon-cancel</option>
124 <option>icon-help-circled</option>
125 <option>icon-info-circled</option>
126 <option>icon-eye</option>
127 <option>icon-tag</option>
128 <option>icon-bookmark</option>
129 <option>icon-heart</option>
130 <option>icon-thumbs-down-alt</option>
131 <option>icon-upload-cloud</option>
132 <option>icon-phone-squared</option>
133 <option>icon-cog</option>
134 <option>icon-wrench</option>
135 <option>icon-volume-down</option>
136 <option>icon-down-dir</option>
137 <option>icon-up-dir</option>
138 <option>icon-left-dir</option>
139 <option>icon-thumbs-up-alt</option>
140</select>
141<!-- JavaScript -->
142<script type="text/javascript">
143 // Make sure to fire only when the DOM is ready
144 jQuery(document).ready(function($) {
145 $('#myselect').fontIconPicker(); // Load with default options
146 });
147</script>
148```
149
150```html
151<!-- INPUT element -->
152<input type="text" name="mytext" id="mytext" />
153<script type="text/javascript">
154 jQuery(document).ready(function($) {
155 $('#mytext').fontIconPicker({
156 source: ['icon-heart', 'icon-search', 'icon-user', 'icon-tag', 'icon-help'],
157 emptyIcon: false,
158 hasSearch: false
159 });
160 });
161</script>
162```
163
164## Plugin Options
165Here's fontIconPicker options:
166```js
167var $picker = $('.picker').fontIconPicker({
168 theme : 'fip-grey', // The CSS theme to use with this fontIconPicker. You can set different themes on multiple elements on the same page
169 source : false, // Icons source (array|false|object)
170 emptyIcon : true, // Empty icon should be shown?
171 emptyIconValue : '', // The value of the empty icon, change if you select has something else, say "none"
172 autoClose : true, // Whether or not to close the FIP automatically when clicked outside
173 iconsPerPage : 20, // Number of icons per page
174 hasSearch : true, // Is search enabled?
175 searchSource : false, // Give a manual search values. If using attributes then for proper search feature we also need to pass icon names under the same order of source
176 appendTo : 'self', // Where to append the selector popup. You can pass string selectors or jQuery objects
177 useAttribute : false, // Whether to use attribute selector for printing icons
178 attributeName : 'data-icon', // HTML Attribute name
179 convertToHex : true, // Whether or not to convert to hexadecimal for attribute value. If true then please pass decimal integer value to the source (or as value="" attribute of the select field)
180 allCategoryText : 'From all categories', // The text for the select all category option
181 unCategorizedText : 'Uncategorized', // The text for the select uncategorized option
182 iconGenerator : null, // Icon Generator function. Passes, item, flipBoxTitle and index
183 windowDebounceDelay: 150, // Debounce delay while fixing position on windowResize
184 searchPlaceholder : 'Search Icons' // Placeholder for the search input
185});
186```
187
188## Plugin APIs
189fontIconPicker provides three public APIs to manipulating the icon picker.
190
191### setIcon( `String` newIcon )
192
193Use this method to set an icon programmatically.
194
195```js
196$picker.setIcon( 'fa fa-arrow-down' );
197```
198
199### setIcons( `Array|Object` newIcons, `Array|Object` iconSearch )
200 Use this method to dynamically change icons on the fly. `newIcons` and `iconSearch` (optional) have same datatypes as `source` and `searchSource` options.
201
202```js
203$picker.setIcons(['icon-one', 'icon-two']);
204$picker.setIcons(['icon-one', 'icon-two'], ['Icon one will be searched by this', 'Icon two will be searched by this']);
205```
206
207### setPage( `String|Number` pageNum )
208
209Use this method to programmatically set pagination in the fip popup. This
210persists between open and close state until user has changed pagination manually.
211
212```js
213$picker.setPage( 2 );
214$picker.setPage( 'first' );
215$picker.setPage( 'last' );
216```
217
218Method accepts numbers and only two special strings.
219
220* Number corresponds to the page number starting from 1.
221* `first` will set the pagination to first page.
222* `last` will set the pagination to last page.
223
224### destroyPicker()
225Use this to remove the icon picker and restore the original element.
226
227```js
228$picker.destroyPicker();
229```
230
231### refreshPicker( `Object|Boolean` newOptions )
232Refresh the icon picker from DOM or passed options. Useful when DOM has been changed or reinitializing after calling the destroy method or changing the options values.
233
234```js
235$picker.refreshPicker({
236 theme: 'fip-bootstrap',
237 hasSearch: false
238});
239```
240
241### repositionPicker()
242
243Reposition picker dropdown in the window. Use this if DOM has changed and dropdown
244is open. Or if your picker is inside a scrolling container.
245
246```js
247var picker = $('.myselect').fontIconPicker();
248$('.mycontainer').on( 'scroll', function() {
249 picker.repositionPicker();
250} );
251```
252
253Options and APIs are discussed in details with live examples at the
254[project page](https://fonticonpicker.github.io).
255
256### Important notes for local demo
257
258Only when loading demo locally: In firefox fontIconPicker icons won't be shown correctly because of CORS. For the same reason "Load icons from Fontello JSON config file" won't work on Chrome or IE 10. If you need to do some local testing you can disable strict_origin_policy at your risk.
259
260Use the bundled [**Browser**sync](https://browsersync.io/) server to run the demo instead. See [Setup](#setup) to learn how.
261
262## Browser Compatibility
263
264jQuery iconPicker has been successfully tested on: Firefox (edge), Safari (edge), Chrome (edge), IE8+ and Opera (edge).
265
266## jQuery Compatibility
267
268`jQuery` >= 1.12.4 has been set as `peerDependencies` in the `package.json`. We have
269tested with `1.x` and `3.x` branch. jQuery Migrate doesn't produce any error when
270using with `3.x`.
271
272## Development Environment
273
274If you want to contribute keep a few things in mind.
275
276* Any code shouldn't produce any error with ESLint and Stylelint. We check for errors
277during build and if it does produce any error, then it will be rejected.
278* All new JS code should have unit or integration test implemented with jest.
279
280### Setup
281
282First make sure Node >= v8 is installed. Then clone the repository
283
284```bash
285git clone git@github.com:fontIconPicker/fontIconPicker.git
286```
287
288and run
289
290```bash
291cd fontIconPicker
292npm install
293npm install -g npx
294```
295
296Which will install all dependencies. You do not need to install `jquery` as it
297is defined in `devDependencies`.
298
299Now edit the files in `src` as needed and while you do, run
300
301```bash
302npx gulp serve
303```
304
305This will serve the changes live in your browser. Access it through `http://localhost:3000`.
306
307### Coding Standards
308
309* All additional modules should go inside `src/js/modules` with unit tests in
310 `__tests__/unit`.
311* Write JS in [ES6](http://es6-features.org/#Constants). [This](https://es6.io) is
312 a great place to learn. It will be compiled with babel on the go.
313* Do not write CSS files directly. Write [SCSS](https://sass-lang.com/). It will
314 be compiled when `npx gulp serve` is running.
315* If you are writing a theme, include in the first box of `demo/index.html`.
316* If you are writing an option or API, include in a separate box of `demo/index.html`.
317* All new JS features should have unit/integration and/or e2e tests. We use [cypress](https://cypress.io) for e2e tests and [jest](https://facebook.github.io/jest/) for unit/integration tests.
318
319### Automated git hooks
320
321When you run `npm install` for the first time, [husky](https://github.com/typicode/husky) setups a few git hooks. It specifically
322does two things.
323
3241. Checks for proper [commit message](http://marionebl.github.io/commitlint/#/concepts-commit-conventions).
3252. Runs linting of `js` and `scss` files to avoid any error before push.
326
327### Continuous Integration (CI)
328
329Additionally we run test suits for each push on [travis ci](https://travis-ci.org/fontIconPicker/fontIconPicker).
330
331Make sure it passes by running
332
333```bash
334npm run test
335```
336
337locally on your machine. It lints all files and runs [`jest`](https://facebook.github.io/jest/) tests.
338
339### Running tests locally
340
341fontIconPicker has two sets of tests.
342
343#### Unit & Integration test with [jest](https://facebook.github.io/jest/)
344
345```bash
346npx jest
347```
348
349#### End2End test with [cypress](https://www.cypress.io/)
350
351```bash
352npx cypress open
353```
354
355Then run the tests from the app. Or
356
357```bash
358npx cypress run
359```
360
361To run the tests in the terminal from a headless browser. You do not need to
362worry about setting up test environment as it is taken care of automatically
363by [cypress](https://www.cypress.io/).
364
365### Available Gulp Commands
366
367* `default` - Builds all scripts/styles and put them in `dist` folder.
368* `build` - Same as default.
369* `lint` - Lint all CSS and JS files.
370 * `lint:script` - Lint all JS files.
371 * `lint:style` - Lint all SCSS files.
372* `fonts` - Copy font files to `dist` directory.
373* `serve` - Build the files, init browsersync and watch files to create dev env.
374
375Run these commands with [`npx`](https://www.npmjs.com/package/npx).
376
377## Credits
378
379jQuery fontIconPicker has been made by [Alessandro Benoit](http://codeb.it) & [swashata](https://github.com/swashata). You
380can use the [issue tracker](https://github.com/fontIconPicker/fontIconPicker/issues) for reporting bugs or feature requests.
381
382> Special thanks to miniMAC for the idea, Zeno Rocha for jQuery plugin boilerplate, Dave Gandy & KeyaMoon for the beautiful sets of icons and Elliot Condon for the amazing Advanced Custom Field WordPress plugin.