-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
1 lines (1 loc) · 14 KB
/
bundle.js
1
(()=>{"use strict";(()=>{const e=document.querySelector(".map"),t=e.querySelector(".map__pins"),n=(e,t=0)=>{let n=t+Math.random()*(e+1-t);return Math.floor(n)};window.util={getRandomValue:n,isEscEvent:e=>t=>{t.preventDefault(),27===t.keyCode&&e()},isEnterEvent:e=>t=>{t.preventDefault(),13===t.keyCode&&e()},getNumberGen:()=>{let e=0;return()=>(e++,e)},getRandomElement:e=>e[n(e.length-1)],getPartialArray:e=>{const t=[];for(let o=0;o<=n(e.length-1);o++)t.push(e[o]);return t},createFragment:(e,t,n)=>{const o=document.createDocumentFragment();if(t<e.length)for(let i=0;i<t;i++)o.appendChild(n(e[i]));else for(let t=0;t<e.length;t++)o.appendChild(n(e[t]));return o},debounce:e=>{let t=null;return(...n)=>{t&&window.clearTimeout(t),t=window.setTimeout((()=>{e(...n)}),500)}},map:e,pinsList:t}})(),(()=>{const e=document.querySelector("#pin").content.querySelector(".map__pin");window.pin={PIN_SIZE:65,PIN_PROTRUSION_HEIGHT:10,renderPin:t=>{const n=e.cloneNode(!0);return n.style.left=t.location.x-32.5+"px",n.style.top=t.location.y-42.5+"px",n.querySelector("img").src=t.author.avatar,n.querySelector("img").alt=t.offer.title,n}}})(),(()=>{const e={PALACE:{NAME:"Дворец",MIN_PRICE:1e4},FLAT:{NAME:"Квартира",MIN_PRICE:1e3},HOUSE:{NAME:"Дом",MIN_PRICE:5e3},BUNGALOW:{NAME:"Бунгало",MIN_PRICE:0}},t=window.util.map,n=document.querySelector("#card").content.querySelector(".map__card"),o=n.querySelector(".popup__close"),i=t.querySelector(".map__filters-container"),r=(e,t,n,o)=>{if(o)switch(e.querySelector("."+t).classList.remove("visually-hidden"),n){case"textContent":e.querySelector("."+t).textContent=o;break;case"src":e.querySelector("."+t).src=o}else e.querySelector("."+t).classList.add("visually-hidden")},a=()=>{n.remove(),o.removeEventListener("click",s),document.removeEventListener("keydown",d)},s=e=>{e.preventDefault(),a()},d=e=>{"Escape"===e.key&&(e.preventDefault(),a())};window.card={pinCard:n,mapFormContainer:i,renderPinCard:(t,n)=>{const o=n.querySelector(".popup__features"),i=n.querySelector(".popup__photos");r(n,"popup__title","textContent",t.offer.title),r(n,"popup__text--address","textContent",t.offer.address),r(n,"popup__text--price","textContent",t.offer.price+"₽/ночь"),r(n,"popup__type","textContent",""+e[t.offer.type.toUpperCase()].NAME),r(n,"popup__text--capacity","textContent",`${t.offer.rooms} комнаты для ${t.offer.guests} гостей`),r(n,"popup__text--time","textContent",`Заезд после ${t.offer.checkin}, выезд до ${t.offer.checkout}`),r(n,"popup__description","textContent",t.offer.description),r(n,"popup__avatar","src",t.author.avatar),((e,t)=>{if(t.length){e.classList.remove("visually-hidden");const n=e.querySelector("li"),o=document.createDocumentFragment();for(let e=0;e<t.length;e++){const i=n.cloneNode(!0);i.className="popup__feature",i.classList.add("popup__feature--"+t[e]),o.appendChild(i)}e.innerHTML="",e.appendChild(o)}else e.classList.add("visually-hidden")})(o,t.offer.features),((e,t)=>{if(t.length){e.classList.remove("visually-hidden");const n=e.querySelector("img"),o=document.createDocumentFragment();for(let e=0;e<t.length;e++){const i=n.cloneNode(!0);i.src=t[e],o.appendChild(i)}e.innerHTML="",e.appendChild(o)}else e.classList.add("visually-hidden")})(i,t.offer.photos)},removePinCard:a,onMapShowPinCard:e=>r=>{r.preventDefault();const a=r.target.closest(".map__pin");if(a&&!a.classList.contains("map__pin--main")){window.map.activatePinMark(a),o.addEventListener("click",s),document.addEventListener("keydown",d);const r=a.querySelector("img").alt;window.card.renderPinCard(e.find((e=>e.offer.title===r)),n),t.insertBefore(n,i)}},HousingTypesRu:e}})(),(()=>{const e=(e,t)=>{const n=new XMLHttpRequest;return n.responseType="json",n.timeout=1e4,n.addEventListener("load",(()=>{200===n.status?e(n.response):t("Статус ответа: "+n.status+" "+n.statusText)})),n.addEventListener("error",(()=>{t("Произошла ошибка соединения")})),n.addEventListener("timeout",(()=>{t("Запрос не успел выполниться за "+n.timeout+"мс")})),n};window.load={getData:(t,n)=>{const o=e(t,n);o.open("GET","https://21.javascript.pages.academy/keksobooking/data"),o.send()},sendFormData:(t,n,o)=>{const i=e(n,o);i.open("POST","https://21.javascript.pages.academy/keksobooking"),i.send(t)}}})(),(()=>{const e=window.util.pinsList.scrollWidth;window.move={onMainPinMove:t=>{const n=window.map.mainPin;t.preventDefault();const o={x:t.clientX,y:t.clientY},i=t=>{t.preventDefault();const i=o.x-t.clientX,r=o.y-t.clientY;o.x=t.clientX,o.y=t.clientY,n.offsetTop+window.pin.PIN_SIZE+window.pin.PIN_PROTRUSION_HEIGHT<130?n.style.top=130-window.pin.PIN_SIZE-window.pin.PIN_PROTRUSION_HEIGHT+"px":n.offsetTop+window.pin.PIN_SIZE+window.pin.PIN_PROTRUSION_HEIGHT>630?n.style.top=630-window.pin.PIN_SIZE-window.pin.PIN_PROTRUSION_HEIGHT+"px":n.style.top=n.offsetTop-r+"px",n.offsetLeft+Math.ceil(window.pin.PIN_SIZE/2)<0?n.style.left=0-Math.ceil(window.pin.PIN_SIZE/2)+"px":n.offsetLeft+Math.ceil(window.pin.PIN_SIZE/2)>e?n.style.left=e-Math.ceil(window.pin.PIN_SIZE/2)+"px":n.style.left=n.offsetLeft-i+"px",window.form.setAdAddress(!0)},r=e=>{e.preventDefault(),document.removeEventListener("mousemove",i),document.removeEventListener("mouseup",r),window.form.setAdAddress()};document.addEventListener("mousemove",i),document.addEventListener("mouseup",r)}}})(),(()=>{const e=window.util.map,t=window.util.pinsList,n=window.card.mapFormContainer.querySelector(".map__filters"),o=n.querySelectorAll("fieldset"),i=window.card.pinCard,r=e.querySelector(".map__pin--main"),a=t.children,s=e=>{t.appendChild(window.util.createFragment(e,5,window.pin.renderPin))},d=()=>{for(let e=a.length-1;e>1;e--)a[e].remove()},l=e=>{e.preventDefault(),0===e.button&&(window.load.getData(window.main.activateMainPage,c),r.removeEventListener("mousedown",l))},c=e=>{const t=document.createElement("div");t.style="z-index: 100; margin: 0 auto; text-align: center; background-color: red;",t.style.position="absolute",t.style.left=0,t.style.right=0,t.style.fontSize="30px",t.textContent=e,document.body.insertAdjacentElement("afterbegin",t)};r.addEventListener("mousedown",l),r.addEventListener("keydown",(e=>{e.preventDefault(),"Enter"===e.key&&window.load.getData(window.main.activateMainPage,c)})),window.map={PINS_NUMBER:5,mainPin:r,pinsList:t,mapForm:n,initializeMap:()=>{n.reset(),(()=>{for(let e=0;e<o.length;e++)o[e].setAttribute("disabled","");window.filter.deactivateFilter()})(),i.remove(),e.classList.add("map--faded"),d(),r.style="left: 570px; top: 375px;",r.removeEventListener("mousedown",window.move.onMainPinMove),r.addEventListener("mousedown",l)},activateMap:t=>{window.filter.fillAds(t),s(t),e.classList.remove("map--faded"),(()=>{for(let e=0;e<o.length;e++)o[e].removeAttribute("disabled");window.filter.activateFilter()})(),e.addEventListener("click",window.card.onMapShowPinCard(t)),r.addEventListener("mousedown",window.move.onMainPinMove),window.form.setAdAddress(!0)},fillInPinsList:s,resetPinsList:d,activatePinMark:e=>{for(let e=2;e<a.length;e++)a[e].classList.remove("map__pin--active");e.classList.add("map__pin--active")}}})(),(()=>{const e=window.map.mapForm,t=window.util.pinsList,n=e.querySelector("#housing-type"),o=e.querySelector("#housing-price"),i=e.querySelector("#housing-rooms"),r=e.querySelector("#housing-guests"),a=e.querySelector("#housing-features"),s={"housing-type":"any","housing-price":"any","housing-rooms":"any","housing-guest":"any"};let d=[];const l=[],c={ANY:{MIN:"",MAX:""},LOW:{MIN:0,MAX:9999},MIDDLE:{MIN:1e4,MAX:49999},HIGH:{MIN:5e4,MAX:1e6}},u=e=>{let t=0;return e.offer.type===s["housing-type"]&&(t+=1),e.offer.rooms===Number.parseInt(s["housing-rooms"],10)&&(t+=1),e.offer.guests===Number.parseInt(s["housing-guests"],10)&&(t+=1),e.offer.price>c[s["housing-price"].toUpperCase()].MIN&&e.offer.price<c[s["housing-price"].toUpperCase()].MAX&&(t+=1),t+=(e=>{let t=0;return l.forEach((n=>{e.offer.features.includes(n)&&(t+=1)})),t})(e),t},p=()=>{window.card.removePinCard(),window.map.resetPinsList(),t.appendChild(window.util.createFragment(d.sort(((e,t)=>{let n=u(t)-u(e);return 0===n&&(n=((e,t)=>e>t?1:e<t?-1:0)(e.offer.price,t.offer.price)),n})),window.map.PINS_NUMBER,window.pin.renderPin))},m=e=>{s[e.name]=e.value},w=window.util.debounce((()=>{m(n),p()})),v=window.util.debounce((()=>{m(o),p()})),f=window.util.debounce((()=>{m(i),p()})),y=window.util.debounce((()=>{m(r),p()})),E=window.util.debounce((e=>{let t=0,n=0;e.target.classList.contains("map__feature")&&(t=e.target),t&&(n=a.querySelector("#"+t.getAttribute("for")),l.includes(n.value)?(l.splice(l.indexOf(n.value),1),n.removeAttribute("checked","checked")):(l.push(n.value),n.setAttribute("checked","checked")),p())}));window.filter={fillAds:e=>{d=e},activateFilter:()=>{n.addEventListener("change",w),o.addEventListener("change",v),i.addEventListener("change",f),r.addEventListener("change",y),a.addEventListener("mousedown",E)},deactivateFilter:()=>{n.removeEventListener("change",w),o.removeEventListener("change",v),i.removeEventListener("change",f),r.removeEventListener("change",y),a.removeEventListener("mousedown",E)}}})(),(()=>{const e=99,t=document.querySelector(".ad-form"),n=t.querySelectorAll("fieldset"),o=t.querySelector("#address"),i=t.querySelector("#type"),r=t.querySelector("#price"),a=t.querySelector("#timein"),s=t.querySelector("#timeout"),d=t.querySelector("#room_number"),l=t.querySelector("#capacity"),c=window.map.mainPin,u=document.querySelector("#success").content.querySelector(".success"),p=document.querySelector("#error").content.querySelector(".error"),m=t.querySelector(".ad-form__reset"),w=e=>{e.preventDefault(),window.main.initializeMainPage(),m.removeEventListener("click",w)},v=(e=!0)=>{o.value=e?`${Math.round(Number.parseInt(c.style.left.slice(0,-2),10)+window.pin.PIN_SIZE/2)}, ${Math.round(Number.parseInt(c.style.top.slice(0,-2),10)+window.pin.PIN_SIZE+window.pin.PIN_PROTRUSION_HEIGHT)}`:`${Math.round(Number.parseInt(c.style.left.slice(0,-2),10)+window.pin.PIN_SIZE/2)}, ${Math.round(Number.parseInt(c.style.top.slice(0,-2),10)+window.pin.PIN_SIZE/2)}`},f=()=>{const e=i.value;r.value<window.card.HousingTypesRu[e.toUpperCase()].MIN_PRICE?r.setCustomValidity(`Минимальная цена за ночь ${window.card.HousingTypesRu[e.toUpperCase()].MIN_PRICE}р`):r.setCustomValidity(""),r.reportValidity()},y=()=>{const e=i.value;r.min=window.card.HousingTypesRu[e.toUpperCase()].MIN_PRICE,r.placeholder=window.card.HousingTypesRu[e.toUpperCase()].MIN_PRICE},E=(e,t,n)=>{e.remove(),document.removeEventListener("click",t),document.removeEventListener("keydown",n)},g=e=>{e.preventDefault(),E(u,g,h)},h=()=>{window.util.isEscEvent(E(u,g,h))},_=e=>{e.preventDefault(),E(p,_,I)},I=()=>{window.util.isEscEvent(E(p,_,I))},L=()=>{window.main.initializeMainPage(),document.querySelector("main").insertAdjacentElement("afterbegin",u),document.addEventListener("click",g),document.addEventListener("keydown",h)},S=()=>{document.querySelector("main").insertAdjacentElement("afterbegin",p),document.addEventListener("click",_),document.addEventListener("keydown",I)},N=e=>{e.preventDefault();const t=e.target.closest(".ad-form");window.load.sendFormData(new FormData(t),L,S)};window.form={adForm:t,initializeAdForm:()=>{t.reset(),t.classList.add("ad-form--disabled"),(()=>{for(let e=0;e<n.length;e++)n[e].setAttribute("disabled","")})(),o.setAttribute("readonly",""),v(!1),window.preview.deactivateChoosers()},activateAdForm:()=>{var o,c,u,p;s.value=a.value,r.min=window.card.HousingTypesRu[i.value.toUpperCase()].MIN_PRICE,r.placeholder=window.card.HousingTypesRu[i.value.toUpperCase()].MIN_PRICE,l.value=d.value<e?d.value:0,t.classList.remove("ad-form--disabled"),t.addEventListener("submit",N),(()=>{for(let e=0;e<n.length;e++)n[e].removeAttribute("disabled")})(),c=s,(o=a).addEventListener("change",(()=>{c.selectedIndex=o.selectedIndex})),c.addEventListener("change",(()=>{o.selectedIndex=c.selectedIndex})),p=d,(u=l).addEventListener("change",(()=>{const t=Number.parseInt(p.value,10),n=Number.parseInt(u.value,10);t>e&&0!==n?u.setCustomValidity("Выбранное помещение не для гостей"):t<=e&&0===n?u.setCustomValidity("Выбранное помещение предусмотрено для гостей"):t<n?u.setCustomValidity("Максимальное число гостей "+t):u.setCustomValidity(""),u.reportValidity()})),p.addEventListener("change",(()=>{const t=Number.parseInt(p.value,10),n=Number.parseInt(u.value,10),o=u.querySelectorAll("option");t<n&&t<=e?u.setCustomValidity("Максимальное число гостей "+t):t<=e&&0===n?u.setCustomValidity("Выбранное помещение предусмотрено для гостей"):t>e&&0!==n?u.setCustomValidity("Выбранное помещение не для гостей"):u.setCustomValidity(""),o.forEach((e=>{e.removeAttribute("disabled")})),t<=e?o.forEach((e=>{(e.value>t||0===Number.parseInt(e.value,10))&&e.setAttribute("disabled","disabled")})):o.forEach((e=>{0!==Number.parseInt(e.value,10)&&e.setAttribute("disabled","disabled")}))})),r.addEventListener("input",f),i.addEventListener("input",y),m.addEventListener("click",w),window.preview.activateChoosers()},setAdAddress:v}})(),(()=>{const e=["gif","jpg","jpeg","png"],t=window.form.adForm,n=t.querySelector("#avatar"),o=t.querySelector(".ad-form-header__preview").querySelector("img"),i=t.querySelector("#images"),r=t.querySelector(".ad-form__photo"),a=(e,t,n)=>()=>{const o=e.files[0],i=o.type.toLowerCase();t.some((function(e){return i.endsWith(e)}))&&((e,t)=>{const n=new FileReader;n.addEventListener("load",(function(){t(n)})),n.readAsDataURL(e)})(o,n)},s=a(n,e,(e=>{o.src=e.result})),d=a(i,e,(e=>{r.innerHTML="";const t=document.createElement("img");t.src=e.result,t.style="max-width: 100%; max-height: 100%",r.appendChild(t)})),l=()=>{n.addEventListener("change",s),i.addEventListener("change",d)};l(),window.preview={activateChoosers:l,deactivateChoosers:()=>{n.removeEventListener("change",s),i.removeEventListener("change",d),r.innerHTML="",o.src="img/muffin-grey.svg"}}})(),(()=>{const e=()=>{window.map.initializeMap(),window.form.initializeAdForm()};e(),window.main={initializeMainPage:e,activateMainPage:e=>{window.map.activateMap(e),window.form.activateAdForm()}}})()})();