/**
 * Ampbin class
 *
 * Controller
 */
export class Ampbin {
  /**
   * Ampbin construct
   * 
   * @param  {Database} database Dependency Injection of Database class
   */
  constructor(database) {
    this.database = database;
    this.save(true);
  }

  /**
   * Save the current bin
   */
  save(newBin = false) {
    let binText = this.getById('copy').value;

    let obj = {
      'bin': binText,
      'timestamp': Date.now(),
      'newbin': newBin
    };

    this.bin = this.database.push(obj);
  }

  update() {
    let binText = this.getById('copy').value;

    let obj = {
      'bin': binText,
      'timestamp': Date.now(),
      'newbin': false
    };

    this.bin.update(obj);
  }

  /**
   * Listen for a click on the save button
   * 
   * @param {String} saveId ID of the Save button
   */
  addSaveHandler(saveId = 'save') {
    let saveEl = this.getById(saveId);
    saveEl.onclick = () => this.update();
  }

  /**
   * Get element by ID
   * @param  {String}  elId ID of the element
   * @return {Element}      Element Object
   */
  getById(elId) {
    return document.getElementById(elId);
  }

}

@@ -224,16 +224,64 @@
 tabase;%0A
+%0A    if(window.location.hash) %7B%0A%0A    %7D else %7B%0A  
     this
@@ -293,16 +293,23 @@
 (true);%0A
+    %7D%0A%0A
   %7D%0A%0A  /
@@ -550,24 +550,132 @@
 e.push(obj);
+%0A%0A    window.history.replaceState(null, null, '#' + this.bin.getKey());%0A%0A    console.log('new bin created');
 %0A  %7D%0A%0A  upda

append entry id as hash to url (#2)