CRM-12337 - Convert navigation script to ajax with browser caching
[civicrm-core.git] / templates / CRM / common / Navigation.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *}
26 {capture assign=menuMarkup}{strip}
27 <ul id="civicrm-menu">
28 {if call_user_func(array('CRM_Core_Permission','giveMeAllACLs'))}
29 <li id="crm-qsearch" class="menumain crm-link-home">
30 <form action="{crmURL p='civicrm/contact/search/advanced' h=0 }" name="search_block" id="id_search_block" method="post">
31 <div id="quickSearch">
32 <input type="text" class="form-text" id="sort_name_navigation" placeholder="{ts}Find Contacts{/ts}" name="sort_name" style="width: 12em;" />
33 <input type="text" id="sort_contact_id" style="display: none" />
34 <input type="hidden" name="hidden_location" value="1" />
35 <input type="hidden" name="qfKey" value="{crmKey name='CRM_Contact_Controller_Search' addSequence=1}" />
36 <div style="height:1px; overflow:hidden;"><input type="submit" value="{ts}Go{/ts}" name="_qf_Advanced_refresh" class="form-submit default" /></div>
37 </div>
38 </form>
39 <ul>
40 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" checked="checked" value="" name="quickSearchField">{ts}Name/Email{/ts}</label></li>
41 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="contact_id" name="quickSearchField">{ts}CiviCRM ID{/ts}</label></li>
42 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="external_identifier" name="quickSearchField">{ts}External ID{/ts}</label></li>
43 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="first_name" name="quickSearchField">{ts}First Name{/ts}</label></li>
44 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="last_name" name="quickSearchField">{ts}Last Name{/ts}</label></li>
45 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="eml" value="email" name="quickSearchField">{ts}Email{/ts}</label></li>
46 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="phe" value="phone_numeric" name="quickSearchField">{ts}Phone{/ts}</label></li>
47 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="street_address" name="quickSearchField">{ts}Street Address{/ts}</label></li>
48 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="city" name="quickSearchField">{ts}City{/ts}</label></li>
49 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="sts" value="postal_code" name="quickSearchField">{ts}Postal Code{/ts}</label></li>
50 <li><label class="crm-quickSearchField"><input type="radio" data-tablename="cc" value="job_title" name="quickSearchField">{ts}Job Title{/ts}</label></li>
51 </ul>
52 </li>
53 {/if}
54 {$navigation}
55 </ul>
56 {/strip}{/capture}{literal}
57 (function($) {
58 var menuMarkup = {/literal}{$menuMarkup|@json_encode}{literal};
59 $(function() {
60 $("#civicrm-menu >li").each(function(i){
61 $(this).attr("tabIndex",i+2);
62 });
63
64 var contactUrl = {/literal}"{crmURL p='civicrm/ajax/rest' q='className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=navigation' h=0 }"{literal};
65
66 $('#sort_name_navigation').autocomplete(contactUrl, {
67 width: 200,
68 selectFirst: false,
69 minChars: 1,
70 matchContains: true,
71 delay: 400,
72 max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal},
73 extraParams:{
74 fieldName:function () {
75 return $('input[name=quickSearchField]:checked').val();
76 },
77 tableName:function () {
78 return $('input[name=quickSearchField]:checked').attr("data-tablename");
79 }
80 }
81 }).result(function(event, data, formatted) {
82 document.location = CRM.url('civicrm/contact/view', {reset: 1, cid: data[1]});
83 return false;
84 });
85 $('#sort_name_navigation').keydown(function() {
86 $.Menu.closeAll();
87 });
88 $('.crm-quickSearchField').click(function() {
89 var label = $(this).text();
90 var value = $('input', this).val();
91 // These fields are not supported by advanced search
92 if (value === 'first_name' || value === 'last_name') {
93 value = 'sort_name';
94 }
95 $('#sort_name_navigation').attr({name: value, placeholder: label}).flushCache().focus();
96 });
97 // check if there is only one contact and redirect to view page
98 $('#id_search_block').on('submit', function() {
99 var contactId, sortValue = $('#sort_name_navigation').val();
100 if (sortValue && $('#sort_name_navigation').attr('name') == 'sort_name') {
101 {/literal}{*
102 // FIXME: async:false == bad,
103 // we should just check the autocomplete results instead of firing a new request
104 // when we fix this, the civicrm/ajax/contact server-side callback can be removed as well
105 // also that would fix the fact that this only works with sort_name search
106 // (and we can remove the above conditional)
107 *}{literal}
108 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/contact' h=0 q='name='}"{literal} + sortValue;
109 contactId = $.ajax({
110 url: dataUrl,
111 async: false
112 }).responseText;
113 }
114 if (contactId && !isNaN(parseInt(contactId))) {
115 var url = {/literal}"{crmURL p='civicrm/contact/view' h=0 q='reset=1&cid='}"{literal} + contactId;
116 this.action = url;
117 }
118 });
119 });
120
121 {/literal}{if $config->userFramework neq 'Joomla' and $config->userFrameworkFrontend ne 1}{literal}
122 $('body').prepend(menuMarkup);
123
124 //Track Scrolling
125 $(window).scroll(function () {
126 var scroll = document.documentElement.scrollTop || document.body.scrollTop;
127 $('#civicrm-menu').css({top: "scroll", position: "fixed", top: "0px"});
128 $('div.sticky-header').css({ 'top' : "23px", position: "fixed" });
129 });
130
131 if ($('#edit-shortcuts').length > 0) {
132 $('#civicrm-menu').css({ 'width': '97%' });
133 }
134 {/literal}{elseif $config->userFrameworkFrontend ne 1}{* Special menu hacks for Joomla *}{literal}
135 // below div is present in older version of joomla 2.5.x
136 var elementExists = $('div#toolbar-box div.m').length;
137 if (elementExists > 0) {
138 $('div#toolbar-box div.m').html(menuMarkup);
139 }
140 else {
141 $("#crm-nav-menu-container").html(menuMarkup).css({'padding-bottom': '10px'});
142 }
143
144 $('#civicrm-menu').ready(function() {
145 $('#root-menu-div .outerbox').css({ 'margin-top': '6px'});
146 $('#root-menu-div .outerbox').first().css({ 'margin-top': '20px'});
147 $('#root-menu-div .menu-ul li').css({ 'padding-bottom' : '2px', 'margin-top' : '2px' });
148 $('img.menu-item-arrow').css({ 'top' : '4px' });
149 });
150 {/literal}{/if}{literal}
151 $('#civicrm-menu').menu({arrowSrc: CRM.config.resourceBase + 'packages/jquery/css/images/arrow.png'});
152 })(cj);{/literal}