CRM-19723 - Display activity type icons in ui
[civicrm-core.git] / js / jquery / jquery.crmIconPicker.js
CommitLineData
0c6fe5b5
CW
1// https://civicrm.org/licensing
2(function($, _) {
3 "use strict";
4 /* jshint validthis: true */
5 var icons = [], loaded;
6
7 $.fn.crmIconPicker = function() {
8
9 function loadIcons() {
10 if (!loaded) {
11 loaded = $.Deferred();
12 CRM.$.get(CRM.config.resourceBase + 'bower_components/font-awesome/css/font-awesome.css').done(function(data) {
13 var match,
14 regex = /\.(fa-[-a-zA-Z0-9]+):before {/g;
15 while((match = regex.exec(data)) !== null) {
16 icons.push(match[1]);
17 }
18 loaded.resolve();
19 });
20 }
21 return loaded;
22 }
23
24 return this.each(function() {
25 if ($(this).hasClass('iconpicker-widget')) {
26 return;
27 }
28 var $input = $(this),
29 button = $('<a href="#" />').button({
30 label: $(this).val() || ts('None'),
31 icons: {primary: $(this).val()}
32 }).attr('title', $input.attr('title'));
33 $input.hide().addClass('iconpicker-widget').after(button).change(function() {
34 button.button('option', {
35 label: $(this).val() || ts('None'),
36 icons: {primary: $(this).val()}
37 });
38 });
39
40 button.click(function(e) {
41 var dialog;
42
43 function displayIcons() {
44 var term = $('input[name=search]', dialog).val().replace(/-/g, '').toLowerCase(),
45 $place = $('div.icons', dialog);
46 $place.html('');
47 $.each(icons, function(i, icon) {
48 if (!term.length || icon.replace(/-/g, '').indexOf(term) > -1) {
49 var item = $('<a href="#" title="' + icon + '"/>').button({
50 icons: {primary: icon}
51 });
52 $place.append(item);
53 }
54 });
55 }
56
57 function displayDialog() {
58 dialog.append('<style type="text/css">' +
59 '#crmIconPicker {font-size: 2em;}' +
60 '#crmIconPicker .icon-search input {font-family: FontAwesome; padding-left: .5em; margin-bottom: 1em;}' +
61 '#crmIconPicker a.ui-button {width: 2em; height: 2em; color: #222;}' +
62 '#crmIconPicker a.ui-button .ui-icon {margin-top: -0.5em; width: auto; height: auto;}' +
63 '</style>' +
64 '<div class="icon-search"><input class="crm-form-text" name="search" placeholder="&#xf002"/></div>' +
65 '<div class="icons"></div>'
66 );
67 displayIcons();
68 dialog.unblock();
69 }
70
71 function pickIcon(e) {
72 var newIcon = $(this).attr('title');
73 $input.val(newIcon).change();
74 dialog.dialog('close');
75 e.preventDefault();
76 }
77
78 dialog = $('<div id="crmIconPicker"/>').dialog({
79 title: $input.attr('title'),
80 width: '80%',
81 height: 400,
82 modal: true
83 }).block()
84 .on('click', 'a', pickIcon)
85 .on('keyup', 'input[name=search]', displayIcons)
86 .on('dialogclose', function() {
87 $(this).dialog('destroy').remove();
88 });
89 loadIcons().done(displayDialog);
90 e.preventDefault();
91 });
92 });
93 };
94}(CRM.$, CRM._));