Merge pull request #12583 from omarabuhussein/dev/core#288
[civicrm-core.git] / templates / CRM / common / navigation.js.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2019 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}// http://civicrm.org/licensing
26 {capture assign=menuMarkup}{strip}
27 <ul id="civicrm-menu">
28 <li id="crm-qsearch" class="menumain">
29 <form action="{crmURL p='civicrm/contact/search/advanced' h=0 }" name="search_block" id="id_search_block" method="post">
30 <div id="quickSearch">
31 <input type="text" class="form-text" id="sort_name_navigation" placeholder="{ts}Contacts{/ts}" name="sort_name" style="width: 6em;" />
32 <input type="text" id="sort_contact_id" style="display: none" />
33 <input type="hidden" name="hidden_location" value="1" />
34 <input type="hidden" name="hidden_custom" value="1" />
35 <input type="hidden" name="qfKey" value="" />
36 <div style="height:1px; overflow:hidden;"><input type="submit" value="{ts}Go{/ts}" name="_qf_Advanced_refresh" class="crm-form-submit default" /></div>
37 </div>
38 </form>
39 <ul>
40 {foreach from=$quicksearchOptions item="optionLabel" key="optionKey"}
41 <li><label class="crm-quickSearchField"><input type="radio" {if $optionKey == 'sort_name'}checked="checked"{/if} value="{$optionKey}" name="quickSearchField"> {$optionLabel}</label></li>
42 {/foreach}
43 </ul>
44 </li>
45 {$navigation}
46 </ul>
47 {/strip}{/capture}// <script> Generated {$smarty.now|date_format:'%d %b %Y %H:%M:%S'}
48 {literal}
49 (function($) {
50 var menuMarkup = {/literal}{$menuMarkup|@json_encode}{literal};
51
52 //Track Scrolling
53 if ($('div.sticky-header').length) {
54 $(window).scroll(function () {
55 $('div.sticky-header').css({top: $('#civicrm-menu').height() + "px", position: "fixed"});
56 });
57 }
58
59 if ($('div#toolbar-box div.m').length) {
60 $('div#toolbar-box div.m').html(menuMarkup);
61 }
62 else if ($("#crm-nav-menu-container").length) {
63 $("#crm-nav-menu-container").html(menuMarkup).css({'padding-bottom': '10px'});
64 }
65 else {
66 $('body').append(menuMarkup);
67 }
68
69 // CRM-15493 get the current qfKey
70 $("input[name=qfKey]", "#quickSearch").val($('#civicrm-navigation-menu').data('qfkey'));
71
72 $('#civicrm-menu').ready(function() {
73 $('#root-menu-div .outerbox').css({'margin-top': '6px'});
74 $('#root-menu-div .menu-ul li').css({'padding-bottom': '2px', 'margin-top': '2px'});
75 $("#civicrm-menu >li").each(function(i){
76 $(this).attr("tabIndex",i+2);
77 });
78
79 $('#sort_name_navigation')
80 .autocomplete({
81 source: function(request, response) {
82 //start spinning the civi logo
83 $('.crm-logo-sm').addClass('crm-i fa-spin');
84 var
85 option = $('input[name=quickSearchField]:checked'),
86 params = {
87 name: request.term,
88 field_name: option.val()
89 };
90 CRM.api3('contact', 'getquick', params).done(function(result) {
91 var ret = [];
92 if (result.values.length > 0) {
93 $('#sort_name_navigation').autocomplete('widget').menu('option', 'disabled', false);
94 $.each(result.values, function(k, v) {
95 ret.push({value: v.id, label: v.data});
96 });
97 } else {
98 $('#sort_name_navigation').autocomplete('widget').menu('option', 'disabled', true);
99 var label = option.closest('label').text();
100 var msg = ts('{/literal}{ts escape='js' 1='%1'}%1 not found.{/ts}'{literal}, {1: label});
101 // Remind user they are not searching by contact name (unless they enter a number)
102 if (params.field_name && !(/[\d].*/.test(params.name))) {
103 msg += {/literal}' {ts escape='js'}Did you mean to search by Name/Email instead?{/ts}'{literal};
104 }
105 ret.push({value: '0', label: msg});
106 }
107 response(ret);
108 //stop spinning the civi logo
109 $('.crm-logo-sm').removeClass('crm-i fa-spin');
110 })
111 },
112 focus: function (event, ui) {
113 return false;
114 },
115 select: function (event, ui) {
116 if (ui.item.value > 0) {
117 document.location = CRM.url('civicrm/contact/view', {reset: 1, cid: ui.item.value});
118 }
119 return false;
120 },
121 create: function() {
122 // Place menu in front
123 $(this).autocomplete('widget')
124 .addClass('crm-quickSearch-results')
125 .css('z-index', $('#civicrm-menu').css('z-index'));
126 }
127 })
128 .keydown(function() {
129 $.Menu.closeAll();
130 })
131 .on('focus', function() {
132 setQuickSearchValue();
133 if ($(this).attr('style').indexOf('14em') < 0) {
134 $(this).animate({width: '14em'});
135 }
136 })
137 .on('blur', function() {
138 // Shrink if no input and menu is not open
139 if (!$(this).val().length && $(this).attr('style').indexOf('6em') < 0 && !$('.crm-quickSearchField:visible', '#root-menu-div').length) {
140 $(this).animate({width: '6em'});
141 }
142 });
143 $('.crm-hidemenu').click(function(e) {
144 $('#civicrm-menu').slideUp();
145 if ($('#crm-notification-container').length) {
146 var alert = CRM.alert({/literal}'<a href="#" id="crm-restore-menu" style="text-align: center; margin-top: -8px;">{ts escape='js'}Restore CiviCRM Menu{/ts}</a>'{literal}, '', 'none', {expires: 10000});
147 $('#crm-restore-menu')
148 .button({icons: {primary: 'fa-undo'}})
149 .click(function(e) {
150 e.preventDefault();
151 alert.close();
152 $('#civicrm-menu').slideDown();
153 })
154 .parent().css('text-align', 'center').find('.ui-button-text').css({'padding-top': '4px', 'padding-bottom': '4px'})
155 ;
156 }
157 e.preventDefault();
158 });
159 function setQuickSearchValue() {
160 var $selection = $('.crm-quickSearchField input:checked'),
161 label = $selection.parent().text(),
162 value = $selection.val();
163 // These fields are not supported by advanced search
164 if (!value || value === 'first_name' || value === 'last_name') {
165 value = 'sort_name';
166 }
167 $('#sort_name_navigation').attr({name: value, placeholder: label});
168 }
169 $('.crm-quickSearchField').click(function() {
170 setQuickSearchValue();
171 $.Menu.closeAll();
172 $('#sort_name_navigation').focus().autocomplete("search");
173 });
174 // Set & retrieve default value
175 if (window.localStorage) {
176 $('.crm-quickSearchField').click(function() {
177 localStorage.quickSearchField = $('input', this).val();
178 });
179 if (localStorage.quickSearchField) {
180 $('.crm-quickSearchField input[value=' + localStorage.quickSearchField + ']').prop('checked', true);
181 }
182 }
183 // redirect to view page if there is only one contact
184 $('#id_search_block').on('submit', function() {
185 var $menu = $('#sort_name_navigation').autocomplete('widget');
186 if ($('li.ui-menu-item', $menu).length === 1) {
187 var cid = $('li.ui-menu-item', $menu).data('ui-autocomplete-item').value;
188 if (cid > 0) {
189 document.location = CRM.url('civicrm/contact/view', {reset: 1, cid: cid});
190 return false;
191 }
192 }
193 });
194 // Close menu after selecting an item
195 $('#root-menu-div').on('click', 'a', $.Menu.closeAll);
196 });
197 $('#civicrm-menu').menuBar({arrowClass: 'crm-i fa-caret-right'});
198 $('#civicrm-menu').trigger('crmLoad');
199 $(window).on("beforeunload", function() {
200 $('.crm-logo-sm', '#civicrm-menu').addClass('crm-i fa-spin');
201 });
202 })(CRM.$);{/literal}