Advertisement
Guest User

Untitled

a guest
May 9th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <style>
  5.   html, body { font-size: 16px; }
  6.   .frogPond { background-color: #0071b9; margin: 1em; padding: 1em; border: 0.125em solid aqua; border-radius: 0.5em; }
  7.   .frogPond h1 { text-align: center; }
  8.   .frogPond .actions { margin: 1em 0 0 0; padding: 0; }
  9.   .frogPond .actions button { display: inline-block; margin: 0 0.75em 0 0; background-color: aqua; border: 0.125em solid darkblue; padding: 0.25em; border-radius: 0.25em; font-size: 0.75em; cursor: pointer; }
  10.   .frogPond .frogs { border: 0.25em solid aqua; border-radius: 0.5em; margin: 0; padding: 0; }
  11.   .frogPond .frogs li { margin: 1em; padding: 0.25em; list-style: none; font-weight: bold; font-size: 1em; border-radius: 0.25em; }
  12.   .frogPond .m { color: darkblue; background-color: #1182ca; }
  13.   .frogPond .f { color: hotpink; background-color: #ffcccc; }
  14. </style>
  15.  
  16. <body>
  17. <script>
  18. var performActions = function(e) { // Helper function for clicking buttons
  19.         switch(e.target.className) {
  20.           case 'add':
  21.             console.log('you hit add');
  22.             frogPond.add(new Frog({ name: "Jack Sparrow", gender: "m" }));
  23.             break;
  24.            
  25.           case 'kill':
  26.             console.log('you hit kill');
  27.             frogPond.killFrog();
  28.             break;
  29.            
  30.           case 'breed':
  31.             console.log('you hit breed');
  32.             break;
  33.         }
  34.       },
  35.    
  36.     Pond = function() { this.create(); };
  37.     Pond.prototype = function() {
  38.       var _body,
  39.           _frogs,
  40.           _actions, /* DOM element holders */
  41.  
  42.           _pond,    /* reference to self */
  43.           Frogs,    /* Frogs in Pond */
  44.          
  45.           create = function() {
  46.             /* Set up DOM */
  47.             _body = document.createElement('div');
  48.             _body.className = 'frogPond';
  49.             _body.innerHTML = '<ul class="frogs"></ul><div class="actions"><button class="add">Add a Frog</button><button class="kill">Kill a frog</button><button class="breed">Encourage breeding</button></div>';
  50.             document.body.appendChild(_body);
  51.             _frogs = _body.getElementsByClassName('frogs')[0];
  52.             _actions = _body.getElementsByClassName('actions')[0];
  53.             /* End DOM Setup */
  54.  
  55.             this.Frogs = [];
  56.             _pond = this;
  57.            
  58.             _actions.addEventListener('click', function(e) { performActions.call(_pond, e); }, false);
  59.             /* perform an action on this pond only */
  60.           },
  61.          
  62.           add = function(frogs) { /* accepts array of frogs or single frog */
  63.             if(frogs) {
  64.               if(!frogs.propertyIsEnumerable(length) && typeof frogs.splice !== "function") {
  65.                 frogs = [frogs];
  66.               }
  67.             }
  68.  
  69.             var i = 0,
  70.                 l = frogs.length;
  71.              
  72.             for(; i < l; i++) {
  73.               this.addFrog(frogs[i]);
  74.             }
  75.           },
  76.  
  77.           killFrog = function(){
  78.               var _randomFrog = this.Frogs[Math.floor(Math.random()*this.Frogs.length)];
  79.               console.log(_randomFrog);
  80.               var i = this.Frogs.indexOf(_randomFrog);
  81.               this.Frogs.splice(i,1); //Remove frog from array
  82.               console.log(_frogs.childNodes);
  83.               console.log("ID of FROG to be deleted" + _randomFrog.getId());
  84.               for (var i = 0; i < _frogs.childNodes.length; i++) {
  85.  
  86.                 console.log("Data attribute of Frog : " + _frogs.childNodes[i].getAttribute("data-frogid"));
  87.                  if (_randomFrog.getId() == _frogs.childNodes[i].getAttribute("data-frogid")){
  88.                     console.log("ID : " + _randomFrog.getId() + " NAME: " + _randomFrog.name + " To be removed");
  89.                     // _frogs.removeChild(_frogs.childNodes[i]);
  90.                     _frogs.childNodes[i].parentNode.removeChild(_frogs.childNodes[i]);
  91.                  }
  92.               };
  93.               console.log(this.Frogs.length + "Frogs left in the array");
  94.           },
  95.          
  96.           addFrog = function(frog) {
  97.             /* only frogs allowed in this pond, this is a speciesist pond */
  98.             if(frog instanceof Frog) {
  99.               frog.setId(this.Frogs[this.Frogs.length - 1] ? this.Frogs[this.Frogs.length - 1].getId() + 1 : 1);
  100.               this.Frogs.push(frog);
  101.              
  102.               var _el = document.createElement('li');
  103.               _el.innerText = frog.name;
  104.               _el.className = frog.gender;
  105.               _el.setAttribute('data-frogid', frog.getId());
  106.               _frogs.appendChild(_el);
  107.             }
  108.           };
  109.      
  110.       return { create: create, add: add, addFrog: addFrog, killFrog : killFrog };
  111.     }(),
  112.  
  113.     Frog = function(data) { this.birth(data); };
  114.     Frog.prototype = function() {
  115.       var _id,
  116.           name,
  117.           gender,
  118.          
  119.           birth = function(frogData) {
  120.             this.name = frogData.name;
  121.             this.gender = frogData.gender;
  122.           },
  123.          
  124.           setId = function(id) {
  125.             _id = parseInt(id);
  126.           },
  127.  
  128.           getId = function() {
  129.             return _id;
  130.           };
  131.      
  132.       return { birth: birth, setId: setId, getId: getId };
  133.     }();
  134.  
  135. var frogPond = new Pond();
  136. frogPond.add([
  137.   new Frog({ name: "Frank", gender: "m" }),
  138.   new Frog({ name: "Georgia", gender: "f" })
  139. ]);
  140. </script>
  141.  
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement