From 02284958a1189ffcb10b34a4c3a02417f8136a4d Mon Sep 17 00:00:00 2001 From: Anson Bridges Date: Mon, 11 Aug 2025 22:24:05 -0700 Subject: Initialize git repo from local project files --- static/js/codemirror/tablist.js | 44 + static/js/editor.js | 805 ++++++++++++++++ static/js/leaflet.js | 6 + static/js/simplemde.js | 2028 +++++++++++++++++++++++++++++++++++++++ static/js/utils.js | 18 + 5 files changed, 2901 insertions(+) create mode 100644 static/js/codemirror/tablist.js create mode 100644 static/js/editor.js create mode 100644 static/js/leaflet.js create mode 100644 static/js/simplemde.js create mode 100644 static/js/utils.js (limited to 'static/js') diff --git a/static/js/codemirror/tablist.js b/static/js/codemirror/tablist.js new file mode 100644 index 0000000..e6cf2d4 --- /dev/null +++ b/static/js/codemirror/tablist.js @@ -0,0 +1,44 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +var CodeMirror = require("codemirror"); + +CodeMirror.commands.tabAndIndentMarkdownList = function (cm) { + var ranges = cm.listSelections(); + var pos = ranges[0].head; + var eolState = cm.getStateAfter(pos.line); + var inList = eolState.list !== false; + + if (inList) { + cm.execCommand("indentMore"); + return; + } + + if (cm.options.indentWithTabs) { + cm.execCommand("insertTab"); + } + else { + var spaces = Array(cm.options.tabSize + 1).join(" "); + cm.replaceSelection(spaces); + } +}; + +CodeMirror.commands.shiftTabAndUnindentMarkdownList = function (cm) { + var ranges = cm.listSelections(); + var pos = ranges[0].head; + var eolState = cm.getStateAfter(pos.line); + var inList = eolState.list !== false; + + if (inList) { + cm.execCommand("indentLess"); + return; + } + + if (cm.options.indentWithTabs) { + cm.execCommand("insertTab"); + } + else { + var spaces = Array(cm.options.tabSize + 1).join(" "); + cm.replaceSelection(spaces); + } +}; diff --git a/static/js/editor.js b/static/js/editor.js new file mode 100644 index 0000000..7804f4c --- /dev/null +++ b/static/js/editor.js @@ -0,0 +1,805 @@ +// uses functions in utils.js +var host = window.location.protocol + "//" + window.location.host; +var collection_id; +var latest_timestamp = -1; + +// "global" map variables +var map; +var location_picking = false; + +// layer variables +const media_markers_l = L.layerGroup([]); // media markers +const media_markers_ref = {}; +const text_markers = L.layerGroup([]); // polyline routes + +// collection info in JSON +const collection_media = {}; +const collection_notes = {}; +const collection_perms = {}; +const collection_info = {}; +const media_divs = {}; +const changes = { "name" : false, "info" : false, "timestamp" : false } + +// +var mode = 0; // 0 = media, 1 = notes, 2 = routes. the index of hte currently selected tab +var ascending = true; +const selected_media_ids = []; +var is_picking_location = false; +var last_selected_index = -1; +var media_modal_open = false; +var share_modal_open = false; +var collection_modal_open = false; + +var Media_Icon = L.Icon.extend({ + options: { + shadowUrl: 'static/img/marker_background.png', + iconSize: [60, 45], + shadowSize: [72, 62], + iconAnchor: [30, 55], + shadowAnchor: [36, 62], + popupAnchor: [-3, -76] + } +}); + +// RUN ON PAGE LOAD +window.onload = function() { + document.getElementById("share-modal").addEventListener("click", close_share_modal); + document.getElementById("share-box").addEventListener("click", stop_propagation); + document.getElementById("collection-modal").addEventListener("click", close_collection_modal); + document.getElementById("collection-box").addEventListener("click", stop_propagation); + + collection_id = document.getElementById("collection_id").innerText; + map = L.map('map').setView([13.6760758587756, -89.21535464697462], 10); + L.tileLayer('https://tiles.stadiamaps.com/tiles/outdoors/{z}/{x}/{y}{r}.{ext}?api_key=24746ff5-179d-482f-97a8-0267c5b276a1', { + minZoom: 0, + maxZoom: 20, + ext: 'png' + }).addTo(map); + + + map.addLayer(media_markers_l); + map.addLayer(text_markers); + var layerControl = L.control.layers(null,null,{collapsed:false}); + layerControl.addOverlay(media_markers_l, "Media"); + layerControl.addOverlay(text_markers, "Notes"); + layerControl.addTo(map); + + map.on('click', function(e) { + var coords = e.latlng; + handle_map_click(coords["lat"], coords["lng"]); + }); + + request_latest_info(); +} +var intervalId = window.setInterval(function(){ // constantly get latest info from server + request_latest_info(); + }, 5000); + + // keyboard event listener for ESC to clear selection or cancel location picking +document.addEventListener('keydown', function(e) { + if (e.key === 'Escape') { + if(media_modal_open) { + close_media_preview(); + return; + } + if(share_modal_open) { + close_share_modal(); + return; + } + if(collection_modal_open) { + close_collection_modal(); + return; + } + if (is_picking_location) { + disable_picking(); + return; + } + clear_selection(); + update_content_editing_box(); + } + if (e.key == 'c') { + if( e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA" ) return; + if( e.target.isContentEditable ) return; + if(selected_media_ids.length)picker_clicked(); + return; + } + // select elements by arrow keys + if (e.key == 'a' || e.key == 'd') { + if( e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA" ) return; + if(last_selected_index != -1) { + const new_index = last_selected_index + (e.key == 'a' ? -1 : 1); + const media = document.querySelector(`.media[data-index="${new_index}"]`); + if (media) { + clear_selection(); + + media.classList.add('selected'); + selected_media_ids.push(media.dataset.id); + last_selected_index = parseInt(media.dataset.index); + media.scrollIntoView(); + //map.panTo(media_markers_ref[media.dataset.id].latlng); + + update_content_editing_box(); + } + } + } + if (e.key == 'w' || e.key == 's') { + if( e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA" ) return; + if(last_selected_index != -1) { + const grid = document.getElementById("media_grid"); + const grid_computed_style = window.getComputedStyle(grid); + const grid_cols = grid_computed_style.getPropertyValue("grid-template-columns").split(" ").length; + + const new_index = last_selected_index + (e.key == 'w' ? -1 : 1)*grid_cols; + const media = document.querySelector(`.media[data-index="${new_index}"]`); + console.log(media); + if (media) { + clear_selection(); + + media.classList.add('selected'); + selected_media_ids.push(media.dataset.id); + last_selected_index = parseInt(media.dataset.index); + media.scrollIntoView(); + //map.panTo(media_markers_ref[media.dataset.id].latlng); + + update_content_editing_box(); + } + } + } +}); + +function stop_propagation(event) { + console.log("stop event propagation"); + event.stopPropagation(); +} + + +function set_tab(evt, tab_id) { + if(is_picking_location) return; + // Hide all tabs' content and mark all tabs as unselected + var tab_content = document.getElementsByClassName("tab-content"); + for (var i = 0; i < tab_content.length; i++) { + tab_content[i].style.display = "none"; + } + var tabs = document.getElementsByClassName("tab"); + for (var i = 0; i < tabs.length; i++) { + tabs[i].className = tabs[i].className.replace(" active", ""); + } + + // Display selected tab's content, show tab as selected + document.getElementById(tab_id).style.display = "flex"; + evt.currentTarget.className += " active"; + + mode = {"media_tab" : 0, "text_tab" : 1, "routes_tab" : 2}[tab_id]; + + update_content_editing_box(); +} + +function upload_media_files() { + var files = document.getElementById("media_upload").files;; + + if (files.length == 0) { + alert("No files selected!"); + return; + } + + const formData = new FormData(); + for (var i = 0; i < files.length; i++){ + console.log(files[i]); + formData.append("files", files[i]); + } + formData.append("collection_id", collection_id); + + fetch( "/editormediaupload", { + method: "POST", + credentials: "include", + body: formData + }).then((response) => response.json()) + .then((json) => { + console.log('Gotcha'); + }).catch((err) => { + console.log(err); + }); +} + +function edit_media_grid_element(media_dict) { + const media_div = media_divs[media_dict["id"]]; + const name = media_div.querySelector("p"); + name.innerText = media_dict["name"]; + + const icons = media_div.querySelectorAll("div.media div img"); + icons[0].src = media_dict["longitude"] ? "static/img/yes-location.png" : "static/img/no-location.png"; + icons[1].src = (media_dict["timestamp"] != "") ? "static/img/yes-datetime.png" : "static/img/no-datetime.png"; + + const timestamp = media_div.querySelector("p.media-timestamp"); + timestamp.innerText = media_dict["timestamp"].replace("T", " "); +} + +function add_media(media_dict){ + // add data + collection_media[media_dict["id"]] = media_dict; + + // add HTML element + const media_element = document.createElement('div'); + media_element.className = "media"; + media_element.dataset.id = media_dict["id"]; + + const img = document.createElement('img'); + img.src = media_dict["thumbbig"]; + + const fn = document.createElement('p'); + fn.innerText = media_dict["name"]; + + const info = document.createElement('div'); + info.style = "display:flex; flex-direction:horizontal; margin:0; justify-content: center;"; + const loc = document.createElement('img'); + loc.src = media_dict["longitude"] ? "static/img/yes-location.png" : "static/img/no-location.png"; + loc.style.width = "20px"; loc.style.height = "20px"; + const dt = document.createElement('img'); + dt.src = (media_dict["timestamp"] != "") ? "static/img/yes-datetime.png" : "static/img/no-datetime.png"; + dt.style.width = "20px"; dt.style.height = "20px"; + const dtext = document.createElement('p'); + dtext.classList.add("media-timestamp") + dtext.innerText = media_dict["timestamp"].replace("T", " "); + + + info.appendChild(loc); + info.appendChild(dt); + + media_element.appendChild(img); + media_element.appendChild(fn); + media_element.appendChild(dtext); + media_element.appendChild(info); + + + media_divs[media_dict["id"]] = media_element; + + // add map icon + console.log(media_dict); + const media_icon = new Media_Icon({iconUrl : media_dict['thumbsmall']}); + var lat = 0; var lng = 0; + if(media_dict['latitude'] && media_dict['longitude']) { + lat = media_dict['latitude']; + lng = media_dict['longitude']; + } + const media_marker = L.marker( [lat, lng], {icon:media_icon} ).on('click', function(e) { map_marker_clicked(media_dict["id"], 'media', e) }); + media_markers_ref[media_dict["id"]] = media_marker; + media_markers_l.addLayer(media_marker); +} + +function remove_media(media_id) { + for(var i = 0; i < selected_media_ids.length; i++) { + if (selected_media_ids[i] == media_id) { + selected_media_ids.pop(i); + break; + } + } + media_divs[media_id].remove(); + + media_markers_l.removeLayer(media_markers_ref[media_id]); + delete media_markers_ref[media_id]; + delete media_divs[media_id]; + delete collection_media[media_id]; + update_content_editing_box(); +} + +// media grid stuff made by deepseek, not finished cleaning up +document.addEventListener('DOMContentLoaded', function() { + const media_grid = document.getElementById('media_grid'); + + // Add click event listeners + media_grid.addEventListener('click', function(e) { + const media_element = e.target.closest('.media'); + const isShiftKey = e.shiftKey; + const isCtrlKey = e.ctrlKey || e.metaKey; // metaKey for Mac Command key + if (!media_element) { + if(!isShiftKey && !isCtrlKey) clear_selection(); + update_content_editing_box(); + return; + } + const isVisible = function (ele, container) { + const { bottom, height, top } = ele.getBoundingClientRect(); + const containerRect = container.getBoundingClientRect(); + + //console.log( ele.getBoundingClientRect()); + //console.log(containerRect); + + return top <= containerRect.top ? containerRect.top - top <= height : bottom - containerRect.bottom <= height; + }; + + + + const currentIndex = parseInt(media_element.dataset.index); + const current_media_id = media_element.dataset.id; + + if (isShiftKey && last_selected_index !== -1) { + // Range selection with Shift key + select_range(last_selected_index, currentIndex, isCtrlKey); + } else if (isCtrlKey) { + // Toggle selection with Ctrl key + media_element.classList.toggle('selected'); + selected_media_ids.push(current_media_id); + last_selected_index = currentIndex; + } else { + // Single selection + clear_selection(); + media_element.classList.add('selected'); + last_selected_index = currentIndex; + selected_media_ids.push(current_media_id); + } + + update_content_editing_box(); + if(!isVisible(media_element, media_element.parentElement))media_element.scrollIntoView(); + }); +}); + +// Clear all selections +function clear_selection() { + selected_media_ids.length = 0; // clear selected media ids array... kind of strange way to do it but appears to be the most readable way of doing it + document.querySelectorAll('.media.selected').forEach(el => { + el.classList.remove('selected'); + }); + last_selected_index = -1; +} + +// Select range of photos +function select_range(start_index, end_index, additive) { + if (!additive) { + clear_selection(); + } + + const start = Math.min(start_index, end_index); + const end = Math.max(start_index, end_index); + + for (let i = start; i <= end; i++) { + const photo = document.querySelector(`.media[data-index="${i}"]`); + if (photo) { + photo.classList.add('selected'); + selected_media_ids.push(photo.dataset.id); + } + } +} + +function update_media_grid() { + const media_grid = document.getElementById('media_grid'); + while (media_grid.firstChild) { + media_grid.removeChild(media_grid.lastChild); + } + const hide_timestamp = document.getElementById("hide-timestamp").checked; + const hide_location = document.getElementById("hide-location").checked; + const sort_by = document.getElementById("sort-type").value; + + const media_ids = [...Object.keys(media_divs)]; + if (sort_by != "none") media_ids.sort( (a, b) => { + if(ascending) { + return collection_media[a][sort_by].localeCompare(collection_media[b][sort_by]); + } else { + return collection_media[b][sort_by].localeCompare(collection_media[a][sort_by]); + } + }); + + // remove hidden entries + for (var i = media_ids.length-1; i >= 0; i-- ) { + if (hide_timestamp && (collection_media[media_ids[i]]["timestamp"] != "")) media_ids.splice(i, 1); + else if (hide_location && (collection_media[media_ids[i]]["latitude"] && collection_media[media_ids[i]]["longitude"])) media_ids.splice(i, 1); + } + + var i = 0; + for (var media_id of media_ids) { + const media_div = media_divs[media_id]; + media_div.dataset.index = i; + media_grid.appendChild(media_div); + i += 1; + } +} + +function update_content_editing_box() { + submit_edit_changes(); // make sure user changes are submitted + + const content_box = document.getElementById("content_info"); + const preview_box = document.getElementById("media-preview"); + const video = document.getElementById("preview-video"); + video.pause(); + if(mode == 0) { // media editing mode + if(selected_media_ids.length == 0){ + content_box.style.display="none"; + preview_box.style.display="none"; + return; + } + + content_box.style.display="block"; + preview_box.style.display="flex"; + + if(selected_media_ids.length == 1){ + const selected_media = collection_media[selected_media_ids[0]]; + for (const [key, value] of Object.entries(selected_media)) { + var field = document.getElementById("media_"+key); + if (field) field.value = value; + } + const location_text = document.getElementById("media_coords"); + if(selected_media['latitude'] && selected_media['longitude']) { + map.panTo( [selected_media['latitude'], selected_media['longitude']] ); + location_text.innerText = `(${selected_media['latitude']}, ${selected_media['longitude']})`; + } else { + location_text.innerText = "No location given! Press the picker button or 'C' to mark location."; + } + + if (selected_media["is_video"]) { + const img = document.getElementById("preview-img"); + video.src = selected_media["mediapath"]; + img.style.display = "none"; + video.style.display = "block"; + } else { + const img = document.getElementById("preview-img"); + img.src = selected_media["mediapath"]; + video.style.display = "none"; + img.style.display = "block"; + } + + } else { // multiple media selected + const fields = { ...collection_media[selected_media_ids[0]] }; + for (var m_i = 1; m_i < selected_media_ids.length; m_i++) { + const selected_media = collection_media[selected_media_ids[ m_i ]]; + for (const [key, value] of Object.entries(selected_media)) { + if( value != fields[key] ) fields[key] = "< ... >"; + } + } + + for (const [key, value] of Object.entries(fields)) { + var field = document.getElementById("media_"+key); + if (field) field.value = value; + } + const location_text = document.getElementById("media_coords"); + if(fields['latitude'] && fields['longitude'] && Number(fields['latitude']) && Number(fields['longitude'])) { + map.panTo( [fields['latitude'], fields['longitude']]); + location_text.innerText = `(${fields['latitude']}, ${fields['longitude']})`; + } else if ( !(fields['latitude'] && fields['longitude']) ) + location_text.innerText = "No location given! Press the picker button or 'C' to mark location."; + else { + location_text.innerText = "< ... >"; + } + } + } +} + +function picker_clicked() { + const picking_style = document.createElement('style'); + picking_style.innerHTML = '*{cursor: crosshair;}'; + picking_style.id = 'picking-style'; + document.head.appendChild(picking_style); + + L.DomUtil.addClass(map._container, "crosshair-cursor-enabled"); + + const media_boxes = document.getElementsByClassName("media"); + for(var i = 0; i < media_boxes.length; i++) { + media_boxes[i].classList.add('no-hover'); + } + const tabs = document.getElementsByClassName("tab-header"); + for(var i = 0; i < tabs.length; i++) { + tabs[i].classList.add('no-hover'); + } + + map.removeLayer(media_markers_l); + + is_picking_location = true; +} + +function disable_picking() { + is_picking_location = false; + document.getElementById('picking-style').remove(); + L.DomUtil.removeClass(map._container, "crosshair-cursor-enabled"); + + const media_boxes = document.getElementsByClassName("media"); + for(var i = 0; i < media_boxes.length; i++) { + media_boxes[i].classList.remove('no-hover'); + } + + const tabs = document.getElementsByClassName("tab-header"); + for(var i = 0; i < tabs.length; i++) { + tabs[i].classList.remove('no-hover'); + } + map.addLayer(media_markers_l); +} + +function handle_map_click(latitude, longitude) { + if(!is_picking_location) return; + map.addLayer(media_markers_l); + edit_media_info({"latitude" : latitude, "longitude" : longitude}); + disable_picking(); +} + +function map_marker_clicked(id, type, e) { + clear_selection(); + if (type === "media") { + const media = document.querySelector(`.media[data-id="${id}"]`); + if (media) { + media.classList.add('selected'); + selected_media_ids.push(media.dataset.id); + last_selected_index = parseInt(media.dataset.index); + media.scrollIntoView(); + } else { // media filtered + document.getElementById("hide-timestamp").checked = false; + document.getElementById("hide-location").checked = false; + update_media_grid(); + const media = document.querySelector(`.media[data-id="${id}"]`); + media.classList.add('selected'); + selected_media_ids.push(media.dataset.id); + last_selected_index = parseInt(media.dataset.index); + media.scrollIntoView(); + } + update_content_editing_box(); + } +} + +function open_media_preview() { + const modal = document.getElementById('media-preview-modal'); + const preview_img = document.getElementById("preview-img"); + const modal_media = document.getElementById("modal-img"); + + modal.style.display = "block"; + modal_media.src = preview_img.src; + media_modal_open = true; +} + +function close_media_preview() { + const modal = document.getElementById('media-preview-modal'); + modal.style.display = "none"; + media_modal_open = false; +} + +function open_share_modal() { + update_share_info(); + var modal = document.getElementById('share-modal'); + share_modal_open = true; + modal.style.display = "block"; +} + +function close_share_modal() { + var modal = document.getElementById('share-modal'); + share_modal_open = false; + modal.style.display = "none"; +} + +function update_share_info() { + const publicity = document.getElementById("visibility-dropdown"); + publicity.value = collection_perms["public"] ? "public" : "private"; + + const user_section = document.getElementById("shared-users"); + while (user_section.firstChild) + user_section.removeChild(user_section.firstChild); + + const editor_option = document.createElement("option"); + editor_option.innerText = "Editor"; + editor_option.value = "editor"; + const viewer_option = document.createElement("option"); + viewer_option.innerText = "Viewer"; + viewer_option.value = "viewer"; + + for (var t = 0; t < 2; t++) { + const user_type = t == 0 ? "editor" : "viewer"; + for (var i = 0; i < collection_perms[user_type+"s"].length; i++) { + const user_email_str = collection_perms[user_type+"s"][i]; + const user_email = document.createElement("p"); + user_email.innerText = collection_perms[user_type+"s"][i]; + user_email.style.margin = "0"; + + const user_permissions = document.createElement("select"); + const e = editor_option.cloneNode(true); + const v = viewer_option.cloneNode(true); + user_permissions.appendChild(e); + user_permissions.appendChild(v); + user_permissions.value = user_type; + user_permissions.onchange = () => { edit_permissions(user_email_str, user_permissions.value) } + + const user_unshare = document.createElement("p"); + user_unshare.innerText = "Unshare"; + user_unshare.classList.add("user-unshare-button"); + user_unshare.onclick = function () { edit_permissions(user_email_str, ""); } + + user_section.appendChild(user_email); + user_section.appendChild(user_permissions); + user_section.appendChild(user_unshare); + } + } +} + +function open_collection_modal() { + update_collection_info(); + var modal = document.getElementById('collection-modal'); + collection_modal_open = true; + modal.style.display = "block"; +} + +function close_collection_modal() { + var modal = document.getElementById('collection-modal'); + collection_modal_open = false; + modal.style.display = "none"; +} + +function update_collection_info() { + document.getElementById("collection-title-field").value = collection_info["title"]; + document.getElementById("collection-subtitle-field").value = collection_info["subtitle"]; + document.getElementById("collection-info-field").value = collection_info["info"]; +} + +function mark_info_change(field) { + changes[field] = true; +} + +function submit_edit_changes() { + console.log(changes); + var edits = {}; + for (const [field, was_changed] of Object.entries(changes)) { + if (was_changed) { + const new_val = document.getElementById("media_"+field).value; + if (new_val != collection_media[selected_media_ids[0]][field]) edits[field] = new_val; + } + changes[field] = false; // reset dictionary + } + if(Object.keys(edits).length > 0) { + edit_media_info(edits); + } +} + +// communicate +// with +// the +// server + +// run every x seconds to get latest info from server +function request_latest_info(){ + console.log("requesting update"); + function handleLatestInfo(json){ + console.log(json); + if(json['status'] != "OK") return; // ADD ERROR NOTIF ON MENU BAR + + if(!json['update']) return; // no changes + + let new_collection = json['collection']; + let new_media_ids = []; + for(var i = 0; i < new_collection['media'].length; i++) { + const media_id = new_collection['media'][i]['id']; + new_media_ids.push(media_id); + if(collection_media[media_id] == undefined) { + add_media(new_collection['media'][i]); + } else { // update existing + collection_media[media_id] = new_collection['media'][i] + edit_media_grid_element(collection_media[media_id]); + if(collection_media[media_id]['latitude'] && collection_media[media_id]['longitude']) + media_markers_ref[media_id].setLatLng([collection_media[media_id]['latitude'], collection_media[media_id]['longitude']]); + } + } + let to_be_removed = []; + for (const [key, _] of Object.entries(collection_media)) { + if (!new_media_ids.includes(key)) to_be_removed.push(key); + } + console.log(to_be_removed); + for (var i = 0; i < to_be_removed.length; i++){ + remove_media(to_be_removed[i]); + } + + // update permissions info + collection_perms["owner"] = new_collection["owner"]; + collection_perms["editors"] = new_collection["editors"]; + collection_perms["viewers"] = new_collection["viewers"]; + collection_perms["public"] = new_collection["public"]; + if(share_modal_open) update_share_info(); + + document.getElementById("collection-title").innerText = new_collection["title"]; + collection_info["title"] = new_collection["title"]; + collection_info["subtitle"] = new_collection["subtitle"]; + collection_info["info"] = new_collection["info"]; + if(collection_modal_open) update_share_info(); + + update_media_grid(); // make smarter later + update_content_editing_box(); + latest_timestamp = new_collection["last_edited"]; + + } + fetch(host+'/editorgetlatestinfo', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ "collection" : collection_id, "last_updated" : latest_timestamp })}) + .then((response) => response.json()) + .then((json) => handleLatestInfo(json)); +} + +function delete_media() { + if(!confirm("Are you sure you want to delete these pieces of media? This cannot be undone!")) return; + + fetch(host+'/editordeletemedia', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ "collection" : collection_id, "to_be_removed" : selected_media_ids })}) + .then((response) => response.json()) + .then((json) => { clear_selection(); request_latest_info(); }); +} + +function edit_media_info(changes) { + console.log("edit"); + const data = []; + for (var i = 0; i < selected_media_ids.length; i++) { + data.push({"media_id" : selected_media_ids[i], "changes" : changes}) + } + fetch(host+'/editoreditmedia', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ "collection" : collection_id, "edits" : data })}) + .then((response) => response.json()) + .then((json) => {console.log(json); request_latest_info();}); +} + +function update_visibility() { + const visibility = document.getElementById("visibility-dropdown").value == "public" ? true : false; + const data = { "collection" : collection_id, "public" : visibility }; + fetch(host+'/editorshare', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data)}) + .then((response) => response.json()) + .then((json) => {console.log(json); request_latest_info();}); +} + +function add_shared_user() { + const user_email_e = document.getElementById("share-email"); + const permission_e = document.getElementById("share-permissions"); + edit_permissions(user_email_e.value, permission_e.value); + + user_email_e.value = ""; // reset email +} + +function edit_permissions(user_email, permission) { + console.log(user_email); + console.log(permission); + const data = { "collection" : collection_id, "user_email" : user_email, "perm" : permission }; + + fetch(host+'/editorshare', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data)}) + .then((response) => response.json()) + .then((json) => {console.log(json); request_latest_info();}); +} + +function collection_info_updated(field) { + const field_input = document.getElementById('collection-'+field+'-field'); + if(field_input) { + edit_collection_info(field, field_input.value); + } +} + +function edit_collection_info(field, value) { + const edits = {}; + edits[field] = value; + const data = { "collection" : collection_id, "edits" : edits }; + + fetch(host+'/editorcollectioninfo', { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data)}) + .then((response) => response.json()) + .then((json) => {console.log(json); request_latest_info();}); +} + +function toggle_sort_direction(){ + ascending = !ascending; + document.getElementById("sort-direction").innerText = ascending ? "Ascending" : "Descending"; + update_media_grid(); +} \ No newline at end of file diff --git a/static/js/leaflet.js b/static/js/leaflet.js new file mode 100644 index 0000000..a3bf693 --- /dev/null +++ b/static/js/leaflet.js @@ -0,0 +1,6 @@ +/* @preserve + * Leaflet 1.9.4, a JS library for interactive maps. https://leafletjs.com + * (c) 2010-2023 Vladimir Agafonkin, (c) 2010-2011 CloudMade + */ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).leaflet={})}(this,function(t){"use strict";function l(t){for(var e,i,n=1,o=arguments.length;n=this.min.x&&i.x<=this.max.x&&e.y>=this.min.y&&i.y<=this.max.y},intersects:function(t){t=_(t);var e=this.min,i=this.max,n=t.min,t=t.max,o=t.x>=e.x&&n.x<=i.x,t=t.y>=e.y&&n.y<=i.y;return o&&t},overlaps:function(t){t=_(t);var e=this.min,i=this.max,n=t.min,t=t.max,o=t.x>e.x&&n.xe.y&&n.y=n.lat&&i.lat<=o.lat&&e.lng>=n.lng&&i.lng<=o.lng},intersects:function(t){t=g(t);var e=this._southWest,i=this._northEast,n=t.getSouthWest(),t=t.getNorthEast(),o=t.lat>=e.lat&&n.lat<=i.lat,t=t.lng>=e.lng&&n.lng<=i.lng;return o&&t},overlaps:function(t){t=g(t);var e=this._southWest,i=this._northEast,n=t.getSouthWest(),t=t.getNorthEast(),o=t.lat>e.lat&&n.late.lng&&n.lng","http://www.w3.org/2000/svg"===(Wt.firstChild&&Wt.firstChild.namespaceURI));function y(t){return 0<=navigator.userAgent.toLowerCase().indexOf(t)}var b={ie:pt,ielt9:mt,edge:n,webkit:ft,android:gt,android23:vt,androidStock:yt,opera:xt,chrome:wt,gecko:bt,safari:Pt,phantom:Lt,opera12:o,win:Tt,ie3d:Mt,webkit3d:zt,gecko3d:_t,any3d:Ct,mobile:Zt,mobileWebkit:St,mobileWebkit3d:Et,msPointer:kt,pointer:Ot,touch:Bt,touchNative:At,mobileOpera:It,mobileGecko:Rt,retina:Nt,passiveEvents:Dt,canvas:jt,svg:Ht,vml:!Ht&&function(){try{var t=document.createElement("div"),e=(t.innerHTML='',t.firstChild);return e.style.behavior="url(#default#VML)",e&&"object"==typeof e.adj}catch(t){return!1}}(),inlineSvg:Wt,mac:0===navigator.platform.indexOf("Mac"),linux:0===navigator.platform.indexOf("Linux")},Ft=b.msPointer?"MSPointerDown":"pointerdown",Ut=b.msPointer?"MSPointerMove":"pointermove",Vt=b.msPointer?"MSPointerUp":"pointerup",qt=b.msPointer?"MSPointerCancel":"pointercancel",Gt={touchstart:Ft,touchmove:Ut,touchend:Vt,touchcancel:qt},Kt={touchstart:function(t,e){e.MSPOINTER_TYPE_TOUCH&&e.pointerType===e.MSPOINTER_TYPE_TOUCH&&O(e);ee(t,e)},touchmove:ee,touchend:ee,touchcancel:ee},Yt={},Xt=!1;function Jt(t,e,i){return"touchstart"!==e||Xt||(document.addEventListener(Ft,$t,!0),document.addEventListener(Ut,Qt,!0),document.addEventListener(Vt,te,!0),document.addEventListener(qt,te,!0),Xt=!0),Kt[e]?(i=Kt[e].bind(this,i),t.addEventListener(Gt[e],i,!1),i):(console.warn("wrong event specified:",e),u)}function $t(t){Yt[t.pointerId]=t}function Qt(t){Yt[t.pointerId]&&(Yt[t.pointerId]=t)}function te(t){delete Yt[t.pointerId]}function ee(t,e){if(e.pointerType!==(e.MSPOINTER_TYPE_MOUSE||"mouse")){for(var i in e.touches=[],Yt)e.touches.push(Yt[i]);e.changedTouches=[e],t(e)}}var ie=200;function ne(t,i){t.addEventListener("dblclick",i);var n,o=0;function e(t){var e;1!==t.detail?n=t.detail:"mouse"===t.pointerType||t.sourceCapabilities&&!t.sourceCapabilities.firesTouchEvents||((e=Ne(t)).some(function(t){return t instanceof HTMLLabelElement&&t.attributes.for})&&!e.some(function(t){return t instanceof HTMLInputElement||t instanceof HTMLSelectElement})||((e=Date.now())-o<=ie?2===++n&&i(function(t){var e,i,n={};for(i in t)e=t[i],n[i]=e&&e.bind?e.bind(t):e;return(t=n).type="dblclick",n.detail=2,n.isTrusted=!1,n._simulated=!0,n}(t)):n=1,o=e))}return t.addEventListener("click",e),{dblclick:i,simDblclick:e}}var oe,se,re,ae,he,le,ue=we(["transform","webkitTransform","OTransform","MozTransform","msTransform"]),ce=we(["webkitTransition","transition","OTransition","MozTransition","msTransition"]),de="webkitTransition"===ce||"OTransition"===ce?ce+"End":"transitionend";function _e(t){return"string"==typeof t?document.getElementById(t):t}function pe(t,e){var i=t.style[e]||t.currentStyle&&t.currentStyle[e];return"auto"===(i=i&&"auto"!==i||!document.defaultView?i:(t=document.defaultView.getComputedStyle(t,null))?t[e]:null)?null:i}function P(t,e,i){t=document.createElement(t);return t.className=e||"",i&&i.appendChild(t),t}function T(t){var e=t.parentNode;e&&e.removeChild(t)}function me(t){for(;t.firstChild;)t.removeChild(t.firstChild)}function fe(t){var e=t.parentNode;e&&e.lastChild!==t&&e.appendChild(t)}function ge(t){var e=t.parentNode;e&&e.firstChild!==t&&e.insertBefore(t,e.firstChild)}function ve(t,e){return void 0!==t.classList?t.classList.contains(e):0<(t=xe(t)).length&&new RegExp("(^|\\s)"+e+"(\\s|$)").test(t)}function M(t,e){var i;if(void 0!==t.classList)for(var n=F(e),o=0,s=n.length;othis.options.maxZoom)?this.setZoom(t):this},panInsideBounds:function(t,e){this._enforcingBounds=!0;var i=this.getCenter(),t=this._limitCenter(i,this._zoom,g(t));return i.equals(t)||this.panTo(t,e),this._enforcingBounds=!1,this},panInside:function(t,e){var i=m((e=e||{}).paddingTopLeft||e.padding||[0,0]),n=m(e.paddingBottomRight||e.padding||[0,0]),o=this.project(this.getCenter()),t=this.project(t),s=this.getPixelBounds(),i=_([s.min.add(i),s.max.subtract(n)]),s=i.getSize();return i.contains(t)||(this._enforcingBounds=!0,n=t.subtract(i.getCenter()),i=i.extend(t).getSize().subtract(s),o.x+=n.x<0?-i.x:i.x,o.y+=n.y<0?-i.y:i.y,this.panTo(this.unproject(o),e),this._enforcingBounds=!1),this},invalidateSize:function(t){if(!this._loaded)return this;t=l({animate:!1,pan:!0},!0===t?{animate:!0}:t);var e=this.getSize(),i=(this._sizeChanged=!0,this._lastCenter=null,this.getSize()),n=e.divideBy(2).round(),o=i.divideBy(2).round(),n=n.subtract(o);return n.x||n.y?(t.animate&&t.pan?this.panBy(n):(t.pan&&this._rawPanBy(n),this.fire("move"),t.debounceMoveend?(clearTimeout(this._sizeTimer),this._sizeTimer=setTimeout(a(this.fire,this,"moveend"),200)):this.fire("moveend")),this.fire("resize",{oldSize:e,newSize:i})):this},stop:function(){return this.setZoom(this._limitZoom(this._zoom)),this.options.zoomSnap||this.fire("viewreset"),this._stop()},locate:function(t){var e,i;return t=this._locateOptions=l({timeout:1e4,watch:!1},t),"geolocation"in navigator?(e=a(this._handleGeolocationResponse,this),i=a(this._handleGeolocationError,this),t.watch?this._locationWatchId=navigator.geolocation.watchPosition(e,i,t):navigator.geolocation.getCurrentPosition(e,i,t)):this._handleGeolocationError({code:0,message:"Geolocation not supported."}),this},stopLocate:function(){return navigator.geolocation&&navigator.geolocation.clearWatch&&navigator.geolocation.clearWatch(this._locationWatchId),this._locateOptions&&(this._locateOptions.setView=!1),this},_handleGeolocationError:function(t){var e;this._container._leaflet_id&&(e=t.code,t=t.message||(1===e?"permission denied":2===e?"position unavailable":"timeout"),this._locateOptions.setView&&!this._loaded&&this.fitWorld(),this.fire("locationerror",{code:e,message:"Geolocation error: "+t+"."}))},_handleGeolocationResponse:function(t){if(this._container._leaflet_id){var e,i,n=new v(t.coords.latitude,t.coords.longitude),o=n.toBounds(2*t.coords.accuracy),s=this._locateOptions,r=(s.setView&&(e=this.getBoundsZoom(o),this.setView(n,s.maxZoom?Math.min(e,s.maxZoom):e)),{latlng:n,bounds:o,timestamp:t.timestamp});for(i in t.coords)"number"==typeof t.coords[i]&&(r[i]=t.coords[i]);this.fire("locationfound",r)}},addHandler:function(t,e){return e&&(e=this[t]=new e(this),this._handlers.push(e),this.options[t]&&e.enable()),this},remove:function(){if(this._initEvents(!0),this.options.maxBounds&&this.off("moveend",this._panInsideMaxBounds),this._containerId!==this._container._leaflet_id)throw new Error("Map container is being reused by another instance");try{delete this._container._leaflet_id,delete this._containerId}catch(t){this._container._leaflet_id=void 0,this._containerId=void 0}for(var t in void 0!==this._locationWatchId&&this.stopLocate(),this._stop(),T(this._mapPane),this._clearControlPos&&this._clearControlPos(),this._resizeRequest&&(r(this._resizeRequest),this._resizeRequest=null),this._clearHandlers(),this._loaded&&this.fire("unload"),this._layers)this._layers[t].remove();for(t in this._panes)T(this._panes[t]);return this._layers=[],this._panes=[],delete this._mapPane,delete this._renderer,this},createPane:function(t,e){e=P("div","leaflet-pane"+(t?" leaflet-"+t.replace("Pane","")+"-pane":""),e||this._mapPane);return t&&(this._panes[t]=e),e},getCenter:function(){return this._checkIfLoaded(),this._lastCenter&&!this._moved()?this._lastCenter.clone():this.layerPointToLatLng(this._getCenterLayerPoint())},getZoom:function(){return this._zoom},getBounds:function(){var t=this.getPixelBounds();return new s(this.unproject(t.getBottomLeft()),this.unproject(t.getTopRight()))},getMinZoom:function(){return void 0===this.options.minZoom?this._layersMinZoom||0:this.options.minZoom},getMaxZoom:function(){return void 0===this.options.maxZoom?void 0===this._layersMaxZoom?1/0:this._layersMaxZoom:this.options.maxZoom},getBoundsZoom:function(t,e,i){t=g(t),i=m(i||[0,0]);var n=this.getZoom()||0,o=this.getMinZoom(),s=this.getMaxZoom(),r=t.getNorthWest(),t=t.getSouthEast(),i=this.getSize().subtract(i),t=_(this.project(t,n),this.project(r,n)).getSize(),r=b.any3d?this.options.zoomSnap:1,a=i.x/t.x,i=i.y/t.y,t=e?Math.max(a,i):Math.min(a,i),n=this.getScaleZoom(t,n);return r&&(n=Math.round(n/(r/100))*(r/100),n=e?Math.ceil(n/r)*r:Math.floor(n/r)*r),Math.max(o,Math.min(s,n))},getSize:function(){return this._size&&!this._sizeChanged||(this._size=new p(this._container.clientWidth||0,this._container.clientHeight||0),this._sizeChanged=!1),this._size.clone()},getPixelBounds:function(t,e){t=this._getTopLeftPoint(t,e);return new f(t,t.add(this.getSize()))},getPixelOrigin:function(){return this._checkIfLoaded(),this._pixelOrigin},getPixelWorldBounds:function(t){return this.options.crs.getProjectedBounds(void 0===t?this.getZoom():t)},getPane:function(t){return"string"==typeof t?this._panes[t]:t},getPanes:function(){return this._panes},getContainer:function(){return this._container},getZoomScale:function(t,e){var i=this.options.crs;return e=void 0===e?this._zoom:e,i.scale(t)/i.scale(e)},getScaleZoom:function(t,e){var i=this.options.crs,t=(e=void 0===e?this._zoom:e,i.zoom(t*i.scale(e)));return isNaN(t)?1/0:t},project:function(t,e){return e=void 0===e?this._zoom:e,this.options.crs.latLngToPoint(w(t),e)},unproject:function(t,e){return e=void 0===e?this._zoom:e,this.options.crs.pointToLatLng(m(t),e)},layerPointToLatLng:function(t){t=m(t).add(this.getPixelOrigin());return this.unproject(t)},latLngToLayerPoint:function(t){return this.project(w(t))._round()._subtract(this.getPixelOrigin())},wrapLatLng:function(t){return this.options.crs.wrapLatLng(w(t))},wrapLatLngBounds:function(t){return this.options.crs.wrapLatLngBounds(g(t))},distance:function(t,e){return this.options.crs.distance(w(t),w(e))},containerPointToLayerPoint:function(t){return m(t).subtract(this._getMapPanePos())},layerPointToContainerPoint:function(t){return m(t).add(this._getMapPanePos())},containerPointToLatLng:function(t){t=this.containerPointToLayerPoint(m(t));return this.layerPointToLatLng(t)},latLngToContainerPoint:function(t){return this.layerPointToContainerPoint(this.latLngToLayerPoint(w(t)))},mouseEventToContainerPoint:function(t){return De(t,this._container)},mouseEventToLayerPoint:function(t){return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(t))},mouseEventToLatLng:function(t){return this.layerPointToLatLng(this.mouseEventToLayerPoint(t))},_initContainer:function(t){t=this._container=_e(t);if(!t)throw new Error("Map container not found.");if(t._leaflet_id)throw new Error("Map container is already initialized.");S(t,"scroll",this._onScroll,this),this._containerId=h(t)},_initLayout:function(){var t=this._container,e=(this._fadeAnimated=this.options.fadeAnimation&&b.any3d,M(t,"leaflet-container"+(b.touch?" leaflet-touch":"")+(b.retina?" leaflet-retina":"")+(b.ielt9?" leaflet-oldie":"")+(b.safari?" leaflet-safari":"")+(this._fadeAnimated?" leaflet-fade-anim":"")),pe(t,"position"));"absolute"!==e&&"relative"!==e&&"fixed"!==e&&"sticky"!==e&&(t.style.position="relative"),this._initPanes(),this._initControlPos&&this._initControlPos()},_initPanes:function(){var t=this._panes={};this._paneRenderers={},this._mapPane=this.createPane("mapPane",this._container),Z(this._mapPane,new p(0,0)),this.createPane("tilePane"),this.createPane("overlayPane"),this.createPane("shadowPane"),this.createPane("markerPane"),this.createPane("tooltipPane"),this.createPane("popupPane"),this.options.markerZoomAnimation||(M(t.markerPane,"leaflet-zoom-hide"),M(t.shadowPane,"leaflet-zoom-hide"))},_resetView:function(t,e,i){Z(this._mapPane,new p(0,0));var n=!this._loaded,o=(this._loaded=!0,e=this._limitZoom(e),this.fire("viewprereset"),this._zoom!==e);this._moveStart(o,i)._move(t,e)._moveEnd(o),this.fire("viewreset"),n&&this.fire("load")},_moveStart:function(t,e){return t&&this.fire("zoomstart"),e||this.fire("movestart"),this},_move:function(t,e,i,n){void 0===e&&(e=this._zoom);var o=this._zoom!==e;return this._zoom=e,this._lastCenter=t,this._pixelOrigin=this._getNewPixelOrigin(t),n?i&&i.pinch&&this.fire("zoom",i):((o||i&&i.pinch)&&this.fire("zoom",i),this.fire("move",i)),this},_moveEnd:function(t){return t&&this.fire("zoomend"),this.fire("moveend")},_stop:function(){return r(this._flyToFrame),this._panAnim&&this._panAnim.stop(),this},_rawPanBy:function(t){Z(this._mapPane,this._getMapPanePos().subtract(t))},_getZoomSpan:function(){return this.getMaxZoom()-this.getMinZoom()},_panInsideMaxBounds:function(){this._enforcingBounds||this.panInsideBounds(this.options.maxBounds)},_checkIfLoaded:function(){if(!this._loaded)throw new Error("Set map center and zoom first.")},_initEvents:function(t){this._targets={};var e=t?k:S;e((this._targets[h(this._container)]=this)._container,"click dblclick mousedown mouseup mouseover mouseout mousemove contextmenu keypress keydown keyup",this._handleDOMEvent,this),this.options.trackResize&&e(window,"resize",this._onResize,this),b.any3d&&this.options.transform3DLimit&&(t?this.off:this.on).call(this,"moveend",this._onMoveEnd)},_onResize:function(){r(this._resizeRequest),this._resizeRequest=x(function(){this.invalidateSize({debounceMoveend:!0})},this)},_onScroll:function(){this._container.scrollTop=0,this._container.scrollLeft=0},_onMoveEnd:function(){var t=this._getMapPanePos();Math.max(Math.abs(t.x),Math.abs(t.y))>=this.options.transform3DLimit&&this._resetView(this.getCenter(),this.getZoom())},_findEventTargets:function(t,e){for(var i,n=[],o="mouseout"===e||"mouseover"===e,s=t.target||t.srcElement,r=!1;s;){if((i=this._targets[h(s)])&&("click"===e||"preclick"===e)&&this._draggableMoved(i)){r=!0;break}if(i&&i.listens(e,!0)){if(o&&!We(s,t))break;if(n.push(i),o)break}if(s===this._container)break;s=s.parentNode}return n=n.length||r||o||!this.listens(e,!0)?n:[this]},_isClickDisabled:function(t){for(;t&&t!==this._container;){if(t._leaflet_disable_click)return!0;t=t.parentNode}},_handleDOMEvent:function(t){var e,i=t.target||t.srcElement;!this._loaded||i._leaflet_disable_events||"click"===t.type&&this._isClickDisabled(i)||("mousedown"===(e=t.type)&&Me(i),this._fireDOMEvent(t,e))},_mouseEvents:["click","dblclick","mouseover","mouseout","contextmenu"],_fireDOMEvent:function(t,e,i){"click"===t.type&&((a=l({},t)).type="preclick",this._fireDOMEvent(a,a.type,i));var n=this._findEventTargets(t,e);if(i){for(var o=[],s=0;sthis.options.zoomAnimationThreshold)return!1;var n=this.getZoomScale(e),n=this._getCenterOffset(t)._divideBy(1-1/n);if(!0!==i.animate&&!this.getSize().contains(n))return!1;x(function(){this._moveStart(!0,i.noMoveStart||!1)._animateZoom(t,e,!0)},this)}return!0},_animateZoom:function(t,e,i,n){this._mapPane&&(i&&(this._animatingZoom=!0,this._animateToCenter=t,this._animateToZoom=e,M(this._mapPane,"leaflet-zoom-anim")),this.fire("zoomanim",{center:t,zoom:e,noUpdate:n}),this._tempFireZoomEvent||(this._tempFireZoomEvent=this._zoom!==this._animateToZoom),this._move(this._animateToCenter,this._animateToZoom,void 0,!0),setTimeout(a(this._onZoomTransitionEnd,this),250))},_onZoomTransitionEnd:function(){this._animatingZoom&&(this._mapPane&&z(this._mapPane,"leaflet-zoom-anim"),this._animatingZoom=!1,this._move(this._animateToCenter,this._animateToZoom,void 0,!0),this._tempFireZoomEvent&&this.fire("zoom"),delete this._tempFireZoomEvent,this.fire("move"),this._moveEnd(!0))}});function Ue(t){return new B(t)}var B=et.extend({options:{position:"topright"},initialize:function(t){c(this,t)},getPosition:function(){return this.options.position},setPosition:function(t){var e=this._map;return e&&e.removeControl(this),this.options.position=t,e&&e.addControl(this),this},getContainer:function(){return this._container},addTo:function(t){this.remove(),this._map=t;var e=this._container=this.onAdd(t),i=this.getPosition(),t=t._controlCorners[i];return M(e,"leaflet-control"),-1!==i.indexOf("bottom")?t.insertBefore(e,t.firstChild):t.appendChild(e),this._map.on("unload",this.remove,this),this},remove:function(){return this._map&&(T(this._container),this.onRemove&&this.onRemove(this._map),this._map.off("unload",this.remove,this),this._map=null),this},_refocusOnMap:function(t){this._map&&t&&0",e=document.createElement("div");return e.innerHTML=t,e.firstChild},_addItem:function(t){var e,i=document.createElement("label"),n=this._map.hasLayer(t.layer),n=(t.overlay?((e=document.createElement("input")).type="checkbox",e.className="leaflet-control-layers-selector",e.defaultChecked=n):e=this._createRadioElement("leaflet-base-layers_"+h(this),n),this._layerControlInputs.push(e),e.layerId=h(t.layer),S(e,"click",this._onInputClick,this),document.createElement("span")),o=(n.innerHTML=" "+t.name,document.createElement("span"));return i.appendChild(o),o.appendChild(e),o.appendChild(n),(t.overlay?this._overlaysList:this._baseLayersList).appendChild(i),this._checkDisabledLayers(),i},_onInputClick:function(){if(!this._preventClick){var t,e,i=this._layerControlInputs,n=[],o=[];this._handlingClick=!0;for(var s=i.length-1;0<=s;s--)t=i[s],e=this._getLayer(t.layerId).layer,t.checked?n.push(e):t.checked||o.push(e);for(s=0;se.options.maxZoom},_expandIfNotCollapsed:function(){return this._map&&!this.options.collapsed&&this.expand(),this},_expandSafely:function(){var t=this._section,e=(this._preventClick=!0,S(t,"click",O),this.expand(),this);setTimeout(function(){k(t,"click",O),e._preventClick=!1})}})),qe=B.extend({options:{position:"topleft",zoomInText:'',zoomInTitle:"Zoom in",zoomOutText:'',zoomOutTitle:"Zoom out"},onAdd:function(t){var e="leaflet-control-zoom",i=P("div",e+" leaflet-bar"),n=this.options;return this._zoomInButton=this._createButton(n.zoomInText,n.zoomInTitle,e+"-in",i,this._zoomIn),this._zoomOutButton=this._createButton(n.zoomOutText,n.zoomOutTitle,e+"-out",i,this._zoomOut),this._updateDisabled(),t.on("zoomend zoomlevelschange",this._updateDisabled,this),i},onRemove:function(t){t.off("zoomend zoomlevelschange",this._updateDisabled,this)},disable:function(){return this._disabled=!0,this._updateDisabled(),this},enable:function(){return this._disabled=!1,this._updateDisabled(),this},_zoomIn:function(t){!this._disabled&&this._map._zoomthis._map.getMinZoom()&&this._map.zoomOut(this._map.options.zoomDelta*(t.shiftKey?3:1))},_createButton:function(t,e,i,n,o){i=P("a",i,n);return i.innerHTML=t,i.href="#",i.title=e,i.setAttribute("role","button"),i.setAttribute("aria-label",e),Ie(i),S(i,"click",Re),S(i,"click",o,this),S(i,"click",this._refocusOnMap,this),i},_updateDisabled:function(){var t=this._map,e="leaflet-disabled";z(this._zoomInButton,e),z(this._zoomOutButton,e),this._zoomInButton.setAttribute("aria-disabled","false"),this._zoomOutButton.setAttribute("aria-disabled","false"),!this._disabled&&t._zoom!==t.getMinZoom()||(M(this._zoomOutButton,e),this._zoomOutButton.setAttribute("aria-disabled","true")),!this._disabled&&t._zoom!==t.getMaxZoom()||(M(this._zoomInButton,e),this._zoomInButton.setAttribute("aria-disabled","true"))}}),Ge=(A.mergeOptions({zoomControl:!0}),A.addInitHook(function(){this.options.zoomControl&&(this.zoomControl=new qe,this.addControl(this.zoomControl))}),B.extend({options:{position:"bottomleft",maxWidth:100,metric:!0,imperial:!0},onAdd:function(t){var e="leaflet-control-scale",i=P("div",e),n=this.options;return this._addScales(n,e+"-line",i),t.on(n.updateWhenIdle?"moveend":"move",this._update,this),t.whenReady(this._update,this),i},onRemove:function(t){t.off(this.options.updateWhenIdle?"moveend":"move",this._update,this)},_addScales:function(t,e,i){t.metric&&(this._mScale=P("div",e,i)),t.imperial&&(this._iScale=P("div",e,i))},_update:function(){var t=this._map,e=t.getSize().y/2,t=t.distance(t.containerPointToLatLng([0,e]),t.containerPointToLatLng([this.options.maxWidth,e]));this._updateScales(t)},_updateScales:function(t){this.options.metric&&t&&this._updateMetric(t),this.options.imperial&&t&&this._updateImperial(t)},_updateMetric:function(t){var e=this._getRoundNum(t);this._updateScale(this._mScale,e<1e3?e+" m":e/1e3+" km",e/t)},_updateImperial:function(t){var e,i,t=3.2808399*t;5280'+(b.inlineSvg?' ':"")+"Leaflet"},initialize:function(t){c(this,t),this._attributions={}},onAdd:function(t){for(var e in(t.attributionControl=this)._container=P("div","leaflet-control-attribution"),Ie(this._container),t._layers)t._layers[e].getAttribution&&this.addAttribution(t._layers[e].getAttribution());return this._update(),t.on("layeradd",this._addAttribution,this),this._container},onRemove:function(t){t.off("layeradd",this._addAttribution,this)},_addAttribution:function(t){t.layer.getAttribution&&(this.addAttribution(t.layer.getAttribution()),t.layer.once("remove",function(){this.removeAttribution(t.layer.getAttribution())},this))},setPrefix:function(t){return this.options.prefix=t,this._update(),this},addAttribution:function(t){return t&&(this._attributions[t]||(this._attributions[t]=0),this._attributions[t]++,this._update()),this},removeAttribution:function(t){return t&&this._attributions[t]&&(this._attributions[t]--,this._update()),this},_update:function(){if(this._map){var t,e=[];for(t in this._attributions)this._attributions[t]&&e.push(t);var i=[];this.options.prefix&&i.push(this.options.prefix),e.length&&i.push(e.join(", ")),this._container.innerHTML=i.join(' ')}}}),n=(A.mergeOptions({attributionControl:!0}),A.addInitHook(function(){this.options.attributionControl&&(new Ke).addTo(this)}),B.Layers=Ve,B.Zoom=qe,B.Scale=Ge,B.Attribution=Ke,Ue.layers=function(t,e,i){return new Ve(t,e,i)},Ue.zoom=function(t){return new qe(t)},Ue.scale=function(t){return new Ge(t)},Ue.attribution=function(t){return new Ke(t)},et.extend({initialize:function(t){this._map=t},enable:function(){return this._enabled||(this._enabled=!0,this.addHooks()),this},disable:function(){return this._enabled&&(this._enabled=!1,this.removeHooks()),this},enabled:function(){return!!this._enabled}})),ft=(n.addTo=function(t,e){return t.addHandler(e,this),this},{Events:e}),Ye=b.touch?"touchstart mousedown":"mousedown",Xe=it.extend({options:{clickTolerance:3},initialize:function(t,e,i,n){c(this,n),this._element=t,this._dragStartTarget=e||t,this._preventOutline=i},enable:function(){this._enabled||(S(this._dragStartTarget,Ye,this._onDown,this),this._enabled=!0)},disable:function(){this._enabled&&(Xe._dragging===this&&this.finishDrag(!0),k(this._dragStartTarget,Ye,this._onDown,this),this._enabled=!1,this._moved=!1)},_onDown:function(t){var e,i;this._enabled&&(this._moved=!1,ve(this._element,"leaflet-zoom-anim")||(t.touches&&1!==t.touches.length?Xe._dragging===this&&this.finishDrag():Xe._dragging||t.shiftKey||1!==t.which&&1!==t.button&&!t.touches||((Xe._dragging=this)._preventOutline&&Me(this._element),Le(),re(),this._moving||(this.fire("down"),i=t.touches?t.touches[0]:t,e=Ce(this._element),this._startPoint=new p(i.clientX,i.clientY),this._startPos=Pe(this._element),this._parentScale=Ze(e),i="mousedown"===t.type,S(document,i?"mousemove":"touchmove",this._onMove,this),S(document,i?"mouseup":"touchend touchcancel",this._onUp,this)))))},_onMove:function(t){var e;this._enabled&&(t.touches&&1e&&(i.push(t[n]),o=n);oe.max.x&&(i|=2),t.ye.max.y&&(i|=8),i}function ri(t,e,i,n){var o=e.x,e=e.y,s=i.x-o,r=i.y-e,a=s*s+r*r;return 0this._layersMaxZoom&&this.setZoom(this._layersMaxZoom),void 0===this.options.minZoom&&this._layersMinZoom&&this.getZoom()t.y!=n.y>t.y&&t.x<(n.x-i.x)*(t.y-i.y)/(n.y-i.y)+i.x&&(l=!l);return l||yi.prototype._containsPoint.call(this,t,!0)}});var wi=ci.extend({initialize:function(t,e){c(this,e),this._layers={},t&&this.addData(t)},addData:function(t){var e,i,n,o=d(t)?t:t.features;if(o){for(e=0,i=o.length;es.x&&(r=i.x+a-s.x+o.x),i.x-r-n.x<(a=0)&&(r=i.x-n.x),i.y+e+o.y>s.y&&(a=i.y+e-s.y+o.y),i.y-a-n.y<0&&(a=i.y-n.y),(r||a)&&(this.options.keepInView&&(this._autopanning=!0),t.fire("autopanstart").panBy([r,a]))))},_getAnchor:function(){return m(this._source&&this._source._getPopupAnchor?this._source._getPopupAnchor():[0,0])}})),Ii=(A.mergeOptions({closePopupOnClick:!0}),A.include({openPopup:function(t,e,i){return this._initOverlay(Bi,t,e,i).openOn(this),this},closePopup:function(t){return(t=arguments.length?t:this._popup)&&t.close(),this}}),o.include({bindPopup:function(t,e){return this._popup=this._initOverlay(Bi,this._popup,t,e),this._popupHandlersAdded||(this.on({click:this._openPopup,keypress:this._onKeyPress,remove:this.closePopup,move:this._movePopup}),this._popupHandlersAdded=!0),this},unbindPopup:function(){return this._popup&&(this.off({click:this._openPopup,keypress:this._onKeyPress,remove:this.closePopup,move:this._movePopup}),this._popupHandlersAdded=!1,this._popup=null),this},openPopup:function(t){return this._popup&&(this instanceof ci||(this._popup._source=this),this._popup._prepareOpen(t||this._latlng)&&this._popup.openOn(this._map)),this},closePopup:function(){return this._popup&&this._popup.close(),this},togglePopup:function(){return this._popup&&this._popup.toggle(this),this},isPopupOpen:function(){return!!this._popup&&this._popup.isOpen()},setPopupContent:function(t){return this._popup&&this._popup.setContent(t),this},getPopup:function(){return this._popup},_openPopup:function(t){var e;this._popup&&this._map&&(Re(t),e=t.layer||t.target,this._popup._source!==e||e instanceof fi?(this._popup._source=e,this.openPopup(t.latlng)):this._map.hasLayer(this._popup)?this.closePopup():this.openPopup(t.latlng))},_movePopup:function(t){this._popup.setLatLng(t.latlng)},_onKeyPress:function(t){13===t.originalEvent.keyCode&&this._openPopup(t)}}),Ai.extend({options:{pane:"tooltipPane",offset:[0,0],direction:"auto",permanent:!1,sticky:!1,opacity:.9},onAdd:function(t){Ai.prototype.onAdd.call(this,t),this.setOpacity(this.options.opacity),t.fire("tooltipopen",{tooltip:this}),this._source&&(this.addEventParent(this._source),this._source.fire("tooltipopen",{tooltip:this},!0))},onRemove:function(t){Ai.prototype.onRemove.call(this,t),t.fire("tooltipclose",{tooltip:this}),this._source&&(this.removeEventParent(this._source),this._source.fire("tooltipclose",{tooltip:this},!0))},getEvents:function(){var t=Ai.prototype.getEvents.call(this);return this.options.permanent||(t.preclick=this.close),t},_initLayout:function(){var t="leaflet-tooltip "+(this.options.className||"")+" leaflet-zoom-"+(this._zoomAnimated?"animated":"hide");this._contentNode=this._container=P("div",t),this._container.setAttribute("role","tooltip"),this._container.setAttribute("id","leaflet-tooltip-"+h(this))},_updateLayout:function(){},_adjustPan:function(){},_setPosition:function(t){var e,i=this._map,n=this._container,o=i.latLngToContainerPoint(i.getCenter()),i=i.layerPointToContainerPoint(t),s=this.options.direction,r=n.offsetWidth,a=n.offsetHeight,h=m(this.options.offset),l=this._getAnchor(),i="top"===s?(e=r/2,a):"bottom"===s?(e=r/2,0):(e="center"===s?r/2:"right"===s?0:"left"===s?r:i.xthis.options.maxZoom||nthis.options.maxZoom||void 0!==this.options.minZoom&&oi.max.x)||!e.wrapLat&&(t.yi.max.y))return!1}return!this.options.bounds||(e=this._tileCoordsToBounds(t),g(this.options.bounds).overlaps(e))},_keyToBounds:function(t){return this._tileCoordsToBounds(this._keyToTileCoords(t))},_tileCoordsToNwSe:function(t){var e=this._map,i=this.getTileSize(),n=t.scaleBy(i),i=n.add(i);return[e.unproject(n,t.z),e.unproject(i,t.z)]},_tileCoordsToBounds:function(t){t=this._tileCoordsToNwSe(t),t=new s(t[0],t[1]);return t=this.options.noWrap?t:this._map.wrapLatLngBounds(t)},_tileCoordsToKey:function(t){return t.x+":"+t.y+":"+t.z},_keyToTileCoords:function(t){var t=t.split(":"),e=new p(+t[0],+t[1]);return e.z=+t[2],e},_removeTile:function(t){var e=this._tiles[t];e&&(T(e.el),delete this._tiles[t],this.fire("tileunload",{tile:e.el,coords:this._keyToTileCoords(t)}))},_initTile:function(t){M(t,"leaflet-tile");var e=this.getTileSize();t.style.width=e.x+"px",t.style.height=e.y+"px",t.onselectstart=u,t.onmousemove=u,b.ielt9&&this.options.opacity<1&&C(t,this.options.opacity)},_addTile:function(t,e){var i=this._getTilePos(t),n=this._tileCoordsToKey(t),o=this.createTile(this._wrapCoords(t),a(this._tileReady,this,t));this._initTile(o),this.createTile.length<2&&x(a(this._tileReady,this,t,null,o)),Z(o,i),this._tiles[n]={el:o,coords:t,current:!0},e.appendChild(o),this.fire("tileloadstart",{tile:o,coords:t})},_tileReady:function(t,e,i){e&&this.fire("tileerror",{error:e,tile:i,coords:t});var n=this._tileCoordsToKey(t);(i=this._tiles[n])&&(i.loaded=+new Date,this._map._fadeAnimated?(C(i.el,0),r(this._fadeFrame),this._fadeFrame=x(this._updateOpacity,this)):(i.active=!0,this._pruneTiles()),e||(M(i.el,"leaflet-tile-loaded"),this.fire("tileload",{tile:i.el,coords:t})),this._noTilesToLoad()&&(this._loading=!1,this.fire("load"),b.ielt9||!this._map._fadeAnimated?x(this._pruneTiles,this):setTimeout(a(this._pruneTiles,this),250)))},_getTilePos:function(t){return t.scaleBy(this.getTileSize()).subtract(this._level.origin)},_wrapCoords:function(t){var e=new p(this._wrapX?H(t.x,this._wrapX):t.x,this._wrapY?H(t.y,this._wrapY):t.y);return e.z=t.z,e},_pxBoundsToTileRange:function(t){var e=this.getTileSize();return new f(t.min.unscaleBy(e).floor(),t.max.unscaleBy(e).ceil().subtract([1,1]))},_noTilesToLoad:function(){for(var t in this._tiles)if(!this._tiles[t].loaded)return!1;return!0}});var Di=Ni.extend({options:{minZoom:0,maxZoom:18,subdomains:"abc",errorTileUrl:"",zoomOffset:0,tms:!1,zoomReverse:!1,detectRetina:!1,crossOrigin:!1,referrerPolicy:!1},initialize:function(t,e){this._url=t,(e=c(this,e)).detectRetina&&b.retina&&0')}}catch(t){}return function(t){return document.createElement("<"+t+' xmlns="urn:schemas-microsoft.com:vml" class="lvml">')}}(),zt={_initContainer:function(){this._container=P("div","leaflet-vml-container")},_update:function(){this._map._animatingZoom||(Wi.prototype._update.call(this),this.fire("update"))},_initPath:function(t){var e=t._container=Vi("shape");M(e,"leaflet-vml-shape "+(this.options.className||"")),e.coordsize="1 1",t._path=Vi("path"),e.appendChild(t._path),this._updateStyle(t),this._layers[h(t)]=t},_addPath:function(t){var e=t._container;this._container.appendChild(e),t.options.interactive&&t.addInteractiveTarget(e)},_removePath:function(t){var e=t._container;T(e),t.removeInteractiveTarget(e),delete this._layers[h(t)]},_updateStyle:function(t){var e=t._stroke,i=t._fill,n=t.options,o=t._container;o.stroked=!!n.stroke,o.filled=!!n.fill,n.stroke?(e=e||(t._stroke=Vi("stroke")),o.appendChild(e),e.weight=n.weight+"px",e.color=n.color,e.opacity=n.opacity,n.dashArray?e.dashStyle=d(n.dashArray)?n.dashArray.join(" "):n.dashArray.replace(/( *, *)/g," "):e.dashStyle="",e.endcap=n.lineCap.replace("butt","flat"),e.joinstyle=n.lineJoin):e&&(o.removeChild(e),t._stroke=null),n.fill?(i=i||(t._fill=Vi("fill")),o.appendChild(i),i.color=n.fillColor||n.color,i.opacity=n.fillOpacity):i&&(o.removeChild(i),t._fill=null)},_updateCircle:function(t){var e=t._point.round(),i=Math.round(t._radius),n=Math.round(t._radiusY||i);this._setPath(t,t._empty()?"M0 0":"AL "+e.x+","+e.y+" "+i+","+n+" 0,23592600")},_setPath:function(t,e){t._path.v=e},_bringToFront:function(t){fe(t._container)},_bringToBack:function(t){ge(t._container)}},qi=b.vml?Vi:ct,Gi=Wi.extend({_initContainer:function(){this._container=qi("svg"),this._container.setAttribute("pointer-events","none"),this._rootGroup=qi("g"),this._container.appendChild(this._rootGroup)},_destroyContainer:function(){T(this._container),k(this._container),delete this._container,delete this._rootGroup,delete this._svgSize},_update:function(){var t,e,i;this._map._animatingZoom&&this._bounds||(Wi.prototype._update.call(this),e=(t=this._bounds).getSize(),i=this._container,this._svgSize&&this._svgSize.equals(e)||(this._svgSize=e,i.setAttribute("width",e.x),i.setAttribute("height",e.y)),Z(i,t.min),i.setAttribute("viewBox",[t.min.x,t.min.y,e.x,e.y].join(" ")),this.fire("update"))},_initPath:function(t){var e=t._path=qi("path");t.options.className&&M(e,t.options.className),t.options.interactive&&M(e,"leaflet-interactive"),this._updateStyle(t),this._layers[h(t)]=t},_addPath:function(t){this._rootGroup||this._initContainer(),this._rootGroup.appendChild(t._path),t.addInteractiveTarget(t._path)},_removePath:function(t){T(t._path),t.removeInteractiveTarget(t._path),delete this._layers[h(t)]},_updatePath:function(t){t._project(),t._update()},_updateStyle:function(t){var e=t._path,t=t.options;e&&(t.stroke?(e.setAttribute("stroke",t.color),e.setAttribute("stroke-opacity",t.opacity),e.setAttribute("stroke-width",t.weight),e.setAttribute("stroke-linecap",t.lineCap),e.setAttribute("stroke-linejoin",t.lineJoin),t.dashArray?e.setAttribute("stroke-dasharray",t.dashArray):e.removeAttribute("stroke-dasharray"),t.dashOffset?e.setAttribute("stroke-dashoffset",t.dashOffset):e.removeAttribute("stroke-dashoffset")):e.setAttribute("stroke","none"),t.fill?(e.setAttribute("fill",t.fillColor||t.color),e.setAttribute("fill-opacity",t.fillOpacity),e.setAttribute("fill-rule",t.fillRule||"evenodd")):e.setAttribute("fill","none"))},_updatePoly:function(t,e){this._setPath(t,dt(t._parts,e))},_updateCircle:function(t){var e=t._point,i=Math.max(Math.round(t._radius),1),n="a"+i+","+(Math.max(Math.round(t._radiusY),1)||i)+" 0 1,0 ",e=t._empty()?"M0 0":"M"+(e.x-i)+","+e.y+n+2*i+",0 "+n+2*-i+",0 ";this._setPath(t,e)},_setPath:function(t,e){t._path.setAttribute("d",e)},_bringToFront:function(t){fe(t._path)},_bringToBack:function(t){ge(t._path)}});function Ki(t){return b.svg||b.vml?new Gi(t):null}b.vml&&Gi.include(zt),A.include({getRenderer:function(t){t=(t=t.options.renderer||this._getPaneRenderer(t.options.pane)||this.options.renderer||this._renderer)||(this._renderer=this._createRenderer());return this.hasLayer(t)||this.addLayer(t),t},_getPaneRenderer:function(t){var e;return"overlayPane"!==t&&void 0!==t&&(void 0===(e=this._paneRenderers[t])&&(e=this._createRenderer({pane:t}),this._paneRenderers[t]=e),e)},_createRenderer:function(t){return this.options.preferCanvas&&Ui(t)||Ki(t)}});var Yi=xi.extend({initialize:function(t,e){xi.prototype.initialize.call(this,this._boundsToLatLngs(t),e)},setBounds:function(t){return this.setLatLngs(this._boundsToLatLngs(t))},_boundsToLatLngs:function(t){return[(t=g(t)).getSouthWest(),t.getNorthWest(),t.getNorthEast(),t.getSouthEast()]}});Gi.create=qi,Gi.pointsToPath=dt,wi.geometryToLayer=bi,wi.coordsToLatLng=Li,wi.coordsToLatLngs=Ti,wi.latLngToCoords=Mi,wi.latLngsToCoords=zi,wi.getFeature=Ci,wi.asFeature=Zi,A.mergeOptions({boxZoom:!0});var _t=n.extend({initialize:function(t){this._map=t,this._container=t._container,this._pane=t._panes.overlayPane,this._resetStateTimeout=0,t.on("unload",this._destroy,this)},addHooks:function(){S(this._container,"mousedown",this._onMouseDown,this)},removeHooks:function(){k(this._container,"mousedown",this._onMouseDown,this)},moved:function(){return this._moved},_destroy:function(){T(this._pane),delete this._pane},_resetState:function(){this._resetStateTimeout=0,this._moved=!1},_clearDeferredResetState:function(){0!==this._resetStateTimeout&&(clearTimeout(this._resetStateTimeout),this._resetStateTimeout=0)},_onMouseDown:function(t){if(!t.shiftKey||1!==t.which&&1!==t.button)return!1;this._clearDeferredResetState(),this._resetState(),re(),Le(),this._startPoint=this._map.mouseEventToContainerPoint(t),S(document,{contextmenu:Re,mousemove:this._onMouseMove,mouseup:this._onMouseUp,keydown:this._onKeyDown},this)},_onMouseMove:function(t){this._moved||(this._moved=!0,this._box=P("div","leaflet-zoom-box",this._container),M(this._container,"leaflet-crosshair"),this._map.fire("boxzoomstart")),this._point=this._map.mouseEventToContainerPoint(t);var t=new f(this._point,this._startPoint),e=t.getSize();Z(this._box,t.min),this._box.style.width=e.x+"px",this._box.style.height=e.y+"px"},_finish:function(){this._moved&&(T(this._box),z(this._container,"leaflet-crosshair")),ae(),Te(),k(document,{contextmenu:Re,mousemove:this._onMouseMove,mouseup:this._onMouseUp,keydown:this._onKeyDown},this)},_onMouseUp:function(t){1!==t.which&&1!==t.button||(this._finish(),this._moved&&(this._clearDeferredResetState(),this._resetStateTimeout=setTimeout(a(this._resetState,this),0),t=new s(this._map.containerPointToLatLng(this._startPoint),this._map.containerPointToLatLng(this._point)),this._map.fitBounds(t).fire("boxzoomend",{boxZoomBounds:t})))},_onKeyDown:function(t){27===t.keyCode&&(this._finish(),this._clearDeferredResetState(),this._resetState())}}),Ct=(A.addInitHook("addHandler","boxZoom",_t),A.mergeOptions({doubleClickZoom:!0}),n.extend({addHooks:function(){this._map.on("dblclick",this._onDoubleClick,this)},removeHooks:function(){this._map.off("dblclick",this._onDoubleClick,this)},_onDoubleClick:function(t){var e=this._map,i=e.getZoom(),n=e.options.zoomDelta,i=t.originalEvent.shiftKey?i-n:i+n;"center"===e.options.doubleClickZoom?e.setZoom(i):e.setZoomAround(t.containerPoint,i)}})),Zt=(A.addInitHook("addHandler","doubleClickZoom",Ct),A.mergeOptions({dragging:!0,inertia:!0,inertiaDeceleration:3400,inertiaMaxSpeed:1/0,easeLinearity:.2,worldCopyJump:!1,maxBoundsViscosity:0}),n.extend({addHooks:function(){var t;this._draggable||(t=this._map,this._draggable=new Xe(t._mapPane,t._container),this._draggable.on({dragstart:this._onDragStart,drag:this._onDrag,dragend:this._onDragEnd},this),this._draggable.on("predrag",this._onPreDragLimit,this),t.options.worldCopyJump&&(this._draggable.on("predrag",this._onPreDragWrap,this),t.on("zoomend",this._onZoomEnd,this),t.whenReady(this._onZoomEnd,this))),M(this._map._container,"leaflet-grab leaflet-touch-drag"),this._draggable.enable(),this._positions=[],this._times=[]},removeHooks:function(){z(this._map._container,"leaflet-grab"),z(this._map._container,"leaflet-touch-drag"),this._draggable.disable()},moved:function(){return this._draggable&&this._draggable._moved},moving:function(){return this._draggable&&this._draggable._moving},_onDragStart:function(){var t,e=this._map;e._stop(),this._map.options.maxBounds&&this._map.options.maxBoundsViscosity?(t=g(this._map.options.maxBounds),this._offsetLimit=_(this._map.latLngToContainerPoint(t.getNorthWest()).multiplyBy(-1),this._map.latLngToContainerPoint(t.getSouthEast()).multiplyBy(-1).add(this._map.getSize())),this._viscosity=Math.min(1,Math.max(0,this._map.options.maxBoundsViscosity))):this._offsetLimit=null,e.fire("movestart").fire("dragstart"),e.options.inertia&&(this._positions=[],this._times=[])},_onDrag:function(t){var e,i;this._map.options.inertia&&(e=this._lastTime=+new Date,i=this._lastPos=this._draggable._absPos||this._draggable._newPos,this._positions.push(i),this._times.push(e),this._prunePositions(e)),this._map.fire("move",t).fire("drag",t)},_prunePositions:function(t){for(;1e.max.x&&(t.x=this._viscousLimit(t.x,e.max.x)),t.y>e.max.y&&(t.y=this._viscousLimit(t.y,e.max.y)),this._draggable._newPos=this._draggable._startPos.add(t))},_onPreDragWrap:function(){var t=this._worldWidth,e=Math.round(t/2),i=this._initialWorldOffset,n=this._draggable._newPos.x,o=(n-e+i)%t+e-i,n=(n+e+i)%t-e-i,t=Math.abs(o+i)e.getMaxZoom()&&1= 0; block_start--) { + line = cm.getLineHandle(block_start); + if(fencing_line(line)) { + break; + } + } + var fencedTok = cm.getTokenAt({ + line: block_start, + ch: 1 + }); + var fence_chars = token_state(fencedTok).fencedChars; + var start_text, start_line; + var end_text, end_line; + // check for selection going up against fenced lines, in which case we don't want to add more fencing + if(fencing_line(cm.getLineHandle(cur_start.line))) { + start_text = ""; + start_line = cur_start.line; + } else if(fencing_line(cm.getLineHandle(cur_start.line - 1))) { + start_text = ""; + start_line = cur_start.line - 1; + } else { + start_text = fence_chars + "\n"; + start_line = cur_start.line; + } + if(fencing_line(cm.getLineHandle(cur_end.line))) { + end_text = ""; + end_line = cur_end.line; + if(cur_end.ch === 0) { + end_line += 1; + } + } else if(cur_end.ch !== 0 && fencing_line(cm.getLineHandle(cur_end.line + 1))) { + end_text = ""; + end_line = cur_end.line + 1; + } else { + end_text = fence_chars + "\n"; + end_line = cur_end.line + 1; + } + if(cur_end.ch === 0) { + // full last line selected, putting cursor at beginning of next + end_line -= 1; + } + cm.operation(function() { + // end line first, so that line numbers don't change + cm.replaceRange(end_text, { + line: end_line, + ch: 0 + }, { + line: end_line + (end_text ? 0 : 1), + ch: 0 + }); + cm.replaceRange(start_text, { + line: start_line, + ch: 0 + }, { + line: start_line + (start_text ? 0 : 1), + ch: 0 + }); + }); + cm.setSelection({ + line: start_line + (start_text ? 1 : 0), + ch: 0 + }, { + line: end_line + (start_text ? 1 : -1), + ch: 0 + }); + cm.focus(); + } else { + // no selection, search for ends of this fenced block + var search_from = cur_start.line; + if(fencing_line(cm.getLineHandle(cur_start.line))) { // gets a little tricky if cursor is right on a fenced line + if(code_type(cm, cur_start.line + 1) === "fenced") { + block_start = cur_start.line; + search_from = cur_start.line + 1; // for searching for "end" + } else { + block_end = cur_start.line; + search_from = cur_start.line - 1; // for searching for "start" + } + } + if(block_start === undefined) { + for(block_start = search_from; block_start >= 0; block_start--) { + line = cm.getLineHandle(block_start); + if(fencing_line(line)) { + break; + } + } + } + if(block_end === undefined) { + lineCount = cm.lineCount(); + for(block_end = search_from; block_end < lineCount; block_end++) { + line = cm.getLineHandle(block_end); + if(fencing_line(line)) { + break; + } + } + } + cm.operation(function() { + cm.replaceRange("", { + line: block_start, + ch: 0 + }, { + line: block_start + 1, + ch: 0 + }); + cm.replaceRange("", { + line: block_end - 1, + ch: 0 + }, { + line: block_end, + ch: 0 + }); + }); + cm.focus(); + } + } else if(is_code === "indented") { + if(cur_start.line !== cur_end.line || cur_start.ch !== cur_end.ch) { + // use selection + block_start = cur_start.line; + block_end = cur_end.line; + if(cur_end.ch === 0) { + block_end--; + } + } else { + // no selection, search for ends of this indented block + for(block_start = cur_start.line; block_start >= 0; block_start--) { + line = cm.getLineHandle(block_start); + if(line.text.match(/^\s*$/)) { + // empty or all whitespace - keep going + continue; + } else { + if(code_type(cm, block_start, line) !== "indented") { + block_start += 1; + break; + } + } + } + lineCount = cm.lineCount(); + for(block_end = cur_start.line; block_end < lineCount; block_end++) { + line = cm.getLineHandle(block_end); + if(line.text.match(/^\s*$/)) { + // empty or all whitespace - keep going + continue; + } else { + if(code_type(cm, block_end, line) !== "indented") { + block_end -= 1; + break; + } + } + } + } + // if we are going to un-indent based on a selected set of lines, and the next line is indented too, we need to + // insert a blank line so that the next line(s) continue to be indented code + var next_line = cm.getLineHandle(block_end + 1), + next_line_last_tok = next_line && cm.getTokenAt({ + line: block_end + 1, + ch: next_line.text.length - 1 + }), + next_line_indented = next_line_last_tok && token_state(next_line_last_tok).indentedCode; + if(next_line_indented) { + cm.replaceRange("\n", { + line: block_end + 1, + ch: 0 + }); + } + + for(var i = block_start; i <= block_end; i++) { + cm.indentLine(i, "subtract"); // TODO: this doesn't get tracked in the history, so can't be undone :( + } + cm.focus(); + } else { + // insert code formatting + var no_sel_and_starting_of_line = (cur_start.line === cur_end.line && cur_start.ch === cur_end.ch && cur_start.ch === 0); + var sel_multi = cur_start.line !== cur_end.line; + if(no_sel_and_starting_of_line || sel_multi) { + insertFencingAtSelection(cm, cur_start, cur_end, fenceCharsToInsert); + } else { + _replaceSelection(cm, false, ["`", "`"]); + } + } +} + +/** + * Action for toggling blockquote. + */ +function toggleBlockquote(editor) { + var cm = editor.codemirror; + _toggleLine(cm, "quote"); +} + +/** + * Action for toggling heading size: normal -> h1 -> h2 -> h3 -> h4 -> h5 -> h6 -> normal + */ +function toggleHeadingSmaller(editor) { + var cm = editor.codemirror; + _toggleHeading(cm, "smaller"); +} + +/** + * Action for toggling heading size: normal -> h6 -> h5 -> h4 -> h3 -> h2 -> h1 -> normal + */ +function toggleHeadingBigger(editor) { + var cm = editor.codemirror; + _toggleHeading(cm, "bigger"); +} + +/** + * Action for toggling heading size 1 + */ +function toggleHeading1(editor) { + var cm = editor.codemirror; + _toggleHeading(cm, undefined, 1); +} + +/** + * Action for toggling heading size 2 + */ +function toggleHeading2(editor) { + var cm = editor.codemirror; + _toggleHeading(cm, undefined, 2); +} + +/** + * Action for toggling heading size 3 + */ +function toggleHeading3(editor) { + var cm = editor.codemirror; + _toggleHeading(cm, undefined, 3); +} + + +/** + * Action for toggling ul. + */ +function toggleUnorderedList(editor) { + var cm = editor.codemirror; + _toggleLine(cm, "unordered-list"); +} + + +/** + * Action for toggling ol. + */ +function toggleOrderedList(editor) { + var cm = editor.codemirror; + _toggleLine(cm, "ordered-list"); +} + +/** + * Action for clean block (remove headline, list, blockquote code, markers) + */ +function cleanBlock(editor) { + var cm = editor.codemirror; + _cleanBlock(cm); +} + +/** + * Action for drawing a link. + */ +function drawLink(editor) { + var cm = editor.codemirror; + var stat = getState(cm); + var options = editor.options; + var url = "http://"; + if(options.promptURLs) { + url = prompt(options.promptTexts.link); + if(!url) { + return false; + } + } + _replaceSelection(cm, stat.link, options.insertTexts.link, url); +} + +/** + * Action for drawing an img. + */ +function drawImage(editor) { + var cm = editor.codemirror; + var stat = getState(cm); + var options = editor.options; + var url = "http://"; + if(options.promptURLs) { + url = prompt(options.promptTexts.image); + if(!url) { + return false; + } + } + _replaceSelection(cm, stat.image, options.insertTexts.image, url); +} + +/** + * Action for drawing a table. + */ +function drawTable(editor) { + var cm = editor.codemirror; + var stat = getState(cm); + var options = editor.options; + _replaceSelection(cm, stat.table, options.insertTexts.table); +} + +/** + * Action for drawing a horizontal rule. + */ +function drawHorizontalRule(editor) { + var cm = editor.codemirror; + var stat = getState(cm); + var options = editor.options; + _replaceSelection(cm, stat.image, options.insertTexts.horizontalRule); +} + + +/** + * Undo action. + */ +function undo(editor) { + var cm = editor.codemirror; + cm.undo(); + cm.focus(); +} + + +/** + * Redo action. + */ +function redo(editor) { + var cm = editor.codemirror; + cm.redo(); + cm.focus(); +} + + +/** + * Toggle side by side preview + */ +function toggleSideBySide(editor) { + var cm = editor.codemirror; + var wrapper = cm.getWrapperElement(); + var preview = wrapper.nextSibling; + var toolbarButton = editor.toolbarElements["side-by-side"]; + var useSideBySideListener = false; + if(/editor-preview-active-side/.test(preview.className)) { + preview.className = preview.className.replace( + /\s*editor-preview-active-side\s*/g, "" + ); + toolbarButton.className = toolbarButton.className.replace(/\s*active\s*/g, ""); + wrapper.className = wrapper.className.replace(/\s*CodeMirror-sided\s*/g, " "); + } else { + // When the preview button is clicked for the first time, + // give some time for the transition from editor.css to fire and the view to slide from right to left, + // instead of just appearing. + setTimeout(function() { + if(!cm.getOption("fullScreen")) + toggleFullScreen(editor); + preview.className += " editor-preview-active-side"; + }, 1); + toolbarButton.className += " active"; + wrapper.className += " CodeMirror-sided"; + useSideBySideListener = true; + } + + // Hide normal preview if active + var previewNormal = wrapper.lastChild; + if(/editor-preview-active/.test(previewNormal.className)) { + previewNormal.className = previewNormal.className.replace( + /\s*editor-preview-active\s*/g, "" + ); + var toolbar = editor.toolbarElements.preview; + var toolbar_div = wrapper.previousSibling; + toolbar.className = toolbar.className.replace(/\s*active\s*/g, ""); + toolbar_div.className = toolbar_div.className.replace(/\s*disabled-for-preview*/g, ""); + } + + var sideBySideRenderingFunction = function() { + preview.innerHTML = editor.options.previewRender(editor.value(), preview); + }; + + if(!cm.sideBySideRenderingFunction) { + cm.sideBySideRenderingFunction = sideBySideRenderingFunction; + } + + if(useSideBySideListener) { + preview.innerHTML = editor.options.previewRender(editor.value(), preview); + cm.on("update", cm.sideBySideRenderingFunction); + } else { + cm.off("update", cm.sideBySideRenderingFunction); + } + + // Refresh to fix selection being off (#309) + cm.refresh(); +} + + +/** + * Preview action. + */ +function togglePreview(editor) { + var cm = editor.codemirror; + var wrapper = cm.getWrapperElement(); + var toolbar_div = wrapper.previousSibling; + var toolbar = editor.options.toolbar ? editor.toolbarElements.preview : false; + var preview = wrapper.lastChild; + if(!preview || !/editor-preview/.test(preview.className)) { + preview = document.createElement("div"); + preview.className = "editor-preview"; + wrapper.appendChild(preview); + } + if(/editor-preview-active/.test(preview.className)) { + preview.className = preview.className.replace( + /\s*editor-preview-active\s*/g, "" + ); + if(toolbar) { + toolbar.className = toolbar.className.replace(/\s*active\s*/g, ""); + toolbar_div.className = toolbar_div.className.replace(/\s*disabled-for-preview*/g, ""); + } + } else { + // When the preview button is clicked for the first time, + // give some time for the transition from editor.css to fire and the view to slide from right to left, + // instead of just appearing. + setTimeout(function() { + preview.className += " editor-preview-active"; + }, 1); + if(toolbar) { + toolbar.className += " active"; + toolbar_div.className += " disabled-for-preview"; + } + } + preview.innerHTML = editor.options.previewRender(editor.value(), preview); + + // Turn off side by side if needed + var sidebyside = cm.getWrapperElement().nextSibling; + if(/editor-preview-active-side/.test(sidebyside.className)) + toggleSideBySide(editor); +} + +function _replaceSelection(cm, active, startEnd, url) { + if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) + return; + + var text; + var start = startEnd[0]; + var end = startEnd[1]; + var startPoint = cm.getCursor("start"); + var endPoint = cm.getCursor("end"); + if(url) { + end = end.replace("#url#", url); + } + if(active) { + text = cm.getLine(startPoint.line); + start = text.slice(0, startPoint.ch); + end = text.slice(startPoint.ch); + cm.replaceRange(start + end, { + line: startPoint.line, + ch: 0 + }); + } else { + text = cm.getSelection(); + cm.replaceSelection(start + text + end); + + startPoint.ch += start.length; + if(startPoint !== endPoint) { + endPoint.ch += start.length; + } + } + cm.setSelection(startPoint, endPoint); + cm.focus(); +} + + +function _toggleHeading(cm, direction, size) { + if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) + return; + + var startPoint = cm.getCursor("start"); + var endPoint = cm.getCursor("end"); + for(var i = startPoint.line; i <= endPoint.line; i++) { + (function(i) { + var text = cm.getLine(i); + var currHeadingLevel = text.search(/[^#]/); + + if(direction !== undefined) { + if(currHeadingLevel <= 0) { + if(direction == "bigger") { + text = "###### " + text; + } else { + text = "# " + text; + } + } else if(currHeadingLevel == 6 && direction == "smaller") { + text = text.substr(7); + } else if(currHeadingLevel == 1 && direction == "bigger") { + text = text.substr(2); + } else { + if(direction == "bigger") { + text = text.substr(1); + } else { + text = "#" + text; + } + } + } else { + if(size == 1) { + if(currHeadingLevel <= 0) { + text = "# " + text; + } else if(currHeadingLevel == size) { + text = text.substr(currHeadingLevel + 1); + } else { + text = "# " + text.substr(currHeadingLevel + 1); + } + } else if(size == 2) { + if(currHeadingLevel <= 0) { + text = "## " + text; + } else if(currHeadingLevel == size) { + text = text.substr(currHeadingLevel + 1); + } else { + text = "## " + text.substr(currHeadingLevel + 1); + } + } else { + if(currHeadingLevel <= 0) { + text = "### " + text; + } else if(currHeadingLevel == size) { + text = text.substr(currHeadingLevel + 1); + } else { + text = "### " + text.substr(currHeadingLevel + 1); + } + } + } + + cm.replaceRange(text, { + line: i, + ch: 0 + }, { + line: i, + ch: 99999999999999 + }); + })(i); + } + cm.focus(); +} + + +function _toggleLine(cm, name) { + if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) + return; + + var stat = getState(cm); + var startPoint = cm.getCursor("start"); + var endPoint = cm.getCursor("end"); + var repl = { + "quote": /^(\s*)\>\s+/, + "unordered-list": /^(\s*)(\*|\-|\+)\s+/, + "ordered-list": /^(\s*)\d+\.\s+/ + }; + var map = { + "quote": "> ", + "unordered-list": "* ", + "ordered-list": "1. " + }; + for(var i = startPoint.line; i <= endPoint.line; i++) { + (function(i) { + var text = cm.getLine(i); + if(stat[name]) { + text = text.replace(repl[name], "$1"); + } else { + text = map[name] + text; + } + cm.replaceRange(text, { + line: i, + ch: 0 + }, { + line: i, + ch: 99999999999999 + }); + })(i); + } + cm.focus(); +} + +function _toggleBlock(editor, type, start_chars, end_chars) { + if(/editor-preview-active/.test(editor.codemirror.getWrapperElement().lastChild.className)) + return; + + end_chars = (typeof end_chars === "undefined") ? start_chars : end_chars; + var cm = editor.codemirror; + var stat = getState(cm); + + var text; + var start = start_chars; + var end = end_chars; + + var startPoint = cm.getCursor("start"); + var endPoint = cm.getCursor("end"); + + if(stat[type]) { + text = cm.getLine(startPoint.line); + start = text.slice(0, startPoint.ch); + end = text.slice(startPoint.ch); + if(type == "bold") { + start = start.replace(/(\*\*|__)(?![\s\S]*(\*\*|__))/, ""); + end = end.replace(/(\*\*|__)/, ""); + } else if(type == "italic") { + start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, ""); + end = end.replace(/(\*|_)/, ""); + } else if(type == "strikethrough") { + start = start.replace(/(\*\*|~~)(?![\s\S]*(\*\*|~~))/, ""); + end = end.replace(/(\*\*|~~)/, ""); + } + cm.replaceRange(start + end, { + line: startPoint.line, + ch: 0 + }, { + line: startPoint.line, + ch: 99999999999999 + }); + + if(type == "bold" || type == "strikethrough") { + startPoint.ch -= 2; + if(startPoint !== endPoint) { + endPoint.ch -= 2; + } + } else if(type == "italic") { + startPoint.ch -= 1; + if(startPoint !== endPoint) { + endPoint.ch -= 1; + } + } + } else { + text = cm.getSelection(); + if(type == "bold") { + text = text.split("**").join(""); + text = text.split("__").join(""); + } else if(type == "italic") { + text = text.split("*").join(""); + text = text.split("_").join(""); + } else if(type == "strikethrough") { + text = text.split("~~").join(""); + } + cm.replaceSelection(start + text + end); + + startPoint.ch += start_chars.length; + endPoint.ch = startPoint.ch + text.length; + } + + cm.setSelection(startPoint, endPoint); + cm.focus(); +} + +function _cleanBlock(cm) { + if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) + return; + + var startPoint = cm.getCursor("start"); + var endPoint = cm.getCursor("end"); + var text; + + for(var line = startPoint.line; line <= endPoint.line; line++) { + text = cm.getLine(line); + text = text.replace(/^[ ]*([# ]+|\*|\-|[> ]+|[0-9]+(.|\)))[ ]*/, ""); + + cm.replaceRange(text, { + line: line, + ch: 0 + }, { + line: line, + ch: 99999999999999 + }); + } +} + +// Merge the properties of one object into another. +function _mergeProperties(target, source) { + for(var property in source) { + if(source.hasOwnProperty(property)) { + if(source[property] instanceof Array) { + target[property] = source[property].concat(target[property] instanceof Array ? target[property] : []); + } else if( + source[property] !== null && + typeof source[property] === "object" && + source[property].constructor === Object + ) { + target[property] = _mergeProperties(target[property] || {}, source[property]); + } else { + target[property] = source[property]; + } + } + } + + return target; +} + +// Merge an arbitrary number of objects into one. +function extend(target) { + for(var i = 1; i < arguments.length; i++) { + target = _mergeProperties(target, arguments[i]); + } + + return target; +} + +/* The right word count in respect for CJK. */ +function wordCount(data) { + var pattern = /[a-zA-Z0-9_\u0392-\u03c9\u0410-\u04F9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g; + var m = data.match(pattern); + var count = 0; + if(m === null) return count; + for(var i = 0; i < m.length; i++) { + if(m[i].charCodeAt(0) >= 0x4E00) { + count += m[i].length; + } else { + count += 1; + } + } + return count; +} + +var toolbarBuiltInButtons = { + "bold": { + name: "bold", + action: toggleBold, + className: "fa fa-bold", + title: "Bold", + default: true + }, + "italic": { + name: "italic", + action: toggleItalic, + className: "fa fa-italic", + title: "Italic", + default: true + }, + "strikethrough": { + name: "strikethrough", + action: toggleStrikethrough, + className: "fa fa-strikethrough", + title: "Strikethrough" + }, + "heading": { + name: "heading", + action: toggleHeadingSmaller, + className: "fa fa-header", + title: "Heading", + default: true + }, + "heading-smaller": { + name: "heading-smaller", + action: toggleHeadingSmaller, + className: "fa fa-header fa-header-x fa-header-smaller", + title: "Smaller Heading" + }, + "heading-bigger": { + name: "heading-bigger", + action: toggleHeadingBigger, + className: "fa fa-header fa-header-x fa-header-bigger", + title: "Bigger Heading" + }, + "heading-1": { + name: "heading-1", + action: toggleHeading1, + className: "fa fa-header fa-header-x fa-header-1", + title: "Big Heading" + }, + "heading-2": { + name: "heading-2", + action: toggleHeading2, + className: "fa fa-header fa-header-x fa-header-2", + title: "Medium Heading" + }, + "heading-3": { + name: "heading-3", + action: toggleHeading3, + className: "fa fa-header fa-header-x fa-header-3", + title: "Small Heading" + }, + "separator-1": { + name: "separator-1" + }, + "code": { + name: "code", + action: toggleCodeBlock, + className: "fa fa-code", + title: "Code" + }, + "quote": { + name: "quote", + action: toggleBlockquote, + className: "fa fa-quote-left", + title: "Quote", + default: true + }, + "unordered-list": { + name: "unordered-list", + action: toggleUnorderedList, + className: "fa fa-list-ul", + title: "Generic List", + default: true + }, + "ordered-list": { + name: "ordered-list", + action: toggleOrderedList, + className: "fa fa-list-ol", + title: "Numbered List", + default: true + }, + "clean-block": { + name: "clean-block", + action: cleanBlock, + className: "fa fa-eraser fa-clean-block", + title: "Clean block" + }, + "separator-2": { + name: "separator-2" + }, + "link": { + name: "link", + action: drawLink, + className: "fa fa-link", + title: "Create Link", + default: true + }, + "image": { + name: "image", + action: drawImage, + className: "fa fa-picture-o", + title: "Insert Image", + default: true + }, + "table": { + name: "table", + action: drawTable, + className: "fa fa-table", + title: "Insert Table" + }, + "horizontal-rule": { + name: "horizontal-rule", + action: drawHorizontalRule, + className: "fa fa-minus", + title: "Insert Horizontal Line" + }, + "separator-3": { + name: "separator-3" + }, + "preview": { + name: "preview", + action: togglePreview, + className: "fa fa-eye no-disable", + title: "Toggle Preview", + default: true + }, + "side-by-side": { + name: "side-by-side", + action: toggleSideBySide, + className: "fa fa-columns no-disable no-mobile", + title: "Toggle Side by Side", + default: true + }, + "fullscreen": { + name: "fullscreen", + action: toggleFullScreen, + className: "fa fa-arrows-alt no-disable no-mobile", + title: "Toggle Fullscreen", + default: true + }, + "separator-4": { + name: "separator-4" + }, + "guide": { + name: "guide", + action: "https://simplemde.com/markdown-guide", + className: "fa fa-question-circle", + title: "Markdown Guide", + default: true + }, + "separator-5": { + name: "separator-5" + }, + "undo": { + name: "undo", + action: undo, + className: "fa fa-undo no-disable", + title: "Undo" + }, + "redo": { + name: "redo", + action: redo, + className: "fa fa-repeat no-disable", + title: "Redo" + } +}; + +var insertTexts = { + link: ["[", "](#url#)"], + image: ["![](", "#url#)"], + table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"], + horizontalRule: ["", "\n\n-----\n\n"] +}; + +var promptTexts = { + link: "URL for the link:", + image: "URL of the image:" +}; + +var blockStyles = { + "bold": "**", + "code": "```", + "italic": "*" +}; + +/** + * Interface of SimpleMDE. + */ +function SimpleMDE(options) { + // Handle options parameter + options = options || {}; + + + // Used later to refer to it"s parent + options.parent = this; + + + // Check if Font Awesome needs to be auto downloaded + var autoDownloadFA = true; + + if(options.autoDownloadFontAwesome === false) { + autoDownloadFA = false; + } + + if(options.autoDownloadFontAwesome !== true) { + var styleSheets = document.styleSheets; + for(var i = 0; i < styleSheets.length; i++) { + if(!styleSheets[i].href) + continue; + + if(styleSheets[i].href.indexOf("//maxcdn.bootstrapcdn.com/font-awesome/") > -1) { + autoDownloadFA = false; + } + } + } + + if(autoDownloadFA) { + var link = document.createElement("link"); + link.rel = "stylesheet"; + link.href = "https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css"; + document.getElementsByTagName("head")[0].appendChild(link); + } + + + // Find the textarea to use + if(options.element) { + this.element = options.element; + } else if(options.element === null) { + // This means that the element option was specified, but no element was found + console.log("SimpleMDE: Error. No element was found."); + return; + } + + + // Handle toolbar + if(options.toolbar === undefined) { + // Initialize + options.toolbar = []; + + + // Loop over the built in buttons, to get the preferred order + for(var key in toolbarBuiltInButtons) { + if(toolbarBuiltInButtons.hasOwnProperty(key)) { + if(key.indexOf("separator-") != -1) { + options.toolbar.push("|"); + } + + if(toolbarBuiltInButtons[key].default === true || (options.showIcons && options.showIcons.constructor === Array && options.showIcons.indexOf(key) != -1)) { + options.toolbar.push(key); + } + } + } + } + + + // Handle status bar + if(!options.hasOwnProperty("status")) { + options.status = ["autosave", "lines", "words", "cursor"]; + } + + + // Add default preview rendering function + if(!options.previewRender) { + options.previewRender = function(plainText) { + // Note: "this" refers to the options object + return this.parent.markdown(plainText); + }; + } + + + // Set default options for parsing config + options.parsingConfig = extend({ + highlightFormatting: true // needed for toggleCodeBlock to detect types of code + }, options.parsingConfig || {}); + + + // Merging the insertTexts, with the given options + options.insertTexts = extend({}, insertTexts, options.insertTexts || {}); + + + // Merging the promptTexts, with the given options + options.promptTexts = promptTexts; + + + // Merging the blockStyles, with the given options + options.blockStyles = extend({}, blockStyles, options.blockStyles || {}); + + + // Merging the shortcuts, with the given options + options.shortcuts = extend({}, shortcuts, options.shortcuts || {}); + + + // Change unique_id to uniqueId for backwards compatibility + if(options.autosave != undefined && options.autosave.unique_id != undefined && options.autosave.unique_id != "") + options.autosave.uniqueId = options.autosave.unique_id; + + + // Update this options + this.options = options; + + + // Auto render + this.render(); + + + // The codemirror component is only available after rendering + // so, the setter for the initialValue can only run after + // the element has been rendered + if(options.initialValue && (!this.options.autosave || this.options.autosave.foundSavedValue !== true)) { + this.value(options.initialValue); + } +} + +/** + * Default markdown render. + */ +SimpleMDE.prototype.markdown = function(text) { + if(marked) { + // Initialize + var markedOptions = {}; + + + // Update options + if(this.options && this.options.renderingConfig && this.options.renderingConfig.singleLineBreaks === false) { + markedOptions.breaks = false; + } else { + markedOptions.breaks = true; + } + + if(this.options && this.options.renderingConfig && this.options.renderingConfig.codeSyntaxHighlighting === true && window.hljs) { + markedOptions.highlight = function(code) { + return window.hljs.highlightAuto(code).value; + }; + } + + + // Set options + marked.setOptions(markedOptions); + + + // Return + return marked(text); + } +}; + +/** + * Render editor to the given element. + */ +SimpleMDE.prototype.render = function(el) { + if(!el) { + el = this.element || document.getElementsByTagName("textarea")[0]; + } + + if(this._rendered && this._rendered === el) { + // Already rendered. + return; + } + + this.element = el; + var options = this.options; + + var self = this; + var keyMaps = {}; + + for(var key in options.shortcuts) { + // null stands for "do not bind this command" + if(options.shortcuts[key] !== null && bindings[key] !== null) { + (function(key) { + keyMaps[fixShortcut(options.shortcuts[key])] = function() { + bindings[key](self); + }; + })(key); + } + } + + keyMaps["Enter"] = "newlineAndIndentContinueMarkdownList"; + keyMaps["Tab"] = "tabAndIndentMarkdownList"; + keyMaps["Shift-Tab"] = "shiftTabAndUnindentMarkdownList"; + keyMaps["Esc"] = function(cm) { + if(cm.getOption("fullScreen")) toggleFullScreen(self); + }; + + document.addEventListener("keydown", function(e) { + e = e || window.event; + + if(e.keyCode == 27) { + if(self.codemirror.getOption("fullScreen")) toggleFullScreen(self); + } + }, false); + + var mode, backdrop; + if(options.spellChecker !== false) { + mode = "spell-checker"; + backdrop = options.parsingConfig; + backdrop.name = "gfm"; + backdrop.gitHubSpice = false; + + CodeMirrorSpellChecker({ + codeMirrorInstance: CodeMirror + }); + } else { + mode = options.parsingConfig; + mode.name = "gfm"; + mode.gitHubSpice = false; + } + + this.codemirror = CodeMirror.fromTextArea(el, { + mode: mode, + backdrop: backdrop, + theme: "paper", + tabSize: (options.tabSize != undefined) ? options.tabSize : 2, + indentUnit: (options.tabSize != undefined) ? options.tabSize : 2, + indentWithTabs: (options.indentWithTabs === false) ? false : true, + lineNumbers: false, + autofocus: (options.autofocus === true) ? true : false, + extraKeys: keyMaps, + lineWrapping: (options.lineWrapping === false) ? false : true, + allowDropFileTypes: ["text/plain"], + placeholder: options.placeholder || el.getAttribute("placeholder") || "", + styleSelectedText: (options.styleSelectedText != undefined) ? options.styleSelectedText : true + }); + + if(options.forceSync === true) { + var cm = this.codemirror; + cm.on("change", function() { + cm.save(); + }); + } + + this.gui = {}; + + if(options.toolbar !== false) { + this.gui.toolbar = this.createToolbar(); + } + if(options.status !== false) { + this.gui.statusbar = this.createStatusbar(); + } + if(options.autosave != undefined && options.autosave.enabled === true) { + this.autosave(); + } + + this.gui.sideBySide = this.createSideBySide(); + + this._rendered = this.element; + + + // Fixes CodeMirror bug (#344) + var temp_cm = this.codemirror; + setTimeout(function() { + temp_cm.refresh(); + }.bind(temp_cm), 0); +}; + +// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem throw QuotaExceededError. We're going to detect this and set a variable accordingly. +function isLocalStorageAvailable() { + if(typeof localStorage === "object") { + try { + localStorage.setItem("smde_localStorage", 1); + localStorage.removeItem("smde_localStorage"); + } catch(e) { + return false; + } + } else { + return false; + } + + return true; +} + +SimpleMDE.prototype.autosave = function() { + if(isLocalStorageAvailable()) { + var simplemde = this; + + if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") { + console.log("SimpleMDE: You must set a uniqueId to use the autosave feature"); + return; + } + + if(simplemde.element.form != null && simplemde.element.form != undefined) { + simplemde.element.form.addEventListener("submit", function() { + localStorage.removeItem("smde_" + simplemde.options.autosave.uniqueId); + }); + } + + if(this.options.autosave.loaded !== true) { + if(typeof localStorage.getItem("smde_" + this.options.autosave.uniqueId) == "string" && localStorage.getItem("smde_" + this.options.autosave.uniqueId) != "") { + this.codemirror.setValue(localStorage.getItem("smde_" + this.options.autosave.uniqueId)); + this.options.autosave.foundSavedValue = true; + } + + this.options.autosave.loaded = true; + } + + localStorage.setItem("smde_" + this.options.autosave.uniqueId, simplemde.value()); + + var el = document.getElementById("autosaved"); + if(el != null && el != undefined && el != "") { + var d = new Date(); + var hh = d.getHours(); + var m = d.getMinutes(); + var dd = "am"; + var h = hh; + if(h >= 12) { + h = hh - 12; + dd = "pm"; + } + if(h == 0) { + h = 12; + } + m = m < 10 ? "0" + m : m; + + el.innerHTML = "Autosaved: " + h + ":" + m + " " + dd; + } + + this.autosaveTimeoutId = setTimeout(function() { + simplemde.autosave(); + }, this.options.autosave.delay || 10000); + } else { + console.log("SimpleMDE: localStorage not available, cannot autosave"); + } +}; + +SimpleMDE.prototype.clearAutosavedValue = function() { + if(isLocalStorageAvailable()) { + if(this.options.autosave == undefined || this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") { + console.log("SimpleMDE: You must set a uniqueId to clear the autosave value"); + return; + } + + localStorage.removeItem("smde_" + this.options.autosave.uniqueId); + } else { + console.log("SimpleMDE: localStorage not available, cannot autosave"); + } +}; + +SimpleMDE.prototype.createSideBySide = function() { + var cm = this.codemirror; + var wrapper = cm.getWrapperElement(); + var preview = wrapper.nextSibling; + + if(!preview || !/editor-preview-side/.test(preview.className)) { + preview = document.createElement("div"); + preview.className = "editor-preview-side"; + wrapper.parentNode.insertBefore(preview, wrapper.nextSibling); + } + + // Syncs scroll editor -> preview + var cScroll = false; + var pScroll = false; + cm.on("scroll", function(v) { + if(cScroll) { + cScroll = false; + return; + } + pScroll = true; + var height = v.getScrollInfo().height - v.getScrollInfo().clientHeight; + var ratio = parseFloat(v.getScrollInfo().top) / height; + var move = (preview.scrollHeight - preview.clientHeight) * ratio; + preview.scrollTop = move; + }); + + // Syncs scroll preview -> editor + preview.onscroll = function() { + if(pScroll) { + pScroll = false; + return; + } + cScroll = true; + var height = preview.scrollHeight - preview.clientHeight; + var ratio = parseFloat(preview.scrollTop) / height; + var move = (cm.getScrollInfo().height - cm.getScrollInfo().clientHeight) * ratio; + cm.scrollTo(0, move); + }; + return preview; +}; + +SimpleMDE.prototype.createToolbar = function(items) { + items = items || this.options.toolbar; + + if(!items || items.length === 0) { + return; + } + var i; + for(i = 0; i < items.length; i++) { + if(toolbarBuiltInButtons[items[i]] != undefined) { + items[i] = toolbarBuiltInButtons[items[i]]; + } + } + + var bar = document.createElement("div"); + bar.className = "editor-toolbar"; + + var self = this; + + var toolbarData = {}; + self.toolbar = items; + + for(i = 0; i < items.length; i++) { + if(items[i].name == "guide" && self.options.toolbarGuideIcon === false) + continue; + + if(self.options.hideIcons && self.options.hideIcons.indexOf(items[i].name) != -1) + continue; + + // Fullscreen does not work well on mobile devices (even tablets) + // In the future, hopefully this can be resolved + if((items[i].name == "fullscreen" || items[i].name == "side-by-side") && isMobile()) + continue; + + + // Don't include trailing separators + if(items[i] === "|") { + var nonSeparatorIconsFollow = false; + + for(var x = (i + 1); x < items.length; x++) { + if(items[x] !== "|" && (!self.options.hideIcons || self.options.hideIcons.indexOf(items[x].name) == -1)) { + nonSeparatorIconsFollow = true; + } + } + + if(!nonSeparatorIconsFollow) + continue; + } + + + // Create the icon and append to the toolbar + (function(item) { + var el; + if(item === "|") { + el = createSep(); + } else { + el = createIcon(item, self.options.toolbarTips, self.options.shortcuts); + } + + // bind events, special for info + if(item.action) { + if(typeof item.action === "function") { + el.onclick = function(e) { + e.preventDefault(); + item.action(self); + }; + } else if(typeof item.action === "string") { + el.href = item.action; + el.target = "_blank"; + } + } + + toolbarData[item.name || item] = el; + bar.appendChild(el); + })(items[i]); + } + + self.toolbarElements = toolbarData; + + var cm = this.codemirror; + cm.on("cursorActivity", function() { + var stat = getState(cm); + + for(var key in toolbarData) { + (function(key) { + var el = toolbarData[key]; + if(stat[key]) { + el.className += " active"; + } else if(key != "fullscreen" && key != "side-by-side") { + el.className = el.className.replace(/\s*active\s*/g, ""); + } + })(key); + } + }); + + var cmWrapper = cm.getWrapperElement(); + cmWrapper.parentNode.insertBefore(bar, cmWrapper); + return bar; +}; + +SimpleMDE.prototype.createStatusbar = function(status) { + // Initialize + status = status || this.options.status; + var options = this.options; + var cm = this.codemirror; + + + // Make sure the status variable is valid + if(!status || status.length === 0) + return; + + + // Set up the built-in items + var items = []; + var i, onUpdate, defaultValue; + + for(i = 0; i < status.length; i++) { + // Reset some values + onUpdate = undefined; + defaultValue = undefined; + + + // Handle if custom or not + if(typeof status[i] === "object") { + items.push({ + className: status[i].className, + defaultValue: status[i].defaultValue, + onUpdate: status[i].onUpdate + }); + } else { + var name = status[i]; + + if(name === "words") { + defaultValue = function(el) { + el.innerHTML = wordCount(cm.getValue()); + }; + onUpdate = function(el) { + el.innerHTML = wordCount(cm.getValue()); + }; + } else if(name === "lines") { + defaultValue = function(el) { + el.innerHTML = cm.lineCount(); + }; + onUpdate = function(el) { + el.innerHTML = cm.lineCount(); + }; + } else if(name === "cursor") { + defaultValue = function(el) { + el.innerHTML = "0:0"; + }; + onUpdate = function(el) { + var pos = cm.getCursor(); + el.innerHTML = pos.line + ":" + pos.ch; + }; + } else if(name === "autosave") { + defaultValue = function(el) { + if(options.autosave != undefined && options.autosave.enabled === true) { + el.setAttribute("id", "autosaved"); + } + }; + } + + items.push({ + className: name, + defaultValue: defaultValue, + onUpdate: onUpdate + }); + } + } + + + // Create element for the status bar + var bar = document.createElement("div"); + bar.className = "editor-statusbar"; + + + // Create a new span for each item + for(i = 0; i < items.length; i++) { + // Store in temporary variable + var item = items[i]; + + + // Create span element + var el = document.createElement("span"); + el.className = item.className; + + + // Ensure the defaultValue is a function + if(typeof item.defaultValue === "function") { + item.defaultValue(el); + } + + + // Ensure the onUpdate is a function + if(typeof item.onUpdate === "function") { + // Create a closure around the span of the current action, then execute the onUpdate handler + this.codemirror.on("update", (function(el, item) { + return function() { + item.onUpdate(el); + }; + }(el, item))); + } + + + // Append the item to the status bar + bar.appendChild(el); + } + + + // Insert the status bar into the DOM + var cmWrapper = this.codemirror.getWrapperElement(); + cmWrapper.parentNode.insertBefore(bar, cmWrapper.nextSibling); + return bar; +}; + +/** + * Get or set the text content. + */ +SimpleMDE.prototype.value = function(val) { + if(val === undefined) { + return this.codemirror.getValue(); + } else { + this.codemirror.getDoc().setValue(val); + return this; + } +}; + + +/** + * Bind static methods for exports. + */ +SimpleMDE.toggleBold = toggleBold; +SimpleMDE.toggleItalic = toggleItalic; +SimpleMDE.toggleStrikethrough = toggleStrikethrough; +SimpleMDE.toggleBlockquote = toggleBlockquote; +SimpleMDE.toggleHeadingSmaller = toggleHeadingSmaller; +SimpleMDE.toggleHeadingBigger = toggleHeadingBigger; +SimpleMDE.toggleHeading1 = toggleHeading1; +SimpleMDE.toggleHeading2 = toggleHeading2; +SimpleMDE.toggleHeading3 = toggleHeading3; +SimpleMDE.toggleCodeBlock = toggleCodeBlock; +SimpleMDE.toggleUnorderedList = toggleUnorderedList; +SimpleMDE.toggleOrderedList = toggleOrderedList; +SimpleMDE.cleanBlock = cleanBlock; +SimpleMDE.drawLink = drawLink; +SimpleMDE.drawImage = drawImage; +SimpleMDE.drawTable = drawTable; +SimpleMDE.drawHorizontalRule = drawHorizontalRule; +SimpleMDE.undo = undo; +SimpleMDE.redo = redo; +SimpleMDE.togglePreview = togglePreview; +SimpleMDE.toggleSideBySide = toggleSideBySide; +SimpleMDE.toggleFullScreen = toggleFullScreen; + +/** + * Bind instance methods for exports. + */ +SimpleMDE.prototype.toggleBold = function() { + toggleBold(this); +}; +SimpleMDE.prototype.toggleItalic = function() { + toggleItalic(this); +}; +SimpleMDE.prototype.toggleStrikethrough = function() { + toggleStrikethrough(this); +}; +SimpleMDE.prototype.toggleBlockquote = function() { + toggleBlockquote(this); +}; +SimpleMDE.prototype.toggleHeadingSmaller = function() { + toggleHeadingSmaller(this); +}; +SimpleMDE.prototype.toggleHeadingBigger = function() { + toggleHeadingBigger(this); +}; +SimpleMDE.prototype.toggleHeading1 = function() { + toggleHeading1(this); +}; +SimpleMDE.prototype.toggleHeading2 = function() { + toggleHeading2(this); +}; +SimpleMDE.prototype.toggleHeading3 = function() { + toggleHeading3(this); +}; +SimpleMDE.prototype.toggleCodeBlock = function() { + toggleCodeBlock(this); +}; +SimpleMDE.prototype.toggleUnorderedList = function() { + toggleUnorderedList(this); +}; +SimpleMDE.prototype.toggleOrderedList = function() { + toggleOrderedList(this); +}; +SimpleMDE.prototype.cleanBlock = function() { + cleanBlock(this); +}; +SimpleMDE.prototype.drawLink = function() { + drawLink(this); +}; +SimpleMDE.prototype.drawImage = function() { + drawImage(this); +}; +SimpleMDE.prototype.drawTable = function() { + drawTable(this); +}; +SimpleMDE.prototype.drawHorizontalRule = function() { + drawHorizontalRule(this); +}; +SimpleMDE.prototype.undo = function() { + undo(this); +}; +SimpleMDE.prototype.redo = function() { + redo(this); +}; +SimpleMDE.prototype.togglePreview = function() { + togglePreview(this); +}; +SimpleMDE.prototype.toggleSideBySide = function() { + toggleSideBySide(this); +}; +SimpleMDE.prototype.toggleFullScreen = function() { + toggleFullScreen(this); +}; + +SimpleMDE.prototype.isPreviewActive = function() { + var cm = this.codemirror; + var wrapper = cm.getWrapperElement(); + var preview = wrapper.lastChild; + + return /editor-preview-active/.test(preview.className); +}; + +SimpleMDE.prototype.isSideBySideActive = function() { + var cm = this.codemirror; + var wrapper = cm.getWrapperElement(); + var preview = wrapper.nextSibling; + + return /editor-preview-active-side/.test(preview.className); +}; + +SimpleMDE.prototype.isFullscreenActive = function() { + var cm = this.codemirror; + + return cm.getOption("fullScreen"); +}; + +SimpleMDE.prototype.getState = function() { + var cm = this.codemirror; + + return getState(cm); +}; + +SimpleMDE.prototype.toTextArea = function() { + var cm = this.codemirror; + var wrapper = cm.getWrapperElement(); + + if(wrapper.parentNode) { + if(this.gui.toolbar) { + wrapper.parentNode.removeChild(this.gui.toolbar); + } + if(this.gui.statusbar) { + wrapper.parentNode.removeChild(this.gui.statusbar); + } + if(this.gui.sideBySide) { + wrapper.parentNode.removeChild(this.gui.sideBySide); + } + } + + cm.toTextArea(); + + if(this.autosaveTimeoutId) { + clearTimeout(this.autosaveTimeoutId); + this.autosaveTimeoutId = undefined; + this.clearAutosavedValue(); + } +}; + +module.exports = SimpleMDE; \ No newline at end of file diff --git a/static/js/utils.js b/static/js/utils.js new file mode 100644 index 0000000..83e6397 --- /dev/null +++ b/static/js/utils.js @@ -0,0 +1,18 @@ +function deg2rad(deg) { + return deg * (Math.PI/180) +} +function getDistanceFromLatLon(item1, item2) { + lat1 = item1['latitude']; lon1 = item1['longitude']; + lat2 = item2['latitude']; lon2 = item2['longitude']; + var R = 3958.8; // Radius of the earth + var dLat = deg2rad(lat2-lat1); + var dLon = deg2rad(lon2-lon1); + var a = + Math.sin(dLat/2) * Math.sin(dLat/2) + + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * + Math.sin(dLon/2) * Math.sin(dLon/2) + ; + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + var d = R * c; // Distance in mi + return d; +} \ No newline at end of file -- cgit v1.2.3