Advertisement
Omegad

Untitled

Dec 12th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 7.53 KB | None | 0 0
  1. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  2. <link rel="stylesheet" href="/resources/demos/style.css">
  3. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  4. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  5.  
  6.   <% @y = [] %>
  7.   <% User.all.each do |user| %>
  8.   <% x = user.username %><br>
  9.   <% if x %>
  10.   <% @y << "@" + x %>
  11.   <% end %>
  12.   <% end %>
  13.   <% @y %>
  14. <style type="text/css">
  15.  
  16. /*the container must be positioned relative:*/
  17. .autocomplete {
  18.   position: relative;
  19.   display: inline-block;
  20. }
  21.  
  22. input {
  23.   border: 1px solid transparent;
  24.   background-color: #f1f1f1;
  25.   padding: 10px;
  26.   font-size: 16px;
  27. }
  28.  
  29. input[type=text] {
  30.   background-color: #f1f1f1;
  31.   width: 100%;
  32. }
  33.  
  34. .autocomplete-items {
  35.   position: absolute;
  36.   border: 1px solid #d4d4d4;
  37.   border-bottom: none;
  38.   border-top: none;
  39.   z-index: 99;
  40.   /*position the autocomplete items to be the same width as the container:*/
  41.   top: 100%;
  42.   left: 0;
  43.   right: 0;
  44. }
  45.  
  46. .autocomplete-items div {
  47.   padding: 10px;
  48.   cursor: pointer;
  49.   background-color: #fff;
  50.   border-bottom: 1px solid #d4d4d4;
  51. }
  52.  
  53. /*when hovering an item:*/
  54. .autocomplete-items div:hover {
  55.   background-color: #e9e9e9;
  56. }
  57.  
  58. /*when navigating through the items using the arrow keys:*/
  59. .autocomplete-active {
  60.   background-color: DodgerBlue !important;
  61.   color: #ffffff;
  62. }
  63. </style>
  64. <p id="notice"><%= notice %></p>
  65.  
  66. <div class="course-show-container">
  67.   <div class="course-show-main">
  68.     <div class="course-show-head">
  69.       <p>
  70.         <h1>
  71.         <%= @course.name %>
  72.         </h1>
  73.  
  74.         <h6>- <%= rand(1..50) %> upvotes</h6>
  75.       </p>
  76.  
  77.       <p>
  78.        
  79.         <%= @course.tagline %>
  80.       </p>
  81.  
  82.        <p>
  83.         Hunted by:
  84.         <%= User.find(@course.user_id).id %>
  85.       </p>
  86.  
  87.       <div class="course-show-url-card">
  88.        
  89.         <div class="course-show-url-img">
  90.          <!-- <img src="http://unsplash.it/300/200" class="course-url-image"> -->
  91.         </div>
  92.         <div class="course-show-url-bottom">
  93.           <div class="course-show-url-button">
  94.             <p> Visit Website </p>
  95.             <a href="#{@course.url}"> <%= @course.name %> </a>
  96.           </div>
  97.           <div class="course-show-url-infos">
  98.             <p></p>
  99.           </div>
  100.         </div>
  101.       </div>
  102.  
  103.      
  104.  
  105.       <p>
  106.         <strong>Category:</strong>
  107.         <%= @course.category %>
  108.       </p>
  109.  
  110.       <p>
  111.         <strong>Content:</strong>
  112.         <%= @course.content.html_safe %>
  113.       </p>
  114.  
  115.       <p>
  116.         <strong>Note:</strong>
  117.         <%= @course.note %>
  118.       </p>
  119.  
  120.       <h3>Comments</h3>
  121.  
  122.       <%= form_for [@course, Comment.new], autocomplete: "off" do |f| %>
  123.       <div class="autocomplete">
  124.       <%= f.text_area :content, placeholder: "Add a comment", id: "myInput" %><br/>
  125.       </div>
  126.       <%= f.submit "Add Comment" %>
  127.       <% end %>
  128.  
  129.       <ul>
  130.         <%= render(partial: 'comments/comment', collection: @course.comments) %>
  131.       </ul>
  132.  
  133.  
  134.  
  135.       <%= link_to 'Edit', edit_course_path(@course) if policy(@course).edit?  %>
  136.       <%= link_to 'Back', courses_path %>
  137.     </div>
  138.   </div>
  139. </div>
  140.  
  141. <script>
  142. function autocomplete(inp, arr) {
  143.   /*the autocomplete function takes two arguments,
  144.   the text field element and an array of possible autocompleted values:*/
  145.   var currentFocus;
  146.   /*execute a function when someone writes in the text field:*/
  147.   inp.addEventListener("input", function(e) {
  148.       var a, b, i, val = this.value;
  149.       /*close any already open lists of autocompleted values*/
  150.       closeAllLists();
  151.       if (!val) { return false;}
  152.       currentFocus = -1;
  153.       /*create a DIV element that will contain the items (values):*/
  154.       a = document.createElement("DIV");
  155.       a.setAttribute("id", this.id + "autocomplete-list");
  156.       a.setAttribute("class", "autocomplete-items");
  157.       /*append the DIV element as a child of the autocomplete container:*/
  158.       this.parentNode.appendChild(a);
  159.       /*for each item in the array...*/
  160.       for (i = 0; i < arr.length; i++) {
  161.        /*check if the item starts with the same letters as the text field value:*/
  162.        if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
  163.          /*create a DIV element for each matching element:*/
  164.          b = document.createElement("DIV");
  165.          /*make the matching letters bold:*/
  166.          b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
  167.           b.innerHTML += arr[i].substr(val.length);
  168.           /*insert a input field that will hold the current array item's value:*/
  169.           b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
  170.           /*execute a function when someone clicks on the item value (DIV element):*/
  171.           b.addEventListener("click", function(e) {
  172.               /*insert the value for the autocomplete text field:*/
  173.               inp.value = this.getElementsByTagName("input")[0].value;
  174.               /*close the list of autocompleted values,
  175.               (or any other open lists of autocompleted values:*/
  176.               closeAllLists();
  177.           });
  178.           a.appendChild(b);
  179.         }
  180.       }
  181.   });
  182.   /*execute a function presses a key on the keyboard:*/
  183.   inp.addEventListener("keydown", function(e) {
  184.       var x = document.getElementById(this.id + "autocomplete-list");
  185.       if (x) x = x.getElementsByTagName("div");
  186.       if (e.keyCode == 40) {
  187.         /*If the arrow DOWN key is pressed,
  188.         increase the currentFocus variable:*/
  189.         currentFocus++;
  190.         /*and and make the current item more visible:*/
  191.         addActive(x);
  192.       } else if (e.keyCode == 38) { //up
  193.         /*If the arrow UP key is pressed,
  194.         decrease the currentFocus variable:*/
  195.         currentFocus--;
  196.         /*and and make the current item more visible:*/
  197.         addActive(x);
  198.       } else if (e.keyCode == 13) {
  199.         /*If the ENTER key is pressed, prevent the form from being submitted,*/
  200.         e.preventDefault();
  201.         if (currentFocus > -1) {
  202.           /*and simulate a click on the "active" item:*/
  203.           if (x) x[currentFocus].click();
  204.         }
  205.       }
  206.   });
  207.   function addActive(x) {
  208.     /*a function to classify an item as "active":*/
  209.     if (!x) return false;
  210.     /*start by removing the "active" class on all items:*/
  211.     removeActive(x);
  212.     if (currentFocus >= x.length) currentFocus = 0;
  213.     if (currentFocus < 0) currentFocus = (x.length - 1);
  214.    /*add class "autocomplete-active":*/
  215.    x[currentFocus].classList.add("autocomplete-active");
  216.  }
  217.  function removeActive(x) {
  218.    /*a function to remove the "active" class from all autocomplete items:*/
  219.    for (var i = 0; i < x.length; i++) {
  220.      x[i].classList.remove("autocomplete-active");
  221.    }
  222.  }
  223.  function closeAllLists(elmnt) {
  224.    /*close all autocomplete lists in the document,
  225.    except the one passed as an argument:*/
  226.    var x = document.getElementsByClassName("autocomplete-items");
  227.    for (var i = 0; i < x.length; i++) {
  228.      if (elmnt != x[i] && elmnt != inp) {
  229.        x[i].parentNode.removeChild(x[i]);
  230.      }
  231.    }
  232.  }
  233.  /*execute a function when someone clicks in the document:*/
  234.  document.addEventListener("click", function (e) {
  235.      closeAllLists(e.target);
  236.  });
  237. }
  238.  
  239. /*An array containing all the country names in the world:*/
  240. var countries = <%= raw @y %>;
  241.  
  242. /*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
  243. autocomplete(document.getElementById("myInput"), countries);
  244. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement