mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-11-04 02:53:06 +00:00 
			
		
		
		
	use spaces for indentation
This commit is contained in:
		@@ -1,66 +1,66 @@
 | 
			
		||||
$(() => {
 | 
			
		||||
	// const buttons = $('.choose_file_button')
 | 
			
		||||
	const popups = $(".choose_file_widget");
 | 
			
		||||
	popups.dialog({
 | 
			
		||||
		autoOpen: false,
 | 
			
		||||
		modal: true,
 | 
			
		||||
		width: "90%",
 | 
			
		||||
		create: (event) => {
 | 
			
		||||
			const target = $(event.target);
 | 
			
		||||
			target.parent().css({
 | 
			
		||||
				position: "fixed",
 | 
			
		||||
				top: "5%",
 | 
			
		||||
				bottom: "5%",
 | 
			
		||||
			});
 | 
			
		||||
			target.css("height", "300px");
 | 
			
		||||
			console.log(target);
 | 
			
		||||
		},
 | 
			
		||||
		buttons: [
 | 
			
		||||
			{
 | 
			
		||||
				text: "Choose",
 | 
			
		||||
				click: function () {
 | 
			
		||||
					console.log($("#file_id"));
 | 
			
		||||
					$(`input[name=${$(this).attr("name")}]`).attr(
 | 
			
		||||
						"value",
 | 
			
		||||
						$("#file_id").attr("value"),
 | 
			
		||||
					);
 | 
			
		||||
					$(this).dialog("close");
 | 
			
		||||
				},
 | 
			
		||||
				disabled: true,
 | 
			
		||||
			},
 | 
			
		||||
		],
 | 
			
		||||
	});
 | 
			
		||||
	$(".choose_file_button")
 | 
			
		||||
		.button()
 | 
			
		||||
		.on("click", function () {
 | 
			
		||||
			const popup = popups.filter(`[name=${$(this).attr("name")}]`);
 | 
			
		||||
			console.log(popup);
 | 
			
		||||
			popup.html(
 | 
			
		||||
				'<iframe src="/file/popup" width="100%" height="95%"></iframe><div id="file_id" value="null" />',
 | 
			
		||||
			);
 | 
			
		||||
			popup.dialog({ title: $(this).text() }).dialog("open");
 | 
			
		||||
		});
 | 
			
		||||
	$("#quick_notif li").click(function () {
 | 
			
		||||
		$(this).hide();
 | 
			
		||||
	});
 | 
			
		||||
  // const buttons = $('.choose_file_button')
 | 
			
		||||
  const popups = $(".choose_file_widget");
 | 
			
		||||
  popups.dialog({
 | 
			
		||||
    autoOpen: false,
 | 
			
		||||
    modal: true,
 | 
			
		||||
    width: "90%",
 | 
			
		||||
    create: (event) => {
 | 
			
		||||
      const target = $(event.target);
 | 
			
		||||
      target.parent().css({
 | 
			
		||||
        position: "fixed",
 | 
			
		||||
        top: "5%",
 | 
			
		||||
        bottom: "5%",
 | 
			
		||||
      });
 | 
			
		||||
      target.css("height", "300px");
 | 
			
		||||
      console.log(target);
 | 
			
		||||
    },
 | 
			
		||||
    buttons: [
 | 
			
		||||
      {
 | 
			
		||||
        text: "Choose",
 | 
			
		||||
        click: function () {
 | 
			
		||||
          console.log($("#file_id"));
 | 
			
		||||
          $(`input[name=${$(this).attr("name")}]`).attr(
 | 
			
		||||
            "value",
 | 
			
		||||
            $("#file_id").attr("value"),
 | 
			
		||||
          );
 | 
			
		||||
          $(this).dialog("close");
 | 
			
		||||
        },
 | 
			
		||||
        disabled: true,
 | 
			
		||||
      },
 | 
			
		||||
    ],
 | 
			
		||||
  });
 | 
			
		||||
  $(".choose_file_button")
 | 
			
		||||
    .button()
 | 
			
		||||
    .on("click", function () {
 | 
			
		||||
      const popup = popups.filter(`[name=${$(this).attr("name")}]`);
 | 
			
		||||
      console.log(popup);
 | 
			
		||||
      popup.html(
 | 
			
		||||
        '<iframe src="/file/popup" width="100%" height="95%"></iframe><div id="file_id" value="null" />',
 | 
			
		||||
      );
 | 
			
		||||
      popup.dialog({ title: $(this).text() }).dialog("open");
 | 
			
		||||
    });
 | 
			
		||||
  $("#quick_notif li").click(function () {
 | 
			
		||||
    $(this).hide();
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
function createQuickNotif(msg) {
 | 
			
		||||
	const el = document.createElement("li");
 | 
			
		||||
	el.textContent = msg;
 | 
			
		||||
	el.addEventListener("click", () => el.parentNode.removeChild(el));
 | 
			
		||||
	document.getElementById("quick_notif").appendChild(el);
 | 
			
		||||
  const el = document.createElement("li");
 | 
			
		||||
  el.textContent = msg;
 | 
			
		||||
  el.addEventListener("click", () => el.parentNode.removeChild(el));
 | 
			
		||||
  document.getElementById("quick_notif").appendChild(el);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function deleteQuickNotifs() {
 | 
			
		||||
	const el = document.getElementById("quick_notif");
 | 
			
		||||
	while (el.firstChild) {
 | 
			
		||||
		el.removeChild(el.firstChild);
 | 
			
		||||
	}
 | 
			
		||||
  const el = document.getElementById("quick_notif");
 | 
			
		||||
  while (el.firstChild) {
 | 
			
		||||
    el.removeChild(el.firstChild);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function display_notif() {
 | 
			
		||||
	$("#header_notif").toggle().parent().toggleClass("white");
 | 
			
		||||
  $("#header_notif").toggle().parent().toggleClass("white");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// You can't get the csrf token from the template in a widget
 | 
			
		||||
@@ -70,7 +70,7 @@ function display_notif() {
 | 
			
		||||
// So, the true workaround is to get the token from the dom
 | 
			
		||||
// https://docs.djangoproject.com/en/2.0/ref/csrf/#acquiring-the-token-if-csrf-use-sessions-is-true
 | 
			
		||||
function getCSRFToken() {
 | 
			
		||||
	return $("[name=csrfmiddlewaretoken]").val();
 | 
			
		||||
  return $("[name=csrfmiddlewaretoken]").val();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const initialUrlParams = new URLSearchParams(window.location.search);
 | 
			
		||||
@@ -80,9 +80,9 @@ const initialUrlParams = new URLSearchParams(window.location.search);
 | 
			
		||||
 * @enum {number}
 | 
			
		||||
 */
 | 
			
		||||
const History = {
 | 
			
		||||
	NONE: 0,
 | 
			
		||||
	PUSH: 1,
 | 
			
		||||
	REPLACE: 2,
 | 
			
		||||
  NONE: 0,
 | 
			
		||||
  PUSH: 1,
 | 
			
		||||
  REPLACE: 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -92,29 +92,29 @@ const History = {
 | 
			
		||||
 * @param {URL | null} url
 | 
			
		||||
 */
 | 
			
		||||
function update_query_string(key, value, action = History.REPLACE, url = null) {
 | 
			
		||||
	let ret = url;
 | 
			
		||||
	if (!ret) {
 | 
			
		||||
		ret = new URL(window.location.href);
 | 
			
		||||
	}
 | 
			
		||||
	if (value === undefined || value === null || value === "") {
 | 
			
		||||
		// If the value is null, undefined or empty => delete it
 | 
			
		||||
		ret.searchParams.delete(key);
 | 
			
		||||
	} else if (Array.isArray(value)) {
 | 
			
		||||
		ret.searchParams.delete(key);
 | 
			
		||||
		for (const v of value) {
 | 
			
		||||
			ret.searchParams.append(key, v);
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		ret.searchParams.set(key, value);
 | 
			
		||||
	}
 | 
			
		||||
  let ret = url;
 | 
			
		||||
  if (!ret) {
 | 
			
		||||
    ret = new URL(window.location.href);
 | 
			
		||||
  }
 | 
			
		||||
  if (value === undefined || value === null || value === "") {
 | 
			
		||||
    // If the value is null, undefined or empty => delete it
 | 
			
		||||
    ret.searchParams.delete(key);
 | 
			
		||||
  } else if (Array.isArray(value)) {
 | 
			
		||||
    ret.searchParams.delete(key);
 | 
			
		||||
    for (const v of value) {
 | 
			
		||||
      ret.searchParams.append(key, v);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ret.searchParams.set(key, value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	if (action === History.PUSH) {
 | 
			
		||||
		window.history.pushState(null, "", ret.toString());
 | 
			
		||||
	} else if (action === History.REPLACE) {
 | 
			
		||||
		window.history.replaceState(null, "", ret.toString());
 | 
			
		||||
	}
 | 
			
		||||
  if (action === History.PUSH) {
 | 
			
		||||
    window.history.pushState(null, "", ret.toString());
 | 
			
		||||
  } else if (action === History.REPLACE) {
 | 
			
		||||
    window.history.replaceState(null, "", ret.toString());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
  return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO : If one day a test workflow is made for JS in this project
 | 
			
		||||
@@ -126,28 +126,26 @@ function update_query_string(key, value, action = History.REPLACE, url = null) {
 | 
			
		||||
 * @return {Promise<Object[]>}
 | 
			
		||||
 */
 | 
			
		||||
async function fetch_paginated(url) {
 | 
			
		||||
	const max_per_page = 199;
 | 
			
		||||
	const paginated_url = new URL(url, document.location.origin);
 | 
			
		||||
	paginated_url.searchParams.set("page_size", max_per_page.toString());
 | 
			
		||||
	paginated_url.searchParams.set("page", "1");
 | 
			
		||||
  const max_per_page = 199;
 | 
			
		||||
  const paginated_url = new URL(url, document.location.origin);
 | 
			
		||||
  paginated_url.searchParams.set("page_size", max_per_page.toString());
 | 
			
		||||
  paginated_url.searchParams.set("page", "1");
 | 
			
		||||
 | 
			
		||||
	const first_page = await (await fetch(paginated_url)).json();
 | 
			
		||||
	const results = first_page.results;
 | 
			
		||||
  const first_page = await (await fetch(paginated_url)).json();
 | 
			
		||||
  const results = first_page.results;
 | 
			
		||||
 | 
			
		||||
	const nb_pictures = first_page.count;
 | 
			
		||||
	const nb_pages = Math.ceil(nb_pictures / max_per_page);
 | 
			
		||||
  const nb_pictures = first_page.count;
 | 
			
		||||
  const nb_pages = Math.ceil(nb_pictures / max_per_page);
 | 
			
		||||
 | 
			
		||||
	if (nb_pages > 1) {
 | 
			
		||||
		const promises = [];
 | 
			
		||||
		for (let i = 2; i <= nb_pages; i++) {
 | 
			
		||||
			paginated_url.searchParams.set("page", i.toString());
 | 
			
		||||
			promises.push(
 | 
			
		||||
				fetch(paginated_url).then((res) =>
 | 
			
		||||
					res.json().then((json) => json.results),
 | 
			
		||||
				),
 | 
			
		||||
			);
 | 
			
		||||
		}
 | 
			
		||||
		results.push(...(await Promise.all(promises)).flat());
 | 
			
		||||
	}
 | 
			
		||||
	return results;
 | 
			
		||||
  if (nb_pages > 1) {
 | 
			
		||||
    const promises = [];
 | 
			
		||||
    for (let i = 2; i <= nb_pages; i++) {
 | 
			
		||||
      paginated_url.searchParams.set("page", i.toString());
 | 
			
		||||
      promises.push(
 | 
			
		||||
        fetch(paginated_url).then((res) => res.json().then((json) => json.results)),
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
    results.push(...(await Promise.all(promises)).flat());
 | 
			
		||||
  }
 | 
			
		||||
  return results;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -158,14 +158,14 @@
 | 
			
		||||
 * @param {Select2Options} options
 | 
			
		||||
 */
 | 
			
		||||
function sithSelect2(options) {
 | 
			
		||||
	const elem = $(options.element);
 | 
			
		||||
	return elem.select2({
 | 
			
		||||
		theme: elem[0].multiple ? "classic" : "default",
 | 
			
		||||
		minimumInputLength: 2,
 | 
			
		||||
		templateResult: select_item_builder(options.picture_getter),
 | 
			
		||||
		...options.data_source,
 | 
			
		||||
		...(options.overrides || {}),
 | 
			
		||||
	});
 | 
			
		||||
  const elem = $(options.element);
 | 
			
		||||
  return elem.select2({
 | 
			
		||||
    theme: elem[0].multiple ? "classic" : "default",
 | 
			
		||||
    minimumInputLength: 2,
 | 
			
		||||
    templateResult: select_item_builder(options.picture_getter),
 | 
			
		||||
    ...options.data_source,
 | 
			
		||||
    ...(options.overrides || {}),
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -180,11 +180,11 @@ function sithSelect2(options) {
 | 
			
		||||
 * @param {RemoteSourceOptions} options
 | 
			
		||||
 */
 | 
			
		||||
function local_data_source(source, options) {
 | 
			
		||||
	if (options.excluded) {
 | 
			
		||||
		const ids = options.excluded();
 | 
			
		||||
		return { data: source.filter((i) => !ids.includes(i.id)) };
 | 
			
		||||
	}
 | 
			
		||||
	return { data: source };
 | 
			
		||||
  if (options.excluded) {
 | 
			
		||||
    const ids = options.excluded();
 | 
			
		||||
    return { data: source.filter((i) => !ids.includes(i.id)) };
 | 
			
		||||
  }
 | 
			
		||||
  return { data: source };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -203,37 +203,37 @@ function local_data_source(source, options) {
 | 
			
		||||
 * @param {RemoteSourceOptions} options
 | 
			
		||||
 */
 | 
			
		||||
function remote_data_source(source, options) {
 | 
			
		||||
	jQuery.ajaxSettings.traditional = true;
 | 
			
		||||
	const params = {
 | 
			
		||||
		url: source,
 | 
			
		||||
		dataType: "json",
 | 
			
		||||
		cache: true,
 | 
			
		||||
		delay: 250,
 | 
			
		||||
		data: function (params) {
 | 
			
		||||
			return {
 | 
			
		||||
				search: params.term,
 | 
			
		||||
				exclude: [
 | 
			
		||||
					...(this.val() || []).map((i) => Number.parseInt(i)),
 | 
			
		||||
					...(options.excluded ? options.excluded() : []),
 | 
			
		||||
				],
 | 
			
		||||
			};
 | 
			
		||||
		},
 | 
			
		||||
	};
 | 
			
		||||
	if (options.result_converter) {
 | 
			
		||||
		params.processResults = (data) => ({
 | 
			
		||||
			results: data.results.map(options.result_converter),
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
	if (options.overrides) {
 | 
			
		||||
		Object.assign(params, options.overrides);
 | 
			
		||||
	}
 | 
			
		||||
	return { ajax: params };
 | 
			
		||||
  jQuery.ajaxSettings.traditional = true;
 | 
			
		||||
  const params = {
 | 
			
		||||
    url: source,
 | 
			
		||||
    dataType: "json",
 | 
			
		||||
    cache: true,
 | 
			
		||||
    delay: 250,
 | 
			
		||||
    data: function (params) {
 | 
			
		||||
      return {
 | 
			
		||||
        search: params.term,
 | 
			
		||||
        exclude: [
 | 
			
		||||
          ...(this.val() || []).map((i) => Number.parseInt(i)),
 | 
			
		||||
          ...(options.excluded ? options.excluded() : []),
 | 
			
		||||
        ],
 | 
			
		||||
      };
 | 
			
		||||
    },
 | 
			
		||||
  };
 | 
			
		||||
  if (options.result_converter) {
 | 
			
		||||
    params.processResults = (data) => ({
 | 
			
		||||
      results: data.results.map(options.result_converter),
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
  if (options.overrides) {
 | 
			
		||||
    Object.assign(params, options.overrides);
 | 
			
		||||
  }
 | 
			
		||||
  return { ajax: params };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function item_formatter(user) {
 | 
			
		||||
	if (user.loading) {
 | 
			
		||||
		return user.text;
 | 
			
		||||
	}
 | 
			
		||||
  if (user.loading) {
 | 
			
		||||
    return user.text;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -242,20 +242,19 @@ function item_formatter(user) {
 | 
			
		||||
 * @return {function(string): jQuery|HTMLElement}
 | 
			
		||||
 */
 | 
			
		||||
function select_item_builder(picture_getter) {
 | 
			
		||||
	return (item) => {
 | 
			
		||||
		const picture =
 | 
			
		||||
			typeof picture_getter === "function" ? picture_getter(item) : null;
 | 
			
		||||
		const img_html = picture
 | 
			
		||||
			? `<img 
 | 
			
		||||
  return (item) => {
 | 
			
		||||
    const picture = typeof picture_getter === "function" ? picture_getter(item) : null;
 | 
			
		||||
    const img_html = picture
 | 
			
		||||
      ? `<img 
 | 
			
		||||
          src="${picture_getter(item)}" 
 | 
			
		||||
          alt="${item.text}" 
 | 
			
		||||
          onerror="this.src = '/static/core/img/unknown.jpg'" 
 | 
			
		||||
        />`
 | 
			
		||||
			: "";
 | 
			
		||||
      : "";
 | 
			
		||||
 | 
			
		||||
		return $(`<div class="select-item">
 | 
			
		||||
    return $(`<div class="select-item">
 | 
			
		||||
        ${img_html}
 | 
			
		||||
         <span class="select-item-text">${item.text}</span>
 | 
			
		||||
         </div>`);
 | 
			
		||||
	};
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user