Advertisement
Guest User

lel

a guest
May 25th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var colorList = [];
  2.  
  3. function initialize() {
  4.     var sliders = document.getElementsByClassName("slider");
  5.     var selectButton = document.getElementById("selectButton");
  6.     var i=0;
  7.     for (i = 0; i < sliders.length; i++) {
  8.         sliders[i].addEventListener("change", update);
  9.     }
  10.     selectButton.addEventListener("click", selectClick);
  11.     update();
  12. }
  13.  
  14. function update() {
  15.     var red=document.getElementById("sldRed").value;
  16.     var green=document.getElementById("sldGreen").value;
  17.     var blue=document.getElementById("sldBlue").value;
  18.     document.getElementById("lblRed").innerHTML=red;
  19.     document.getElementById("lblGreen").innerHTML=green;
  20.     document.getElementById("lblBlue").innerHTML=blue;
  21.     var swatch=document.getElementById("swatch");
  22.     swatch.style.background="rgb("+red+","+green+","+blue+")";
  23. }
  24.  
  25. function selectClick(){
  26.     //Get current Color
  27.     var color = getColor();
  28.    
  29.     //Add Color to the list
  30.     addColor(color);
  31.    
  32.     //Write the list to UI
  33.     showColors();
  34. }
  35.  
  36. function getColor()
  37. {
  38.     var red=document.getElementById("sldRed").value;
  39.     var green=document.getElementById("sldGreen").value;
  40.     var blue=document.getElementById("sldBlue").value;
  41.     return "rgb("+red+","+green+","+blue+")";
  42. }
  43.  
  44. function addColor(color){
  45.     colorList.push(color); 
  46. }
  47.  
  48. function removeColor(index){
  49.     colorList.splice(index, 1);
  50. }
  51.  
  52. function showColors(){
  53.     //get the block for the list
  54.     var colorListBlock=document.getElementById("colorListBlock");
  55.    
  56.     //Remove the current items
  57.     //colorListBlock.innerHTML = "";
  58.     //Dit is veel sneller:
  59.     while(colorListBlock.firstChild)
  60.     {
  61.         colorListBlock.removeChild(colorListBlock.firstChild);
  62.     }
  63.    
  64.     var i = 0;
  65.     //Add them to the UI
  66.     for(i = 0; i<colorList.length; i++)
  67.     {
  68.         colorListBlock.appendChild(createColorItem(i));
  69.     }
  70. }
  71.  
  72. function createColorItem(index)
  73. {
  74.     var color = colorList[index];
  75.     var parentDiv = createParentDiv();
  76.     var colorDiv = createColorDiv(color);
  77.     var textDiv = createTextDiv(color);
  78.     var deleteButton = createDeleteButton(index);
  79.        
  80.     parentDiv.appendChild(colorDiv);
  81.     parentDiv.appendChild(textDiv);
  82.     textDiv.appendChild(deleteButton);
  83.    
  84.     return parentDiv
  85. }
  86.  
  87. function createParentDiv(){
  88.     var parentDiv = document.createElement("div");
  89.     parentDiv.style.clear = "both";
  90.     return parentDiv;
  91. }
  92.  
  93. function createColorDiv(color){
  94.     var colorDiv = document.createElement("div");
  95.     colorDiv.style.width = "50px";
  96.     colorDiv.style.height = "50px";
  97.     colorDiv.style.margin = "5px";
  98.     colorDiv.style.border = "solid 1px #000000";
  99.     colorDiv.style.cssFloat = "left";
  100.     colorDiv.style.backgroundColor = color;
  101.     return colorDiv;
  102. }
  103.  
  104. function createTextDiv(color)
  105. {
  106.     var textDiv = document.createElement("div");
  107.     textDiv.innerHTML = color; 
  108.     textDiv.style.cssFloat = "left";
  109.     return textDiv;
  110. }
  111.  
  112. function createDeleteButton(index){
  113.     var deleteButton = document.createElement("input");
  114.     deleteButton.type = "button";
  115.     deleteButton.value = "Delete Color";
  116.     deleteButton.id = "deleteButton" + index;
  117.     deleteButton.addEventListener("click", deleteColorItem);
  118.     return deleteButton;
  119. }
  120.  
  121. function deleteColorItem(e){
  122.     //Get the event Source Element
  123.     var button = e.srcElement;
  124.     //Get the color index from the button Id
  125.     var indexText = button.id.substring(12, button.id.length)
  126.     var index = parseInt(indexText);
  127.    
  128.     //Remove color from index
  129.     removeColor(index);
  130.    
  131.     showColors();
  132. }
  133.  
  134. window.addEventListener("load", initialize, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement