diff --git a/PHP/GamUsino.php b/PHP/GamUsino.php index b891c4c..d306157 100644 --- a/PHP/GamUsino.php +++ b/PHP/GamUsino.php @@ -32,13 +32,13 @@ break; case "add_option": $database = $this->load_database(); - if(isset($database["polls"]) && isset($database["polls"][$data["name"]])){ - $database["polls"][$data["name"]][] = [ + if(isset($database["polls"]) && isset($database["polls"][$data["i"]])){ + $database["polls"][$data["i"]]["options"][] = [ "text" => $data["text"], "points" => 0 ]; $this->save_database($database); - $this->response(200, $database["polls"][$data["name"]]); + $this->response(200, $database["polls"][$data["i"]]["options"]); }else $this->response(404, [ "message" => "poll_not_exists", diff --git a/Public/ecma/GamUsino.ecma.js b/Public/ecma/GamUsino.ecma.js index 5507dea..9e5defa 100644 --- a/Public/ecma/GamUsino.ecma.js +++ b/Public/ecma/GamUsino.ecma.js @@ -138,22 +138,6 @@ GamUsino = function(custom){ }; - this.add_poll = (item, event) => { - - const poll_name = item.parentNode.parentNode.querySelector("input").value; - - if(poll_name) - self.confirm("add_poll_sure", () => { - self.send({ - action : "add_poll", - name : poll_name - }, response => { - console.log(response); - }); - }); - - }; - this.preload = (selector, callback) => { if(!selector){ callback(null, false); @@ -209,23 +193,84 @@ GamUsino = function(custom){ }); }; - const build_polls = polls => document.querySelector(".polls-box>.polls").innerHTML = polls.map((poll, i) => self.string_variables(fragments.poll_menu_fragment, { - ...poll, - i : i - })).join(""); + const build_polls = polls => { + + const text = document.querySelector(".polls-box [type=text]").value.trim(); + + document.querySelector(".polls-box>.polls>ul").innerHTML = polls.map((poll, i) => self.string_variables(fragments.poll_menu_fragment, { + ...poll, + i : i, + visible : !text || poll.name.includes(text) + })).join(""); + + }; + + this.add_poll = (item, event) => { + + const poll_name = item.parentNode.parentNode.querySelector("input").value; + + poll_name && self.confirm("add_poll_sure", () => { + self.send({ + action : "add_poll", + name : poll_name + }, data => build_polls(data.content)); + }); + + }; this.show_poll = (item, event) => { + + const i = Number(item.getAttribute("data-i")); + document.querySelectorAll(".polls-box>.polls [data-seleted=true]").forEach(item => item.setAttribute("data-selected", false)); item.setAttribute("data-selected", true); + self.send({ action : "get_poll", - i : Number(item.getAttribute("data-i")) + i : i }, data => { console.log(data); - document.querySelector(".poll-box>.poll").innerHTML = data.content.name; + document.querySelector(".poll-box>.poll").innerHTML = self.string_variables(fragments.poll_box, { + ...data.content, + i : i, + maximum_points : data.content.options.length ? Math.max(...data.content.options.map(option => option.points)) : 0, + options : data.content.options.map((option, j) => self.string_variables(fragments.option_item, { + ...option, + i : j + })).join("") + }); + }); + + }; + + this.add_option = (item, event) => { + + const text = item.parentNode.parentNode.querySelector("textarea").value, + i = Number(document.querySelector(".polls-box>.polls [data-selected=true]").getAttribute("data-i")); + + !isNaN(i) && text && self.confirm("add_option_sure", () => { + self.send({ + action : "add_option", + i : i, + text : text + }, data => build_options(data.content)); }); }; + const build_options = options => { + + const text = document.querySelector(".poll-box textarea").value.trim(); + + document.querySelector(".poll-box>.poll .options>ul").innerHTML = options.map((option, k) => self.string_variables(fragments.option_item, { + ...option, + i : k, + visible : !text || option.text.includes(text) + })).join(""); + + }; + + this.select_option = (item, event) => {}; + constructor(); }; \ No newline at end of file diff --git a/Public/index.html b/Public/index.html index 6d14cf7..904d017 100644 --- a/Public/index.html +++ b/Public/index.html @@ -20,13 +20,37 @@
+ -
  • +
  • {name}
  • + + +
    +
    + {name} + +
    +
    + + + +
  • + {text} + + + + + +
  • + +

    @@ -55,15 +79,13 @@

    Encuesta

    - +
    - +