Add version selector (#143)

diff --git a/Dockerfile b/Dockerfile
index 585cbc7..d338ebc 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -54,6 +54,9 @@
     && apt-get clean \
     && rm -rf /var/lib/apt/lists/*
 
+RUN curl -sL "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64" > /usr/local/bin/jq \
+    && chmod +x /usr/local/bin/jq
+
 RUN HUGOHOME="$(mktemp -d)" \
     && export HUGOHOME \
     && curl -sL https://github.com/gohugoio/hugo/releases/download/v0.58.3/hugo_extended_0.58.3_Linux-64bit.tar.gz > "${HUGOHOME}/hugo.tar.gz" \
diff --git a/docs-archive/show_docs_index_json.sh b/docs-archive/show_docs_index_json.sh
new file mode 100755
index 0000000..297d737
--- /dev/null
+++ b/docs-archive/show_docs_index_json.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+set -euo pipefail
+
+MY_DIR="$(cd "$(dirname "$0")" && pwd)"
+pushd "${MY_DIR}" &>/dev/null || exit 1
+
+jq -n '{stable: $stable, versions: $versions[0:-1] | split("\n") }' \
+    -M \
+    --rawfile stable <(cat stable.txt | tr -d '[:space:]') \
+    --rawfile versions <(ls -d ./*/ | cut -d "/" -f 2)
+
+popd &>/dev/null || exit 1
diff --git a/landing-pages/.gitignore b/landing-pages/.gitignore
index c8b191f..4aa6c9e 100644
--- a/landing-pages/.gitignore
+++ b/landing-pages/.gitignore
@@ -2,4 +2,4 @@
 dist/
 site/data/webpack.json
 resources/
-site/static/indexes/
+site/static/_gen/
diff --git a/landing-pages/create-index.js b/landing-pages/create-index.js
index 69ec28f..495b5fc 100644
--- a/landing-pages/create-index.js
+++ b/landing-pages/create-index.js
@@ -27,7 +27,7 @@
 const lunrjs = require("lunr");
 
 const contentDirectory = `${__dirname}/site/content`;
