1/*!
2 * WHMCS Dynamic Client Dropdown Library
3 *
4 * Based upon Selectize.js
5 *
6 * @copyright Copyright (c) WHMCS Limited 2005-2015
7 * @license http://www.whmcs.com/license/ WHMCS Eula
8 */
9
10$(document).ready(function(){
11 if (typeof WHMCS.selectize !== "undefined") {
12 jQuery('.selectize-client-search').data('search-url', getClientSearchPostUrl());
13 WHMCS.selectize.clientSearch();
14 } else {
15 var clientDropdown = jQuery(".selectize-client-search");
16
17 var clientSearchSelectize = clientDropdown.selectize(
18 {
19 plugins: ['whmcs_no_results'],
20 valueField: clientDropdown.data('value-field'),
21 labelField: 'name',
22 searchField: ['name', 'email', 'companyname'],
23 create: false,
24 maxItems: 1,
25 preload: 'focus',
26 optgroupField: 'status',
27 optgroupLabelField: 'name',
28 optgroupValueField: 'id',
29 optgroups: [
30 {$order: 1, id: 'active', name: clientDropdown.data('active-label')},
31 {$order: 2, id: 'inactive', name: clientDropdown.data('inactive-label')}
32 ],
33 render: {
34 item: function (item, escape) {
35 if (typeof dropdownSelectClient == "function") {
36 dropdownSelectClient(
37 escape(item.id),
38 escape(item.name) + (item.companyname ? ' (' + escape(item.companyname) + ')' : '') +
39 (item.id > 0 ? ' - #' + escape(item.id) : ''),
40 escape(item.email)
41 );
42 }
43 return '<div><span class="name">' + escape(item.name) +
44 (item.companyname ? ' (' + escape(item.companyname) + ')' : '') +
45 (item.id > 0 ? ' - #' + escape(item.id) : '') + '</span></div>';
46 },
47 option: function (item, escape) {
48 return '<div><span class="name">'
49 + escape(item.name) + (item.companyname ? ' (' + escape(item.companyname) + ')' : '')
50 + (item.id > 0 ? ' - #' + escape(item.id) : '') + '</span>' +
51 (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') + '</div>';
52 }
53 },
54 load: function (query, callback) {
55 jQuery.ajax({
56 url: getClientSearchPostUrl(),
57 type: 'POST',
58 dataType: 'json',
59 data: {
60 dropdownsearchq: query,
61 clientId: currentValue
62 },
63 error: function () {
64 callback();
65 },
66 success: function (res) {
67 callback(res);
68 }
69 });
70 },
71 score: function(search) {
72 var score = this.getScoreFunction(search);
73 return function(item) {
74 var thisScore = score(item);
75 if (thisScore && item.status === 'inactive') {
76 thisScore = 0.0000001;
77 }
78 return thisScore;
79 };
80 },
81 onChange: function (value) {
82 if (jQuery('#goButton').length) {
83 if (value.length && value != currentValue) {
84 jQuery('#goButton').click();
85 }
86 }
87 },
88 onFocus: function () {
89 currentValue = clientSearchSelectize.getValue();
90 clientSearchSelectize.clear();
91 },
92 onBlur: function () {
93 if (clientSearchSelectize.getValue() == '' || clientSearchSelectize.getValue() < 1) {
94 clientSearchSelectize.setValue(currentValue);
95 }
96 }
97 });
98 var currentValue = '';
99
100 if (clientSearchSelectize.length) {
101 /**
102 * selectize assigns any items to an array. In order to be able to run additional
103 * functions on this (like auto-submit and clear).
104 *
105 * @link https://github.com/brianreavis/selectize.js/blob/master/examples/api.html
106 */
107 clientSearchSelectize = clientSearchSelectize[0].selectize;
108 }
109 }
110});
111