bootstrap-iconpicker.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*!========================================================================
  2. * Bootstrap: bootstrap-iconpicker.js v1.7.0 by @recktoner
  3. * https://victor-valencia.github.com/bootstrap-iconpicker
  4. * ========================================================================
  5. * Copyright 2013-2015 Victor Valencia Rico.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. ;(function($){ "use strict";
  20. // ICONPICKER PUBLIC CLASS DEFINITION
  21. // ==============================
  22. var Iconpicker = function (element, options) {
  23. this.$element = $(element);
  24. this.options = $.extend({}, Iconpicker.DEFAULTS, this.$element.data());
  25. this.options = $.extend({}, this.options, options);
  26. };
  27. // ICONPICKER ICONSET_EMPTY
  28. // ==============================
  29. Iconpicker.ICONSET_EMPTY = {
  30. iconClass: '',
  31. iconClassFix: '',
  32. icons: []
  33. };
  34. // ICONPICKER ICONSET
  35. // ==============================
  36. Iconpicker.ICONSET = {
  37. _custom: null,
  38. elusiveicon: $.iconset_elusiveicon || Iconpicker.ICONSET_EMPTY,
  39. fontawesome: $.iconset_fontawesome || Iconpicker.ICONSET_EMPTY,
  40. ionicon: $.iconset_ionicon || Iconpicker.ICONSET_EMPTY,
  41. glyphicon: $.iconset_glyphicon || Iconpicker.ICONSET_EMPTY,
  42. mapicon: $.iconset_mapicon || Iconpicker.ICONSET_EMPTY,
  43. materialdesign: $.iconset_materialdesign || Iconpicker.ICONSET_EMPTY,
  44. octicon: $.iconset_octicon || Iconpicker.ICONSET_EMPTY,
  45. typicon: $.iconset_typicon || Iconpicker.ICONSET_EMPTY,
  46. weathericon: $.iconset_weathericon || Iconpicker.ICONSET_EMPTY
  47. };
  48. // ICONPICKER DEFAULTS
  49. // ==============================
  50. Iconpicker.DEFAULTS = {
  51. align: 'center',
  52. arrowClass: 'btn-primary',
  53. arrowNextIconClass: 'glyphicon glyphicon-arrow-right',
  54. arrowPrevIconClass: 'glyphicon glyphicon-arrow-left',
  55. cols: 4,
  56. icon: '',
  57. iconset: 'glyphicon',
  58. header: true,
  59. labelHeader: '{0} / {1}',
  60. footer: true,
  61. labelFooter: '{0} - {1} of {2}',
  62. placement: 'bottom',
  63. rows: 4,
  64. search: true,
  65. searchText: 'Search icon',
  66. selectedClass: 'btn-warning',
  67. unselectedClass: 'btn-default'
  68. };
  69. // ICONPICKER PRIVATE METHODS
  70. // ==============================
  71. Iconpicker.prototype.bindEvents = function () {
  72. var op = this.options;
  73. var el = this;
  74. op.table.find('.btn-previous, .btn-next').off('click').on('click', function(e) {
  75. e.preventDefault();
  76. var inc = parseInt($(this).val(), 10);
  77. el.changeList(op.page + inc);
  78. });
  79. op.table.find('.btn-icon').off('click').on('click', function(e) {
  80. e.preventDefault();
  81. el.select($(this).val());
  82. if(op.inline === false){
  83. el.$element.popover('destroy');
  84. }
  85. else{
  86. op.table.find('i.' + $(this).val()).parent().addClass(op.selectedClass);
  87. }
  88. });
  89. op.table.find('.search-control').off('keyup').on('keyup', function() {
  90. el.changeList(1);
  91. });
  92. };
  93. Iconpicker.prototype.changeList = function (page) {
  94. this.filterIcons();
  95. this.updateLabels(page);
  96. this.updateIcons(page);
  97. this.options.page = page;
  98. this.bindEvents();
  99. };
  100. Iconpicker.prototype.filterIcons = function () {
  101. var op = this.options;
  102. var search = op.table.find('.search-control').val();
  103. if (search === "") {
  104. op.icons = Iconpicker.ICONSET[op.iconset].icons;
  105. }
  106. else {
  107. var result = [];
  108. $.each(Iconpicker.ICONSET[op.iconset].icons, function(i, v) {
  109. if (v.indexOf(search) > -1) {
  110. result.push(v);
  111. }
  112. });
  113. op.icons = result;
  114. }
  115. };
  116. Iconpicker.prototype.removeAddClass = function (target, remove, add) {
  117. this.options.table.find(target).removeClass(remove).addClass(add);
  118. return add;
  119. };
  120. Iconpicker.prototype.reset = function () {
  121. this.updatePicker();
  122. this.changeList(1);
  123. };
  124. Iconpicker.prototype.select = function (icon) {
  125. var op = this.options;
  126. var el = this.$element;
  127. op.selected = $.inArray(icon.replace(op.iconClassFix, ''), op.icons);
  128. if (op.selected === -1) {
  129. op.selected = 0;
  130. icon = op.iconClassFix + op.icons[op.selected];
  131. }
  132. if (icon !== '' && op.selected >= 0) {
  133. op.icon = icon;
  134. if(op.inline === false){
  135. el.find('input').val(icon);
  136. el.find('i').attr('class', '').addClass(op.iconClass).addClass(icon);
  137. }
  138. if(icon === op.iconClassFix){
  139. el.trigger({ type: "change", icon: 'empty' });
  140. }
  141. else {
  142. el.trigger({ type: "change", icon: icon });
  143. }
  144. op.table.find('button.' + op.selectedClass).removeClass(op.selectedClass);
  145. }
  146. };
  147. Iconpicker.prototype.switchPage = function (icon) {
  148. var op = this.options;
  149. op.selected = $.inArray(icon.replace(op.iconClassFix, ''), op.icons);
  150. if(op.selected >= 0) {
  151. var page = Math.ceil((op.selected + 1) / this.totalIconsPerPage());
  152. this.changeList(page);
  153. }
  154. if(icon === ''){
  155. op.table.find('i.' + op.iconClassFix).parent().addClass(op.selectedClass);
  156. }
  157. else{
  158. op.table.find('i.' + icon).parent().addClass(op.selectedClass);
  159. }
  160. };
  161. Iconpicker.prototype.totalPages = function () {
  162. return Math.ceil(this.totalIcons() / this.totalIconsPerPage());
  163. };
  164. Iconpicker.prototype.totalIcons = function () {
  165. return this.options.icons.length;
  166. };
  167. Iconpicker.prototype.totalIconsPerPage = function () {
  168. if(this.options.rows === 0){
  169. return this.options.icons.length;
  170. }
  171. else{
  172. return this.options.cols * this.options.rows;
  173. }
  174. };
  175. Iconpicker.prototype.updateArrows = function (page) {
  176. var op = this.options;
  177. var total_pages = this.totalPages();
  178. if (page === 1) {
  179. op.table.find('.btn-previous').addClass('disabled');
  180. }
  181. else {
  182. op.table.find('.btn-previous').removeClass('disabled');
  183. }
  184. if (page === total_pages || total_pages === 0) {
  185. op.table.find('.btn-next').addClass('disabled');
  186. }
  187. else {
  188. op.table.find('.btn-next').removeClass('disabled');
  189. }
  190. };
  191. Iconpicker.prototype.updateIcons = function (page) {
  192. var op = this.options;
  193. var tbody = op.table.find('tbody').empty();
  194. var offset = (page - 1) * this.totalIconsPerPage();
  195. var length = op.rows;
  196. if(op.rows === 0){
  197. length = op.icons.length;
  198. }
  199. for (var i = 0; i < length; i++) {
  200. var tr = $('<tr></tr>');
  201. for (var j = 0; j < op.cols; j++) {
  202. var pos = offset + (i * op.cols) + j;
  203. var btn = $('<button class="btn ' + op.unselectedClass + ' btn-icon"></button>').hide();
  204. if (pos < op.icons.length) {
  205. var v = op.iconClassFix + op.icons[pos];
  206. btn.val(v).attr('title', v).append('<i class="' + op.iconClass + ' ' + v + '"></i>').show();
  207. if (op.icon === v) {
  208. btn.addClass(op.selectedClass).addClass('btn-icon-selected');
  209. }
  210. }
  211. tr.append($('<td></td>').append(btn));
  212. }
  213. tbody.append(tr);
  214. }
  215. };
  216. Iconpicker.prototype.updateIconsCount = function () {
  217. var op = this.options;
  218. if(op.footer === true){
  219. var icons_count = [
  220. '<tr>',
  221. ' <td colspan="' + op.cols + '" class="text-center">',
  222. ' <span class="icons-count"></span>',
  223. ' </td>',
  224. '</tr>'
  225. ];
  226. op.table.find('tfoot').empty().append(icons_count.join(''));
  227. }
  228. };
  229. Iconpicker.prototype.updateLabels = function (page) {
  230. var op = this.options;
  231. var total_icons = this.totalIcons();
  232. var total_pages = this.totalPages();
  233. op.table.find('.page-count').html(op.labelHeader.replace('{0}', (total_pages === 0 ) ? 0 : page).replace('{1}', total_pages));
  234. var offset = (page - 1) * this.totalIconsPerPage();
  235. var total = page * this.totalIconsPerPage();
  236. op.table.find('.icons-count').html(op.labelFooter.replace('{0}', offset + 1).replace('{1}', (total < total_icons) ? total: total_icons).replace('{2}', total_icons));
  237. this.updateArrows(page);
  238. };
  239. Iconpicker.prototype.updatePagesCount = function () {
  240. var op = this.options;
  241. if(op.header === true){
  242. var tr = $('<tr></tr>');
  243. for (var i = 0; i < op.cols; i++) {
  244. var td = $('<td class="text-center"></td>');
  245. if (i === 0 || i === op.cols - 1) {
  246. var arrow = [
  247. '<button class="btn btn-arrow ' + ((i === 0) ? 'btn-previous' : 'btn-next') + ' ' + op.arrowClass + '" value="' + ((i === 0) ? -1 : 1) + '">',
  248. '<span class="' + ((i === 0) ? op.arrowPrevIconClass : op.arrowNextIconClass) + '"></span>',
  249. '</button>'
  250. ];
  251. td.append(arrow.join(''));
  252. tr.append(td);
  253. }
  254. else if (tr.find('.page-count').length === 0) {
  255. td.attr('colspan', op.cols - 2).append('<span class="page-count"></span>');
  256. tr.append(td);
  257. }
  258. }
  259. op.table.find('thead').empty().append(tr);
  260. }
  261. };
  262. Iconpicker.prototype.updatePicker = function () {
  263. var op = this.options;
  264. if (op.cols < 4) {
  265. throw 'Iconpicker => The number of columns must be greater than or equal to 4. [option.cols = ' + op.cols + ']';
  266. }
  267. else if (op.rows < 0) {
  268. throw 'Iconpicker => The number of rows must be greater than or equal to 0. [option.rows = ' + op.rows + ']';
  269. }
  270. else {
  271. this.updatePagesCount();
  272. this.updateSearch();
  273. this.updateIconsCount();
  274. }
  275. };
  276. Iconpicker.prototype.updateSearch = function () {
  277. var op = this.options;
  278. var search = [
  279. '<tr>',
  280. ' <td colspan="' + op.cols + '">',
  281. ' <input type="text" class="form-control search-control" style="width: ' + op.cols * 39 + 'px;" placeholder="' + op.searchText + '">',
  282. ' </td>',
  283. '</tr>'
  284. ];
  285. search = $(search.join(''));
  286. if (op.search === true) {
  287. search.show();
  288. }
  289. else {
  290. search.hide();
  291. }
  292. op.table.find('thead').append(search);
  293. };
  294. // ICONPICKER PUBLIC METHODS
  295. // ==============================
  296. Iconpicker.prototype.setAlign = function (value) {
  297. this.$element.removeClass(this.options.align).addClass(value);
  298. this.options.align = value;
  299. };
  300. Iconpicker.prototype.setArrowClass = function (value) {
  301. this.options.arrowClass = this.removeAddClass('.btn-arrow', this.options.arrowClass, value);
  302. };
  303. Iconpicker.prototype.setArrowNextIconClass = function (value) {
  304. this.options.arrowNextIconClass = this.removeAddClass('.btn-next > span', this.options.arrowNextIconClass, value);
  305. };
  306. Iconpicker.prototype.setArrowPrevIconClass = function (value) {
  307. this.options.arrowPrevIconClass = this.removeAddClass('.btn-previous > span', this.options.arrowPrevIconClass, value);
  308. };
  309. Iconpicker.prototype.setCols = function (value) {
  310. this.options.cols = value;
  311. this.reset();
  312. };
  313. Iconpicker.prototype.setFooter = function (value) {
  314. var footer = this.options.table.find('tfoot');
  315. if (value === true) {
  316. footer.show();
  317. }
  318. else {
  319. footer.hide();
  320. }
  321. this.options.footer = value;
  322. };
  323. Iconpicker.prototype.setHeader = function (value) {
  324. var header = this.options.table.find('thead');
  325. if (value === true) {
  326. header.show();
  327. }
  328. else {
  329. header.hide();
  330. }
  331. this.options.header = value;
  332. };
  333. Iconpicker.prototype.setIcon = function (value) {
  334. this.select(value);
  335. };
  336. Iconpicker.prototype.setIconset = function (value) {
  337. var op = this.options;
  338. if ($.isPlainObject(value)) {
  339. Iconpicker.ICONSET._custom = $.extend(Iconpicker.ICONSET_EMPTY, value);
  340. op.iconset = '_custom';
  341. }
  342. else if (!Iconpicker.ICONSET.hasOwnProperty(value)) {
  343. op.iconset = Iconpicker.DEFAULTS.iconset;
  344. }
  345. else {
  346. op.iconset = value;
  347. }
  348. op = $.extend(op, Iconpicker.ICONSET[op.iconset]);
  349. this.reset();
  350. this.select(op.icon);
  351. };
  352. Iconpicker.prototype.setLabelHeader = function (value) {
  353. this.options.labelHeader = value;
  354. this.updateLabels(this.options.page);
  355. };
  356. Iconpicker.prototype.setLabelFooter = function (value) {
  357. this.options.labelFooter = value;
  358. this.updateLabels(this.options.page);
  359. };
  360. Iconpicker.prototype.setPlacement = function (value) {
  361. this.options.placement = value;
  362. };
  363. Iconpicker.prototype.setRows = function (value) {
  364. this.options.rows = value;
  365. this.reset();
  366. };
  367. Iconpicker.prototype.setSearch = function (value) {
  368. var search = this.options.table.find('.search-control');
  369. if (value === true) {
  370. search.show();
  371. }
  372. else {
  373. search.hide();
  374. }
  375. search.val('');
  376. this.changeList(1);
  377. this.options.search = value;
  378. };
  379. Iconpicker.prototype.setSearchText = function (value) {
  380. this.options.table.find('.search-control').attr('placeholder', value);
  381. this.options.searchText = value;
  382. };
  383. Iconpicker.prototype.setSelectedClass = function (value) {
  384. this.options.selectedClass = this.removeAddClass('.btn-icon-selected', this.options.selectedClass, value);
  385. };
  386. Iconpicker.prototype.setUnselectedClass = function (value) {
  387. this.options.unselectedClass = this.removeAddClass('.btn-icon', this.options.unselectedClass, value);
  388. };
  389. // ICONPICKER PLUGIN DEFINITION
  390. // ========================
  391. var old = $.fn.iconpicker;
  392. $.fn.iconpicker = function (option, params) {
  393. return this.each(function () {
  394. var $this = $(this);
  395. var data = $this.data('bs.iconpicker');
  396. var options = typeof option === 'object' && option;
  397. if (!data) {
  398. $this.data('bs.iconpicker', (data = new Iconpicker(this, options)));
  399. }
  400. if (typeof option === 'string') {
  401. if (typeof data[option] === 'undefined') {
  402. throw 'Iconpicker => The "' + option + '" method does not exists.';
  403. }
  404. else {
  405. data[option](params);
  406. }
  407. }
  408. else{
  409. var op = data.options;
  410. op = $.extend(op, {
  411. inline: false,
  412. page: 1,
  413. selected: -1,
  414. table: $('<table class="table-icons"><thead></thead><tbody></tbody><tfoot></tfoot></table>')
  415. });
  416. var name = (typeof $this.attr('name') !== 'undefined') ? 'name="' + $this.attr('name') + '"' : '';
  417. if($this.prop('tagName') === 'BUTTON'){
  418. $this.empty()
  419. .append('<i></i>')
  420. .append('<input type="hidden" ' + name + '></input>')
  421. .append('<span class="caret"></span>')
  422. .addClass('iconpicker');
  423. data.setIconset(op.iconset);
  424. $this.on('click', function(e) {
  425. e.preventDefault();
  426. $this.popover({
  427. animation: false,
  428. trigger: 'manual',
  429. html: true,
  430. content: op.table,
  431. container: 'body',
  432. placement: op.placement
  433. }).on('shown.bs.popover', function () {
  434. data.switchPage(op.icon);
  435. data.bindEvents();
  436. });
  437. $this.data('bs.popover').tip().addClass('iconpicker-popover');
  438. $this.popover('show');
  439. });
  440. }
  441. else{
  442. op.inline = true;
  443. data.setIconset(op.iconset);
  444. $this.empty()
  445. .append('<input type="hidden" ' + name + '></input>')
  446. .append(op.table)
  447. .addClass('iconpicker')
  448. .addClass(op.align);
  449. data.switchPage(op.icon);
  450. data.bindEvents();
  451. }
  452. }
  453. });
  454. };
  455. $.fn.iconpicker.Constructor = Iconpicker;
  456. // ICONPICKER NO CONFLICT
  457. // ==================
  458. $.fn.iconpicker.noConflict = function () {
  459. $.fn.iconpicker = old;
  460. return this;
  461. };
  462. // ICONPICKER DATA-API
  463. // ===============
  464. $(document).on('click', 'body', function (e) {
  465. $('.iconpicker').each(function () {
  466. //the 'is' for buttons that trigger popups
  467. //the 'has' for icons within a button that triggers a popup
  468. if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
  469. $(this).popover('destroy');
  470. }
  471. });
  472. });
  473. $('button[role="iconpicker"],div[role="iconpicker"]').iconpicker();
  474. })(jQuery);