From b66a47ef6718113df0dbd0040b15cfc436fc2ed5 Mon Sep 17 00:00:00 2001
From: Antonin <zepcome@gmail.com>
Date: Thu, 26 Mar 2015 16:33:11 +0100
Subject: [PATCH] Added simple javascript check for the name.

	- The goal here is to prevent the losing of the choices by having a bad name.
	- This is a POC, there is probably some cleaning before merge
---
 adminstuds.php                  |  2 ++
 js/app/studs.js                 | 21 +++++++++++++++++++++
 studs.php                       |  2 ++
 tpl/head.tpl                    |  2 ++
 tpl/part/vote_table_classic.tpl |  2 +-
 tpl/part/vote_table_date.tpl    |  2 +-
 tpl/studs.tpl                   | 18 +++++++++++++++---
 7 files changed, 44 insertions(+), 5 deletions(-)
 create mode 100644 js/app/studs.js

diff --git a/adminstuds.php b/adminstuds.php
index a26bbd22..e613d517 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -373,5 +373,7 @@ $smarty->assign('comments', $comments);
 $smarty->assign('editingVoteId', $editingVoteId);
 $smarty->assign('message', $message);
 $smarty->assign('admin', true);
+$smarty->assign('parameter_name_regex', NAME_REGEX);
+$smarty->assign('parameter_name_error', _('Name is incorrect.'));
 
 $smarty->display('studs.tpl');
\ No newline at end of file
diff --git a/js/app/studs.js b/js/app/studs.js
new file mode 100644
index 00000000..7a32203a
--- /dev/null
+++ b/js/app/studs.js
@@ -0,0 +1,21 @@
+$(document).ready(function() {
+
+    $("#poll_form").submit(function( event ) {
+        var name = $("#name").val();
+        var regexContent = $("#parameter_name_regex").text().split("/");
+        var regex = new RegExp(regexContent[1], regexContent[2]);
+        if (name.length == 0 || !regex.test(name)) {
+            event.preventDefault();
+            var errorMessage = $("#parameter_name_error").text();
+            var addedDiv = "<div class='alert alert-dismissible alert-danger' role='alert'>";
+            addedDiv += errorMessage;
+            addedDiv += "<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button></div>";
+            $("#message-container").empty();
+            $("#message-container").append(addedDiv);
+            $('html, body').animate({
+                scrollTop: $("#message-container").offset().top
+            }, 750);
+        }
+    });
+
+});
\ No newline at end of file
diff --git a/studs.php b/studs.php
index 281fdc97..1bfbb525 100644
--- a/studs.php
+++ b/studs.php
@@ -202,5 +202,7 @@ $smarty->assign('comments', $comments);
 $smarty->assign('editingVoteId', $editingVoteId);
 $smarty->assign('message', $message);
 $smarty->assign('admin', false);
+$smarty->assign('parameter_name_regex', NAME_REGEX);
+$smarty->assign('parameter_name_error', _('Name is incorrect.'));
 
 $smarty->display('studs.tpl');
diff --git a/tpl/head.tpl b/tpl/head.tpl
index 3b8bbaca..401a271a 100644
--- a/tpl/head.tpl
+++ b/tpl/head.tpl
@@ -24,6 +24,8 @@
             <script src="{'nav/nav.js'|resource}" id="nav_js" type="text/javascript" charset="utf-8"></script><!-- /Framanav -->
         {/if}
 
+        {block name="header"}{/block}
+
     </head>
     <body>
     <div class="container ombre">
\ No newline at end of file
diff --git a/tpl/part/vote_table_classic.tpl b/tpl/part/vote_table_classic.tpl
index e36559d3..ab0a6524 100644
--- a/tpl/part/vote_table_classic.tpl
+++ b/tpl/part/vote_table_classic.tpl
@@ -5,7 +5,7 @@
 <h3>{__('Poll results\\Votes of the poll')}</h3>
 
 <div id="tableContainer" class="tableContainer">
-    <form action="" method="POST">
+    <form action="" method="POST"  id="poll_form">
         <table class="results">
             <caption class="sr-only">{__('Poll results\\Votes of the poll')} {$poll->title|html}</caption>
             <thead>
diff --git a/tpl/part/vote_table_date.tpl b/tpl/part/vote_table_date.tpl
index ab00563c..2e8416dd 100644
--- a/tpl/part/vote_table_date.tpl
+++ b/tpl/part/vote_table_date.tpl
@@ -5,7 +5,7 @@
 <h3>{__('Poll results\\Votes of the poll')}</h3>
 
 <div id="tableContainer" class="tableContainer">
-    <form action="" method="POST">
+    <form action="" method="POST" id="poll_form">
         <table class="results">
             <caption class="sr-only">{__('Poll results\\Votes of the poll')} {$poll->title|html}</caption>
             <thead>
diff --git a/tpl/studs.tpl b/tpl/studs.tpl
index 9f9613e5..44a7ce84 100644
--- a/tpl/studs.tpl
+++ b/tpl/studs.tpl
@@ -1,10 +1,16 @@
 {extends file='page.tpl'}
 
+{block name="header"}
+    <script src="{"js/app/studs.js"|resource}" type="text/javascript"></script>
+{/block}
+
 {block name=main}
 
-    {if !empty($message)}
-        <div class="alert alert-dismissible alert-{$message->type|html}" role="alert">{$message->message|html}<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>
-    {/if}
+    <div id="message-container">
+        {if !empty($message)}
+            <div class="alert alert-dismissible alert-{$message->type|html}" role="alert">{$message->message|html}<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>
+        {/if}
+    </div>
 
 {* Global informations about the current poll *}
 
@@ -37,6 +43,12 @@
     </div>
 </div>
 
+<div class="hidden">
+    <p id="parameter_name_regex">{$parameter_name_regex}</p>
+    <p id="parameter_name_error">{$parameter_name_error}</p>
+</div>
+
+
 {* Vote table *}
 
 {if $poll->format === 'D'}
-- 
GitLab