Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Mediator Pattern Demo</h1>
<button id="addEmployee">新增員工</button>
<div id="app"></div>
              
            
!

CSS

              
                button {
  margin-bottom: 10px;
}

              
            
!

JS

              
                const orgChart = {
  employees: [],
  addNewEmployee() {
    // 提供使用者要互動的視圖
    const employeeDetail = this.getEmployeeDetail();
    // 當員工詳細資料完成後,調解者決定接下來要發生的事
    employeeDetail.on("complete", (employee) => {
      const managerSelector = this.selectManager(employee);
      managerSelector.on("save", (employee) => {
        // 設定具有附加事件的附加物件
        // 調解者使用這些事件執行附加操作
        employee.save();
        this.employees.push(employee);
        console.log("Employee saved:", employee);
      });
    });
  },

  getEmployeeDetail() {
    const detailContainer = document.createElement("div");
    const nameInput = document.createElement("input");
    nameInput.placeholder = "員工姓名";
    detailContainer.appendChild(nameInput);
    const roleInput = document.createElement("input");
    roleInput.placeholder = "員工職稱";
    detailContainer.appendChild(roleInput);
    const submitBtn = document.createElement("button");
    submitBtn.textContent = "提交";
    detailContainer.appendChild(submitBtn);

    const eventHandlers = {};

    submitBtn.addEventListener("click", () => {
      const employee = new Employee(nameInput.value, roleInput.value);
      if (eventHandlers["complete"]) {
        eventHandlers["complete"](employee);
      }
    });

    document.getElementById("app").appendChild(detailContainer);

    return {
      on(event, callback) {
        eventHandlers[event] = callback;
      }
    };
  },

  selectManager(employee) {
    const managerContainer = document.createElement("div");
    const managerInput = document.createElement("input");
    managerInput.placeholder = "管理者姓名";
    managerContainer.appendChild(managerInput);
    const saveBtn = document.createElement("button");
    saveBtn.textContent = "儲存";
    managerContainer.appendChild(saveBtn);

    const eventHandlers = {};

    saveBtn.addEventListener("click", () => {
      employee.manager = managerInput.value;
      if (eventHandlers["save"]) {
        eventHandlers["save"](employee);
      }
    });

    document.getElementById("app").appendChild(managerContainer);

    return {
      on(event, callback) {
        eventHandlers[event] = callback;
      }
    };
  }
};

class Employee {
  constructor(name, role) {
    this.name = name;
    this.role = role;
    this.manager = null;
  }

  save() {
    alert(
      `${this.name} (${this.role}) saved with manager ${this.manager}`
    );
  }
}

document.getElementById("addEmployee").addEventListener("click", () => {
  orgChart.addNewEmployee();
});

              
            
!
999px

Console