From 6bea1ee4a10492804f8e21df20610b187eb674d4 Mon Sep 17 00:00:00 2001 From: Florent Becker Date: Thu, 17 Dec 2020 18:59:29 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9compte=20du=20temps=20de=20parole?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 + default.nix | 16 -------- index.html | 7 ++-- kikikoz.css | 2 +- rust-channel.nix | 15 ------- shell.nix | 14 +++---- src/lib.rs | 105 ++++++++++++++++++++++++++++++++--------------- 7 files changed, 86 insertions(+), 75 deletions(-) delete mode 100644 rust-channel.nix diff --git a/Cargo.toml b/Cargo.toml index 63eddac..ff24bf7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,8 @@ console_error_panic_hook = { version = "0.1.6", optional = true } # Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. wee_alloc = { version = "0.4.5", optional = true } +js-sys = "0.3" + [dependencies.web-sys] version = "0.3" features = [ diff --git a/default.nix b/default.nix index 03ac5f3..508abb9 100644 --- a/default.nix +++ b/default.nix @@ -1,22 +1,6 @@ { pkgs ? (import {}) }: -let - date = "2020-03-31"; -in -let - rust-channel = pkgs.callPackage ./rust-channel.nix { inherit date; }; - rustPlatform = pkgs.makeRustPlatform { - cargo = rust-channel.rust; - rustc = rust-channel.rust; - }; -in -let - buildRustCrate = pkgs.buildRustCrate.override { - cargo = rust-channel.rust; - rustc = rust-channel.rust;}; -in pkgs.callPackage ./Cargo.nix { - inherit buildRustCrate; defaultCrateOverrides = pkgs.defaultCrateOverrides // { lixiangyuan = attrs: { buildInputs = [ pkgs.wasm-pack ]; diff --git a/index.html b/index.html index 1595c25..7a21594 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ Kikikoz

Kikikoz

-

- Personne +

+ Personne