-const outputtDirectory = `${__dirname}/site/static/indexes`;
+const outputtDirectory = `${__dirname}/site/static/_gen/indexes`;
 
 
 async function isDirectoryExists(dirPath) {
diff --git a/landing-pages/site/assets/scss/_dropdown.scss b/landing-pages/site/assets/scss/_dropdown.scss
new file mode 100644
index 0000000..888d67c
--- /dev/null
+++ b/landing-pages/site/assets/scss/_dropdown.scss
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+@import "fonts";
+@import "colors";
+
+.dropdown-menu {
+  font-family: $primary-font;
+}
+
+.dropdown-toggle::after {
+  color: map-get($colors, greyish-brown);
+}
+
+.sidebar__version-selector {
+  margin-bottom: 22px;
+
+  a {
+    @extend .bodytext__medium--brownish-grey;
+  }
+}
diff --git a/landing-pages/site/assets/scss/main-custom.scss b/landing-pages/site/assets/scss/main-custom.scss
index 8a1ed45..7b06c2a 100644
--- a/landing-pages/site/assets/scss/main-custom.scss
+++ b/landing-pages/site/assets/scss/main-custom.scss
@@ -49,3 +49,4 @@
 @import "rst-content";
 @import "pygments";
 @import "content-drawer";
+@import "dropdown";
diff --git a/landing-pages/site/layouts/partials/version-selector.html b/landing-pages/site/layouts/partials/version-selector.html
new file mode 100644
index 0000000..63db3e9
--- /dev/null
+++ b/landing-pages/site/layouts/partials/version-selector.html
@@ -0,0 +1,31 @@
+{{/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/}}
+
+<div id="docs-version-selector" class="docs-version-selector sidebar__version-selector">
+    <a class="dropdown-toggle" href="#" id="versionDropdown" role="button" data-toggle="dropdown" aria-haspopup="true"
+       aria-expanded="false">
+        <span class="bodytext__medium--greyish-brown">Version: </span><span class="version">Select version</span>
+    </a>
+    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
+
+    </div>
+    <script type="application/x-template" id="version-item-template">
+        <a class="dropdown-item"></a>
+    </script>
+</div>
diff --git a/landing-pages/site/static/indexes/.keep b/landing-pages/site/static/_gen/indexes/.keep
similarity index 100%
rename from landing-pages/site/static/indexes/.keep
rename to landing-pages/site/static/_gen/indexes/.keep
diff --git a/landing-pages/src/docs-index.js b/landing-pages/src/docs-index.js
index 884ea65..7c0917e 100644
--- a/landing-pages/src/docs-index.js
+++ b/landing-pages/src/docs-index.js
@@ -24,3 +24,4 @@
 import "./js/rating";
 import "./js/makeTableResponsive";
 import "./js/tocTree";
+import "./js/versionSelector";
diff --git a/landing-pages/src/index.js b/landing-pages/src/index.js
index 9a871e7..2ebe023 100644
--- a/landing-pages/src/index.js
+++ b/landing-pages/src/index.js
@@ -25,6 +25,7 @@
 import "./js/progressTracking";
 import "./js/rating";
 import "./js/makeTableResponsive";
+import "./js/versionSelector";
 
 if (document.querySelector("#search")) {
     import(/* webpackChunkName: "search" */ "./js/searchBlogPosts");
diff --git a/landing-pages/src/js/searchBlogPosts.js b/landing-pages/src/js/searchBlogPosts.js
index 85fe443..5cd516e 100644
--- a/landing-pages/src/js/searchBlogPosts.js
+++ b/landing-pages/src/js/searchBlogPosts.js
@@ -47,8 +47,8 @@
 };
 
 Promise.all([
-  fetch("/indexes/en/blog-index.json"),
-  fetch("/indexes/en/blog-posts.json")
+  fetch("/_gen/indexes/en/blog-index.json"),
+  fetch("/_gen/indexes/en/blog-posts.json")
 ]).then(function([indexResp, postsResp]) {
   return Promise.all([
     indexResp.json(),
diff --git a/landing-pages/src/js/versionSelector.js b/landing-pages/src/js/versionSelector.js
new file mode 100644
index 0000000..316fcda
--- /dev/null
+++ b/landing-pages/src/js/versionSelector.js
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const runVersionSelector = () => {
+  const versionSelectors = window.document.querySelectorAll(".docs-version-selector");
+
+  if (!versionSelectors || versionSelectors.length === 0) {
+    return;
+  }
+
+  fetch("/_gen/docs-index.json")
+    .then((resp) => resp.json())
+    .then(({stable, versions}) => {
+      versionSelectors.forEach((versionSelector) => {
+        const templateText = versionSelector.querySelector("#version-item-template").innerText;
+        let templateElement = document.createElement("div");
+        templateElement.innerHTML = templateText;
+        templateElement = templateElement.firstElementChild;
+
+        const dropdownMenu = versionSelector.querySelector(".dropdown-menu");
+
+
+        const currentVersion = window.document.location.pathname.split("/")[2];
+
+        const appendNewVersionLink = (location, label) => {
+          const newElement = templateElement.cloneNode(true);
+          const newDocsLink = document.location.toString().replace(
+            `/${currentVersion}/`, `/${location}/`
+          );
+          newElement.setAttribute("href", newDocsLink);
+          newElement.innerText = label;
+          dropdownMenu.appendChild(newElement);
+        };
+        appendNewVersionLink("stable", `Stable (${stable})`);
+        versions.forEach((version) => appendNewVersionLink(version, version));
+      });
+    });
+};
+
+runVersionSelector();
diff --git a/site.sh b/site.sh
index 851bd55..e27d23c 100755
--- a/site.sh
+++ b/site.sh
@@ -203,9 +203,14 @@
     run_command "${script_working_directory}" "${command}" "${DOCKER_PATHS[@]}"
 }
 
+function prepare_docs_index() {
+    run_command "/opt/site/docs-archive/" ./show_docs_index_json.sh > landing-pages/site/static/_gen/docs-index.json
+}
+
 function build_site {
     mkdir -p dist
     rm -rf dist/*
+    prepare_docs_index
     run_command "/opt/site/landing-pages/" npm run build
     cp -R landing-pages/dist/ dist/
     mkdir -p dist/docs/
@@ -291,6 +296,7 @@
 elif [[ "${CMD}" == "preview" ]]; then
     ensure_node_module_exists
     run_command "/opt/site/landing-pages/" npm run index
+    prepare_docs_index
     run_command "/opt/site/landing-pages/" npm run preview
 elif [[ "${CMD}" == "build-landing-pages" ]]; then
     ensure_node_module_exists
diff --git a/sphinx_airflow_theme/demo/conf.py b/sphinx_airflow_theme/demo/conf.py
index 44cc1a5..e3eafd5 100644
--- a/sphinx_airflow_theme/demo/conf.py
+++ b/sphinx_airflow_theme/demo/conf.py
@@ -15,6 +15,7 @@
 # specific language governing permissions and limitations
 # under the License.
 
+import os
 # Configuration file for the Sphinx documentation builder.
 #
 # This file only contains a selection of the most common options. For a full
@@ -37,7 +38,7 @@
 project = 'Theme demo'
 copyright = '2019, Apache Software Foundation'
 author = 'Apache Software Foundation'
-
+version = os.environ.get('RELEASE_VERSION', 'latest')
 
 # -- General configuration ---------------------------------------------------
 
diff --git a/sphinx_airflow_theme/sphinx_airflow_theme/__init__.py b/sphinx_airflow_theme/sphinx_airflow_theme/__init__.py
index 7165840..5132835 100644
--- a/sphinx_airflow_theme/sphinx_airflow_theme/__init__.py
+++ b/sphinx_airflow_theme/sphinx_airflow_theme/__init__.py
@@ -31,4 +31,3 @@
 def setup(app):
     app.add_html_theme('sphinx_airflow_theme', path.abspath(path.dirname(__file__)))
     app.add_stylesheet('_gen/css//main-custom.min.css')
-    app.add_js_file('_gen/js/docs.js')
diff --git a/sphinx_airflow_theme/sphinx_airflow_theme/version-selector.html b/sphinx_airflow_theme/sphinx_airflow_theme/version-selector.html
index d5e400a..253c7b1 100644
--- a/sphinx_airflow_theme/sphinx_airflow_theme/version-selector.html
+++ b/sphinx_airflow_theme/sphinx_airflow_theme/version-selector.html
@@ -17,17 +17,15 @@
  under the License.
 #}
 
-<div id="version-selector" class="sidebar__version-selector">
-    <a class="dropdown-toggle" href="#" id="versionDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
-        <span class="bodytext__medium--greyish-brown">Version: </span><span id="selected-version">undefined</span>
+<div id="docs-version-selector" class="docs-version-selector sidebar__version-selector">
+    <a class="dropdown-toggle" href="#" id="versionDropdown" role="button" data-toggle="dropdown" aria-haspopup="true"
+       aria-expanded="false">
+        <span class="bodytext__medium--greyish-brown">Version: </span><span class="version">{{ version }}</span>
     </a>
-    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(15px, 33px, 0px);">
-        <a class="dropdown-item" href="/docs/1.10.1">1.10.1</a>
-        <a class="dropdown-item" href="/docs/1.10.2">1.10.2</a>
-        <a class="dropdown-item" href="/docs/1.10.3">1.10.3</a>
-        <a class="dropdown-item" href="/docs/1.10.4">1.10.4</a>
-        <a class="dropdown-item" href="/docs/1.10.5">1.10.5</a>
-        <a class="dropdown-item" href="/docs/1.10.6">1.10.6</a>
-        <a class="dropdown-item" href="/docs/1.10.7">1.10.7</a>
+    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
+
     </div>
+    <script type="application/x-template" id="version-item-template">
+        <a class="dropdown-item"></a>
+    </script>
 </div>