commit
102bff02b1
5 changed files with 87 additions and 16 deletions
@ -1,13 +1,7 @@ |
||||
{% load static %} |
||||
|
||||
<div id="expo_top_events"> |
||||
|
||||
</div> |
||||
<script> |
||||
/* НУЖНО ЛИ??? */ |
||||
window.sendData = { |
||||
"theme": [{{ themes|join:", " }}], |
||||
"country": "{{ country }}", |
||||
"city": "{{ city }}", |
||||
"tag": "{{ tag }}" |
||||
}; |
||||
var url = "/expo-b/get-tops/"; |
||||
</script> |
||||
|
||||
<script type="text/javascript" src="{% static 'client/js/rejs/tops.js' %}"></script> |
||||
@ -0,0 +1,72 @@ |
||||
(function () { |
||||
"use strict"; |
||||
|
||||
var API_URL_ROOT = "/expo-b/get-tops/"; |
||||
var PARENT_ID = "expo_top_events"; |
||||
|
||||
var getUrl = function () { |
||||
var catalog = ""; |
||||
|
||||
var parts = /^\/([^\/]+)(?:\/|$)/i.exec(location.pathname); |
||||
if (parts) { |
||||
catalog = parts[1]; |
||||
} |
||||
|
||||
var data = window.sendData; |
||||
|
||||
if (data instanceof Object) { |
||||
var search = ["catalog=" + catalog]; |
||||
|
||||
for (var key in data) { |
||||
if (data.hasOwnProperty(key)) { |
||||
var value = data[key]; |
||||
|
||||
if (value instanceof Array) { |
||||
for (var i = 0, l = value.length; i < l; i++) { |
||||
search.push(encodeURIComponent(key) + "=" + encodeURIComponent(value[i])); |
||||
} |
||||
} else { |
||||
search.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return API_URL_ROOT + "?" + search.join("&"); |
||||
} |
||||
|
||||
return API_URL_ROOT; |
||||
}; |
||||
|
||||
var getTops = function (url, callback) { |
||||
var xhr = new XMLHttpRequest(); |
||||
xhr.open("GET", url, true); |
||||
|
||||
xhr.onreadystatechange = function (event) { |
||||
if (event.target.readyState === 4) { |
||||
if (event.target.status === 200) { |
||||
try { |
||||
callback(event.target.responseText); |
||||
} catch (error) { |
||||
// do nothing
|
||||
} |
||||
} |
||||
} |
||||
}; |
||||
|
||||
xhr.send(); |
||||
}; |
||||
|
||||
var insertTops = function (text) { |
||||
var parent = document.getElementById(PARENT_ID); |
||||
|
||||
if (parent) { |
||||
parent.innerHTML = text; |
||||
} |
||||
}; |
||||
|
||||
var main = function () { |
||||
getTops(getUrl(), insertTops); |
||||
}; |
||||
|
||||
window.addEventListener("load", main); |
||||
})(); |
||||
Loading…
Reference in new issue