Première version qui marche
This commit is contained in:
79
src/lib.rs
Normal file
79
src/lib.rs
Normal 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
10
src/utils.rs
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user