diff --git a/kikikoz.css b/kikikoz.css index 9ce1e4e..e56ffe0 100644 --- a/kikikoz.css +++ b/kikikoz.css @@ -2,7 +2,7 @@ body { background-color: aliceblue; } -p.prochain { +p.orateurice { font-size: 3rem; text-align: center; } diff --git a/rust-channel.nix b/rust-channel.nix deleted file mode 100644 index 19bda3f..0000000 --- a/rust-channel.nix +++ /dev/null @@ -1,15 +0,0 @@ -{ nixpkgs ? }: - -with (import nixpkgs {}); - -let - mozillaOverlay = fetchFromGitHub { - owner = "mozilla"; - repo = "nixpkgs-mozilla"; - rev = "4521bc61c2332f41e18664812a808294c8c78580"; - sha256 = "sha256:0lyzzfb6malnvnlvc2p5r895qxwp6p1h8vjvxbk0p8qk9cjl1j2p"; - }; - mozilla = callPackage "${mozillaOverlay.out}/package-set.nix" {}; -in -mozilla.latest.rustChannels.stable - diff --git a/shell.nix b/shell.nix index 71c5c58..383e36e 100644 --- a/shell.nix +++ b/shell.nix @@ -1,9 +1,10 @@ -{ pkgs ? (import {}) }: let - date = "2020-06-01"; -in -let - rust-channel = pkgs.callPackage ./rust-channel.nix { }; + sources = import ./nix/sources.nix; + nixpkgs-mozilla = import sources.nixpkgs-mozilla; + pkgs = import sources.nixpkgs { + overlays = [ nixpkgs-mozilla ]; + }; + rust-channel = pkgs.latest.rustChannels.stable; in pkgs.mkShell { buildInputs = [ @@ -17,9 +18,6 @@ pkgs.mkShell { pkgs.pkg-config pkgs.wasm-pack pkgs.cargo-generate - pkgs.sqlitebrowser - pkgs.sqlite - pkgs.diesel-cli pkgs.zlib ]; } diff --git a/src/lib.rs b/src/lib.rs index 1333b21..d4d1cb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ mod utils; -use wasm_bindgen::prelude::*; -use web_sys; -use wasm_bindgen::JsCast; +use js_sys::Date; use rand::prelude::*; +use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. @@ -12,45 +12,80 @@ use rand::prelude::*; static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; #[wasm_bindgen] -extern { +extern "C" { fn alert(s: &str); } #[wasm_bindgen] -pub fn next() { - let window = web_sys::window().expect("no global `window` exists"); - let document = window.document().expect("should have a document on window"); +pub fn intervalle_orateurice() { + let document = le_document(); + let now: f64 = Date::new_0().get_time(); + let prochain = document.get_element_by_id("orateurice").expect("blu"); + let compte = document.get_element_by_id("compte").expect("blaou"); + match js_sys::Reflect::get(&prochain, &JsValue::from("debut")) { + Err(_) => return, + Ok(date_debut) => { + let date_debut = date_debut.as_f64().expect("tut tut"); + let diff = ((now - date_debut) / 1000.0) as u64; + let sec = diff % 60; + let min = diff / 60; + compte.set_inner_html(&format!("{}:{:02}", min, sec)); + } + } +} - let prochain = document.get_element_by_id("prochain").expect("blu"); - let queue_prio = document.get_elements_by_class_name("courante").item(0).expect("bla"); - let queue_non_prio = document.get_elements_by_class_name("attente").item(0).expect("bla"); +fn le_document() -> web_sys::Document { + let window = web_sys::window().expect("no global `window` exists"); + window.document().expect("should have a document on window") +} + +#[wasm_bindgen] +pub fn next() { + let document = le_document(); + + let prochain = document.get_element_by_id("orateurice").expect("blu"); + let queue_prio = document + .get_elements_by_class_name("courante") + .item(0) + .expect("bla"); + let queue_non_prio = document + .get_elements_by_class_name("attente") + .item(0) + .expect("bla"); let lis_prio = queue_prio.get_elements_by_tag_name("li"); let queue = if lis_prio.length() == 0 { - &queue_non_prio + &queue_non_prio } else { - &queue_prio + &queue_prio }; let lis = queue.get_elements_by_tag_name("li"); queue_prio.set_class_name("queue attente"); queue_non_prio.set_class_name("queue courante"); match lis.item(0) { - Some(li) => { - let nom_prochain = li.text_content().expect(""); - prochain.set_text_content(Some(nom_prochain.as_str())); - li.parent_element().expect("").remove_child(&li); - prochain.set_class_name(""); - }, - None => { - prochain.set_text_content(Some("Personne")); - prochain.set_class_name("personne"); - } - } + Some(li) => { + let nom_prochain = li.text_content().expect(""); + let temps_debut = Date::new_0().get_time(); + prochain.set_text_content(Some(nom_prochain.as_str())); + js_sys::Reflect::set( + &prochain, + &JsValue::from("debut"), + &JsValue::from(temps_debut), + ); + li.parent_element().expect("").remove_child(&li); + prochain.set_class_name(""); + } + None => { + prochain.set_text_content(Some("Personne")); + prochain.set_class_name("personne"); + } + }; + intervalle_orateurice(); } #[wasm_bindgen] pub fn ajoute_kp(ev: web_sys::KeyboardEvent, id: &str) { if ev.key_code() == 13 { - ajoute(id) + ajoute(id) } } @@ -61,23 +96,29 @@ pub fn ajoute(id: &str) { let section = document.get_element_by_id(id).expect("bla"); let inputs = section.get_elements_by_tag_name("input"); - let input = inputs.item(0).unwrap().dyn_into::().unwrap(); + let input = inputs + .item(0) + .unwrap() + .dyn_into::() + .unwrap(); let content = input.value(); let ols = section.get_elements_by_tag_name("ol"); - let ol = ols.item(0).unwrap().dyn_into::().unwrap(); + let ol = ols + .item(0) + .unwrap() + .dyn_into::() + .unwrap(); let lis = ol.get_elements_by_tag_name("li"); let n = lis.length(); let mut rng = thread_rng(); - let k = rng.gen_range(0, n+1); + let k = rng.gen_range(0, n + 1); let new_li = document.create_element("li").unwrap(); new_li.set_inner_html(&content); if k == n { - ol.append_child(&new_li); + ol.append_child(&new_li); } else { - let li = lis.item(k).unwrap(); - li.after_with_node_1(&new_li); + let li = lis.item(k).unwrap(); + li.after_with_node_1(&new_li); } - - }