-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
1 lines (1 loc) · 11.1 KB
/
bundle.js
1
(()=>{"use strict";(()=>{const e="https://21.javascript.pages.academy/keksobooking";function t(e,t){const n=new XMLHttpRequest;return n.responseType="json",n.addEventListener("load",(function(){200===n.status?e(n.response):t("Статус ошибки: "+n.status+" "+n.statusText)})),n.addEventListener("error",(function(){t("Произошла ошибка соединения")})),n.addEventListener("timeout",(function(){t("Запрос не успел выполниться за "+n.timeout+"мс")})),n.timeout=1e4,n}window.ajax={load:function(n,o){const r=t(n,o);return r.open("GET",e+"/data"),r.send(),r},upload:function(n,o,r){const i=t(o,r);i.open("POST",e),i.send(n)}}})(),window.data={mapTopBorder:43,mapBottomBorder:630,mapLeftBorder:-32,mapRightBorder:1168},(()=>{const e=document.querySelector(".map__pin--main"),t=document.querySelector(".ad-form"),n=t.querySelectorAll("fieldset"),o=document.querySelector(".map__filters"),r=o.querySelectorAll("select"),i=o.querySelector("fieldset"),a=document.querySelector("#address"),c=document.querySelector(".map");function d(){c.querySelectorAll(".map__card.popup").forEach((e=>e.remove()))}function u(){c.classList.remove("map--faded"),t.classList.remove("ad-form--disabled"),n.forEach((e=>e.removeAttribute("disabled","true"))),o.classList.remove("ad-form--disabled"),r.forEach((e=>e.removeAttribute("disabled"))),i.removeAttribute("disabled")}function s(e,t){let n=e+32+", "+t;a.value=n}s(e.offsetLeft,e.offsetTop);const l=document.querySelector(".map__filters"),p=document.querySelector("#housing-type"),f=document.querySelector("#housing-price"),m=document.querySelector("#housing-rooms"),w=document.querySelector("#housing-guests");function v(e){window.pin.createPins(e.slice(0,5)),u(),function(){const e=window.pin.mapPinsBlock.querySelectorAll(".map__pin");e.forEach((t=>t.addEventListener("click",(function(){!function(t){e.forEach((e=>e.classList.remove("map__pin--active"))),t.classList.add("map__pin--active")}(t)}))))}(),l.addEventListener("change",(function(){window.util.debounce(function(e){d(),window.pin.removePins();let t=e;const n=t.filter((function(e){return"any"===p.value?t:e.offer.type===p.value})).filter((function(e){let n;return"any"===f.value?n=t:"middle"===f.value?e.offer.price>=1e4&&e.offer.price<=5e4&&(n=e.offer.price):"low"===f.value?e.offer.price<1e4&&(n=e.offer.price):"high"===f.value&&e.offer.price>5e4&&(n=e.offer.price),n})).filter((function(e){return"any"===m.value?t:e.offer.rooms===Number(m.value)})).filter((function(e){return"any"===w.value?t:e.offer.guests===Number(w.value)})),o=n.filter((function(e,t){return n.indexOf(e)===t}));window.pin.createPins(o.slice(0,5))}(e))}))}const y=function(){window.util.renderErrorMessage()};e.addEventListener("mousedown",(function(n){0===n.button&&(s(e.offsetLeft,e.offsetTop),t.classList.contains("ad-form--disabled")&&window.ajax.load(v,y))})),e.addEventListener("keydown",(function(n){window.util.isEnterEvent(n,(function(){s(e.offsetTop,e.offsetLeft),t.classList.contains("ad-form--disabled")&&(window.ajax.load(v,y),u())}))})),window.pageMap={pinHeight:87,pinWidth:32,addressField:a,deactivatePage:function(){c.classList.add("map--faded"),t.classList.add("ad-form--disabled"),n.forEach((e=>e.setAttribute("disabled","true"))),o.classList.add("ad-form--disabled"),r.forEach((e=>e.setAttribute("disabled","true"))),i.setAttribute("disabled","true"),window.pin.removePins(),d()},setAddress:s,onAdsLoadError:y,removeCardPopups:d,mainPin:e}})(),(()=>{const e=document.querySelector("#card").content.querySelector(".map__card"),t=e.querySelector(".popup__photo");window.card={adCard:function(n){const o=document.querySelector(".map__filters-container"),r=e.cloneNode(!0);r.querySelector(".popup__title").textContent=n.offer.title,r.querySelector(".popup__text--address").textContent=n.offer.address,r.querySelector(".popup__text--price").textContent=n.offer.price+"₽/ночь",r.querySelector(".popup__type").textContent=function(e){let t;return"flat"===e?t="Квартира":"bungalow"===e?t="Бунгало":"house"===e?t="Дом":"palace"===e&&(t="Дворец"),t}(n.offer.type),r.querySelector(".popup__text--capacity").textContent=n.offer.rooms+" комнаты для "+n.offer.guests+" гостей",r.querySelector(".popup__text--time").textContent="Заезд после "+n.offer.checkin+", выезд до "+n.offer.checkout,r.querySelector(".popup__features").innerHTML="",r.querySelector(".popup__features").appendChild(function(e){const t=document.createDocumentFragment();for(let n=0;n<e.offer.features.length;n++){const o=document.createElement("li");o.className="popup__feature popup__feature--"+e.offer.features[n],t.appendChild(o)}return t}(n)),r.querySelector(".popup__description").textContent=n.offer.description,r.querySelector(".popup__avatar").src=n.author.avatar,r.querySelector(".popup__photos").removeChild(r.querySelector(".popup__photo")),r.querySelector(".popup__photos").appendChild(function(e){const n=document.createDocumentFragment();for(let o=0;o<e.offer.photos.length;o++){const r=t.cloneNode(!0);r.src=e.offer.photos[o],n.appendChild(r)}return n}(n)),r.querySelector(".popup__close").addEventListener("click",(function(){window.pageMap.removeCardPopups()})),r.querySelector(".popup__close").addEventListener("keydown",(function(e){window.util.isEnterEvent(e,window.pageMap.removeCardPopups)})),document.addEventListener("keydown",(function(e){window.util.isEscEvent(e,window.pageMap.removeCardPopups)})),o.insertAdjacentElement("beforebegin",r)}}})(),(()=>{const e=document.querySelector(".map__pins");window.pin={createPins:function(t){const n=document.createDocumentFragment(),o=document.querySelector("#pin").content.querySelector("button");for(let e=0;e<t.length;e++){const r=o.cloneNode(!0);r.style="left:"+t[e].location.x+"px; top:"+t[e].location.y+"px;",r.querySelector("img").src=t[e].author.avatar,r.querySelector("img").alt=t[e].offer.description;const i=function(){window.card.adCard(t[e])};r.addEventListener("click",(function(){window.pageMap.removeCardPopups(),i()})),r.addEventListener("keydown",(function(e){window.util.isEnterEvent(e,(function(){window.pageMap.removeCardPopups(),i()}))})),n.appendChild(r)}e.appendChild(n)},removePins:function(){e.querySelectorAll(".map__pin").forEach((function(e){e.classList.contains("map__pin--main")||e.remove()}))},mapPinsBlock:e}})(),(()=>{const e=["gif","jpg","jpeg","png"],t={1:[1],2:[1,2],3:[1,2,3],100:[0]},n=document.querySelector("#type"),o=document.querySelector("#price");n.addEventListener("change",(function(){"bungalow"===n.value?(o.setAttribute("min","0"),o.setAttribute("placeholder","0")):"flat"===n.value?(o.setAttribute("min","1000"),o.setAttribute("placeholder","1000")):"house"===n.value?(o.setAttribute("min","5000"),o.setAttribute("placeholder","5000")):"palace"===n.value&&(o.setAttribute("min","10000"),o.setAttribute("placeholder","10000"))}));const r=document.querySelector("#room_number"),i=document.querySelector("#capacity"),a=document.querySelector(".ad-form__submit"),c=document.querySelector(".ad-form"),d=document.querySelector("main");function u(){const e=document.querySelector("#success").content.querySelector(".success").cloneNode(!0),t=document.createDocumentFragment();t.appendChild(e),d.appendChild(t),document.addEventListener("keydown",(function(e){window.util.isEscEvent(e,(function(){s()})),document.removeEventListener("keydown",s)})),document.addEventListener("click",(function(){s(),document.removeEventListener("click",s)}))}function s(){const e=d.querySelector(".success");e&&d.removeChild(e)}function l(){const e=document.querySelector("#error").content.querySelector(".error").cloneNode(!0),t=document.createDocumentFragment();t.appendChild(e),d.appendChild(t),document.addEventListener("keydown",(function(e){window.util.isEscEvent(e,(function(){p()})),document.removeEventListener("keydown",s)})),document.addEventListener("click",(function(){p(),document.removeEventListener("click",s)}))}function p(){const e=d.querySelector(".error");e&&d.removeChild(e)}r.addEventListener("change",(function(e){var n;e.target.setCustomValidity(""),n=r.value,i.querySelectorAll("option").forEach((function(e){e.disabled=!0})),t[n].forEach((function(e){i.querySelector('option[value="'+e+'"]').disabled=!1,i.value=e}))})),a.addEventListener("click",(function(e){!function(e){!function(){const e=-1===t[r.value].indexOf(+i.value)?"Количество гостей не влезут в выбранную комнату":"";i.setCustomValidity(e)}(),c.checkValidity()&&(e.preventDefault(),window.ajax.upload(new FormData(c),u,l),window.pageMap.deactivatePage(),window.pageMap.removeCardPopups(),c.reset(),window.pin.removePins())}(e)}));const f=document.querySelector("#timein"),m=document.querySelector("#timeout");f.addEventListener("change",(function(e){m.value=e.target.value})),m.addEventListener("change",(function(e){f.value=e.target.value}));const w=document.querySelector(".ad-form-header__preview img"),v=document.querySelector(".ad-form__photo"),y=document.querySelector("#avatar"),g=document.querySelector("#images");y.addEventListener("change",(function(){const t=y.files[0],n=t.name.toLowerCase();if(e.some((function(e){return n.endsWith(e)}))){const e=new FileReader;e.addEventListener("load",(function(){w.src=e.result})),e.readAsDataURL(t)}else y.value=""})),g.addEventListener("change",(function(){const t=g.files[0],n=t.name.toLowerCase();if(e.some((function(e){return n.endsWith(e)}))){const e=new FileReader;e.addEventListener("load",(function(){const t=document.createElement("img");t.style.height="60px",t.style.margin="5px",t.src=e.result,v.appendChild(t)})),e.readAsDataURL(t)}else g.value=""})),document.querySelector(".ad-form__reset").addEventListener("click",window.pageMap.deactivatePage)})(),window.pageMap.mainPin.addEventListener("mousedown",(function(e){e.preventDefault();let t={x:e.clientX,y:e.clientY};function n(e){e.preventDefault();const n=t.x-e.clientX,o=t.y-e.clientY;t={x:e.clientX,y:e.clientY};let r=window.pageMap.mainPin.offsetTop-o,i=window.pageMap.mainPin.offsetLeft-n;window.pageMap.mainPin.style.top=r+"px",window.pageMap.mainPin.style.left=i+"px",window.pageMap.setAddress(i,r),r>window.data.mapBottomBorder?window.pageMap.mainPin.style.top=window.data.mapBottomBorder+"px":r<window.data.mapTopBorder?window.pageMap.mainPin.style.top=window.data.mapTopBorder+"px":i<window.data.mapLeftBorder?window.pageMap.mainPin.style.left=window.data.mapLeftBorder+"px":i>window.data.mapRightBorder&&(window.pageMap.mainPin.style.left=window.data.mapRightBorder+"px")}window.pageMap.setAddress(window.pageMap.mainPin.offsetLeft,window.pageMap.mainPin.offsetTop),document.addEventListener("mousemove",n),document.addEventListener("mouseup",(function e(t){t.preventDefault(),document.removeEventListener("mousemove",n),document.removeEventListener("mouseup",e)}))})),window.util={isEscEvent:function(e,t){27===e.keyCode&&t()},isEnterEvent:function(e,t){13===e.keyCode&&t()},renderErrorMessage:function(){const e=document.createElement("div");e.classList.add("error-message"),e.textContent="Произошла неизвестная ошибка. Пожалуйста, обновите страницу.",document.body.insertAdjacentElement("afterbegin",e)},debounce:function(e){let t=null;return function(...n){t&&window.clearTimeout(t),t=window.setTimeout((function(){e(...n)}),500)}}}})();