...page content...
*---------------------------------------------------------------------*/
- let bannerJsonUrl = 'https://lvgl.io/data/banner.json';
- let bannerContainerClass = 'lv-custom-banner-list';
- let bannerClass = 'lv-custom-banner';
- /* Note: banner priority property can have only one of these values:
- * ("highest" | "high" | "normal" | "low" | "lowest").
- * If not present, the default is "normal-priority". This controls banner styling. */
- let priorityPropVals = ["highest", "high", "normal", "low", "lowest"];
- let defaultPrioPropVal = priorityPropVals[2];
- let priorityClassSuffix = "-priority";
+ let bannerJsonUrl = "https://lvgl.io/data/banner.json";
+ let bannerContainerClass = "lv-custom-banner-list";
+ let bannerClass = "lv-custom-banner";
+ /* Note: banner priority property can have only one of these values:
+ * ("highest" | "high" | "normal" | "low" | "lowest").
+ * If not present, the default is "normal-priority". This controls banner styling. */
+ let priorityPropVals = ["highest", "high", "normal", "low", "lowest"];
+ let defaultPrioPropVal = priorityPropVals[2];
+ let priorityClassSuffix = "-priority";
- /* Sorting json banners in priority order.
- * `a` and `b` are BANNER objects from incoming `banner.json`. */
- function prio_compare(a, b) {
- let aPrioPropStr = a.hasOwnProperty('priority') ? a.priority : defaultPrioPropVal;
- let bPrioPropStr = b.hasOwnProperty('priority') ? b.priority : defaultPrioPropVal;
- let aPrio = 0;
- let bPrio = 0;
+ /* Sorting json banners in priority order.
+ * `a` and `b` are BANNER objects from incoming `banner.json`. */
+ function prio_compare(a, b) {
+ let aPrioPropStr = a.hasOwnProperty("priority") ? a.priority : defaultPrioPropVal;
+ let bPrioPropStr = b.hasOwnProperty("priority") ? b.priority : defaultPrioPropVal;
+ let aPrio = 0;
+ let bPrio = 0;
- /* Establish numeric values for `a` and `b`. */
- for (var i = 0; i < 5; i++) {
- if (aPrioPropStr === priorityPropVals[i]) {
- aPrio = i;
- break;
- }
- }
-
- for (var i = 0; i < 5; i++) {
- if (bPrioPropStr === priorityPropVals[i]) {
- bPrio = i;
- break;
- }
- }
-
- /* Correctness Proof
- * -----------------
- * < 0 = `a` should come before `b`.
- * > 0 = `a` should come after `b`.
- * 0 or NaN = a === b.
- *
- * Example: a === "highest-priority"; b === "normal-priority".
- * aPrio === 0 ; bPrio === 2.
- * aPrio - bPrio === -2 means (`a` should come before `b`).
- */
- return aPrio - bPrio;
+ /* Establish numeric values for `a` and `b`. */
+ for (var i = 0; i < 5; i++) {
+ if (aPrioPropStr === priorityPropVals[i]) {
+ aPrio = i;
+ break;
+ }
}
- fetch(bannerJsonUrl)
- .then(response => {
- if (response.ok) {
- return response.json();
- } else {
- /* Note: per OOSC2, it is not appropriate to throw an exception for a
- * situation that is being checked for. Sometimes the banner file will
- * not be there, in which case, we simply return an empty array object. */
- return [];
+ for (var i = 0; i < 5; i++) {
+ if (bPrioPropStr === priorityPropVals[i]) {
+ bPrio = i;
+ break;
+ }
+ }
+
+ /* Correctness Proof
+ * -----------------
+ * < 0 = `a` should come before `b`.
+ * > 0 = `a` should come after `b`.
+ * 0 or NaN = a === b.
+ *
+ * Example: a === "highest-priority"; b === "normal-priority".
+ * aPrio === 0 ; bPrio === 2.
+ * aPrio - bPrio === -2 means (`a` should come before `b`).
+ */
+ return aPrio - bPrio;
+ }
+
+ fetch(bannerJsonUrl)
+ .then((response) => {
+ if (response.ok) {
+ return response.json();
+ } else {
+ /* Note: per OOSC2, it is not appropriate to throw an exception for a
+ * situation that is being checked for. Sometimes the banner file will
+ * not be there, in which case, we simply return an empty array object. */
+ return [];
+ }
+ })
+ /* JSON file was fetched successfully.... */
+ .then((json) => {
+ if (json.constructor !== Array) {
+ /* Data structure not recognized. */
+ } else {
+ /* console.log('JSON is an array.'); */
+ /* Does it contain any banners? */
+ if (json.length === 0) {
+ console.log("JSON has no banners -- nothing to do.");
+ } else {
+ /* Note: `div.page` is unique to Furo theme. */
+
+ /* Create and insert banner container. */
+ const newDiv = document.createElement("div");
+ newDiv.classList.add(bannerContainerClass);
+ let bannerCount = 0;
+
+ /* Create a
or an element for each banner.
+ * First, sort them in priority order with "highest-priority" being
+ * at the top. The JSON is an ARRAY of BANNER objects.
+ * `prio_compare()` knows how to compare them.
+ *
+ * If the BANNER object has a "url" property, then
+ * encapsulate banner in an anchor element that will send
+ * user to designated URL.
+ */
+ json.sort(prio_compare);
+
+ for (var i = 0; i < json.length; i++) {
+ let banner = json[i];
+
+ if (banner.hasOwnProperty("label")) {
+ bannerCount++;
+ let priorityClass = "";
+ let newElement = null;
+
+ if (banner.hasOwnProperty("url")) {
+ newElement = document.createElement("a");
+ newElement.setAttribute("href", banner.url);
+ } else {
+ newElement = document.createElement("p");
+ }
+
+ if (banner.hasOwnProperty("priority")) {
+ priorityClass = banner.priority + priorityClassSuffix;
+ } else {
+ priorityClass = defaultPrioPropVal + priorityClassSuffix;
+ }
+
+ newElement.innerHTML = banner.label;
+ newElement.classList.add(bannerClass);
+ newElement.classList.add(priorityClass);
+ newDiv.appendChild(newElement);
}
- })
- /* JSON file was fetched successfully.... */
- .then(json => {
- if (json.constructor !== Array) {
- /* Data structure not recognized. */
- } else {
- /* console.log('JSON is an array.'); */
- /* Does it contain any banners? */
- if (json.length === 0) {
- console.log('JSON has no banners -- nothing to do.');
- } else {
- /* Note: `div.page` is unique to Furo theme. */
+ }
- /* Create and insert banner container. */
- const newDiv = document.createElement('div');
- newDiv.classList.add(bannerContainerClass);
- let bannerCount = 0;
+ if (bannerCount > 0) {
+ const page = document.querySelector("div.page");
+ const pgParent = page.parentElement;
+ pgParent.insertBefore(newDiv, page);
- /* Create a or an element for each banner.
- * First, sort them in priority order with "highest-priority" being
- * at the top. The JSON is an ARRAY of BANNER objects.
- * `prio_compare()` knows how to compare them.
- *
- * If the BANNER object has a "url" property, then
- * encapsulate banner in an anchor element that will send
- * user to designated URL.
- */
- json.sort(prio_compare);
-
- for (var i = 0; i < json.length; i++) {
- let banner = json[i];
-
- if (banner.hasOwnProperty('label')) {
- bannerCount++;
- let priorityClass = '';
- let newElement = null;
-
- if (banner.hasOwnProperty('url')) {
- newElement = document.createElement('a');
- newElement.setAttribute('href', banner.url)
- } else {
- newElement = document.createElement('p');
- }
-
- if (banner.hasOwnProperty('priority')) {
- priorityClass = banner.priority + priorityClassSuffix;
- } else {
- priorityClass = defaultPrioPropVal + priorityClassSuffix;
- }
-
- newElement.innerHTML = banner.label;
- newElement.classList.add(bannerClass);
- newElement.classList.add(priorityClass);
- newDiv.appendChild(newElement);
- }
- }
-
- if (bannerCount > 0) {
- const page = document.querySelector('div.page');
- const pgParent = page.parentElement;
- pgParent.insertBefore(newDiv, page);
-
- /* Finally, we need to tell the page element that its `min-hight`
- * is 100% minus the hight of all the banners, including the one
- * supplied by `conf.py` in `conf.html_theme_options.announcement`
- * if one is present === var(--header-height).
- *
- * This extends short pages by just enough to place [PREV] and [NEXT]
- * buttoms and footer at bottom of page without scrolling.
- *
- * Note: this overrides the `min-height` property set for this
- * element in `furo.css`, which is: calc(100% - var(--header-height)).
- * It additionally subtracts height of banner list.
- * */
- let height = newDiv.offsetHeight;
- page.style['min-height'] = `calc(100% - var(--header-height) - ${height}px)`;
- }
- }
- }
- }) .catch(error => {
- console.error('Fetch error: ' + error.message);
- });
+ /* Finally, we need to tell the page element that its `min-hight`
+ * is 100% minus the hight of all the banners, including the one
+ * supplied by `conf.py` in `conf.html_theme_options.announcement`
+ * if one is present === var(--header-height).
+ *
+ * This extends short pages by just enough to place [PREV] and [NEXT]
+ * buttoms and footer at bottom of page without scrolling.
+ *
+ * Note: this overrides the `min-height` property set for this
+ * element in `furo.css`, which is: calc(100% - var(--header-height)).
+ * It additionally subtracts height of banner list.
+ * */
+ let height = newDiv.offsetHeight;
+ page.style["min-height"] = `calc(100% - var(--header-height) - ${height}px)`;
+ }
+ }
+ }
+ })
+ .catch((error) => {
+ console.error("Fetch error: " + error.message);
+ });
});