Première version qui marche

This commit is contained in:
2020-10-08 00:17:08 +02:00
parent 48651421a2
commit 55c2e4a0c3
11 changed files with 841 additions and 1 deletions

79
src/lib.rs Normal file
View File

@@ -0,0 +1,79 @@
mod utils;
use wasm_bindgen::prelude::*;
use web_sys;
use wasm_bindgen::JsCast;
use rand::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, kikikoz!");
}
#[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");
let prochain = document.get_element_by_id("prochain").expect("blu");
let queue_prio = document.get_element_by_id("queue_prio").expect("bla");
let queue_non_prio = document.get_element_by_id("queue_non_prio").expect("bla");
let lis_prio = queue_prio.get_elements_by_tag_name("li");
let queue = if lis_prio.length() == 0 {
queue_non_prio
} else {
queue_prio
};
let lis = queue.get_elements_by_tag_name("li");
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");
}
}
}
#[wasm_bindgen]
pub fn ajoute(id: &str) {
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
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::<web_sys::HtmlInputElement>().unwrap();
let content = input.value();
let ols = section.get_elements_by_tag_name("ol");
let ol = ols.item(0).unwrap().dyn_into::<web_sys::HtmlOListElement>().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 new_li = document.create_element("li").unwrap();
new_li.set_inner_html(&content);
if k == n {
ol.append_child(&new_li);
} else {
let li = lis.item(k).unwrap();
li.after_with_node_1(&new_li);
}
}

10
src/utils.rs Normal file
View File

@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}