var zyk_search_module = {
uid: "",
company_id: 0,
data: {},
load_css: function() {
$('head').append($('').attr('href', '//static.getzyk.com/css/search.style.css'));
},
get_autocomplete(obj) {
var _this = this;
$.ajax({
url: '//search.getzyk.com/search/suggest/query',
type: "POST",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//data.id
$("#___zyk_autocomplete_dropdown").empty()
$.each(data.suggestions, function(index, item) {
var _item = "
" + item.term + ""
$("#___zyk_autocomplete_dropdown").append(_item)
});
if ($("#___zyk_autocomplete_dropdown").hasClass("hide")) {
$("#___zyk_autocomplete_dropdown").removeClass("hide")
}
_this.init_autocomplete_click()
}
})
},
get_search_id(obj) {
var _this = this;
$.ajax({
url: "//search.getzyk.com/search/query",
type: "POST",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//data.id
var apiCallCount = 0;
var findResults = setInterval(function() {
apiCallCount++;
_this.get_search_results(data.id, apiCallCount, findResults)
}, 1000, findResults, apiCallCount);
}
})
},
get_search_results(id, count, interval) {
if (count == 4) {
clearInterval(interval)
$("#___zyk_search_loader").addClass("hide")
return;
}
var _this = this;
$.getJSON("//search.getzyk.com/search/" + id, function(data) {
$("#___zyk_search_module_search-div").empty();
$("#___zyk_search_module_search-div").append("All Results");
$("#___zyk_search_module_search-div-content").empty();
$("#___zyk_search_module_search-div-content").append("All Results
")
if (data.results) {
var allResultsData = "";
for (var key in data.results) {
if (data.results.hasOwnProperty(key)) {
var _tab = "" + key + ""
$("#___zyk_search_module_search-div").append(_tab)
var _tab_content = ""
$.each(data.results[key].results, function(index, value) {
var _link = "" + value.text + ""
_tab_content += _link
allResultsData += _link
});
var _content = "" + key + "
" + _tab_content + "
";
$("#___zyk_search_module_search-div-content").append(_content);
}
}
$("#all_result_data").append(allResultsData)
_this.preventDefault()
}
})
.fail(function(err) {
console.log(err);
});
},
load_js: function() {
$.getScript("http://static.getzyk.com/js/jQueryTab.js")
.done(function(script, textStatus) {
console.log(textStatus);
$('.tabs-7').jQueryTab({
initialTab: 4,
tabInTransition: 'fadeIn',
tabOutTransition: 'fadeOut',
cookieName: 'active-tab-7'
});
})
.fail(function(jqxhr, settings, exception) {
console.log("error in tabs");
});
},
preventDefault: function() {
$('#___zyk_search_module_search-div li').on('click', function(event) {
event.preventDefault();
});
},
do_search() {
var _this = this;
var searchKey = $("#zyk_search_modeule_search_text_box").val()
var obj = {
q: searchKey,
uid: _this.uid
}
$("#___zyk_search_loader").removeClass("hide")
$("#___zyk_search_module_search-div").empty();
$("#___zyk_search_module_search-div").append("All Results");
$("#___zyk_search_module_search-div-content").empty();
$("#___zyk_search_module_search-div-content").append("All Results
")
_this.get_search_id(obj);
},
init_clicks: function() {
$('#___zyk_search_module_search-div li').on('click', function(event) {
event.preventDefault();
});
var _this = this;
$("#zyk_search_modeule_search_text_box").on("keyup", function(e) {
if (e.keyCode == 13) {
_this.do_search()
$("#___zyk_autocomplete_dropdown").addClass("hide")
} else {
var searchKey = $("#zyk_search_modeule_search_text_box").val()
var obj = {
term: searchKey,
source: 'crm',
companyId: ZYK64.encode(_this.company_id)
}
_this.get_autocomplete(obj);
}
});
$("#zyk_search_module_hide").on("click", function(e) {
$("#___zyk_search_module").addClass("hide")
});
$("#___zyk_search_button").on("click", function(e) {
_this.do_search()
$("#___zyk_autocomplete_dropdown").addClass("hide")
});
$("#___zyk_search_container").on('click', '.___zyk_search_mobile_pill', function(e) {
var idToSwitch = $(this).data("toggle");
$("#___zyk_search_module_search-div-content div.active").removeClass("active")
$("#" + idToSwitch).removeClass("fade").addClass("active")
$("#___zyk_search_module_search-div-content h3.active").removeClass("active")
$(this).addClass("active")
});
},
load_html: function(itself) {
$.ajax({
url: "//static.getzyk.com/search-template.html", //http://static.getzyk.com/search-template.html ../../templates/search-template.html
beforeSend: function(xhr) {
xhr.overrideMimeType("text/html");
}
})
.done(function(data) {
$('#content').append(data);
itself.init_clicks()
});
},
init_autocomplete_click: function() {
var _this = this;
$("#___zyk_autocomplete_dropdown>li").on("click", function(e) {
var searchBoxVal = $(this).find("a").text()
console.log(searchBoxVal);
$("#zyk_search_modeule_search_text_box").val(searchBoxVal)
_this.do_search()
$("#___zyk_autocomplete_dropdown").addClass("hide")
});
},
init: function(uid, company_id) {
this.load_html(this)
this.uid = String(uid)
this.company_id = String(company_id)
this.showAppDownloadIcon()
this.notificationWorker()
},
showSearchDialog: function() {
if ($("#___zyk_search_module").hasClass("hide")) {
$("#___zyk_search_module").removeClass("hide")
$("#zyk_search_modeule_search_text_box").focus()
} else {
console.log("Initialization failed");
}
},
showAppDownloadIcon() {
if (window.location.hostname === "localhost")
return
var url = "https://commons.getzyk.com:12005/web/app/" + this.uid + "/status";
_this = this;
$.getJSON(url, function(data) {
if (!data.isKeyboardEnabled && _this.readCookie("showDownloadDropdown")==null) {
var appendTextToAdd = '';
$("#header").append(appendTextToAdd)
$("#_close_the_download_header").on('click', function() {
$("#_zyk_keyboard_downloader_header").remove()
_this.createCookie("showDownloadDropdown","1",1)
});
}
});
},
createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
},
readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
},
eraseCookie(name) {
createCookie(name, "", -1);
},
subscribe(a_uid){
_pe.subscribe({}.pe,function(data){
console.log(data.status);
if(data.status=="SUBSCRIBED"){
console.log(data.subscriber_hash);//(This will be filled when the user subscribes to the notifications)
_pe.addProfileId(a_uid);
}
});
},
notificationWorker(){
var a_uid = this.uid
var _this = this;
if(a_uid!=null && document.location.protocol === "https:"){
$.ajaxSetup({ cache: true });
$.getScript("https://clientcdn.pushengage.com/core/12635.js", function(){
$.ajaxSetup({ cache: false });
_this.subscribe(a_uid)
_pe.isSubscribed(
function(isSubscribed){
console.log(isSubscribed);
});
});
}
}
}
var ZYK64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=ZYK64._utf8_encode(e);while(f>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=ZYK64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}