Sha256: c29b6aace2817b2dbede081f58b526bd7ee9bf68d1e68b8ddc5f790bb5369507

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

import { Controller } from "@hotwired/stimulus";

export class SlugController extends Controller {
  static targets = [
    "existing_root",
    "input_select",
    "input_text",
    "output_text",
  ];

  connect() {
    console.debug("[Panda CMS] Slug handler connected...");
  }

  generatePath() {
    this.output_textTarget.value = "/" + this.createSlug(this.input_textTarget.value);
    console.log("Have set the path to: " + this.output_textTarget.value);
  }

  setPrePath() {
    this.parent_slugs = this.input_selectTarget.options[this.input_selectTarget.selectedIndex].text.match(/.*\((.*)\)$/)[1];
    this.output_textTarget.previousSibling.innerHTML = (this.existing_rootTarget.value + this.parent_slugs).replace(/\/$/, "");
    console.log("Have set the pre-path to: " + this.output_textTarget.previousSibling.innerHTML);
  }

  // TODO: Invoke a library or helper which can be shared with the backend
  // and check for uniqueness at the same time
  createSlug(input) {
    var str = input
      .toLowerCase()
      .trim()
      .replace(/[^\w\s-]/g, "-")
      .replace(/&/g, "and")
      .replace(/[\s_-]+/g, "-")
      .trim();

    return this.trimStartEnd(str, "-");
  }

  trimStartEnd(str, ch) {
    var start = 0;
    var end = str.length;

    while (start < end && str[start] === ch) ++start;
    while (end > start && str[end - 1] === ch) --end;
    return start > 0 || end < str.length ? str.substring(start, end) : str;
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
panda_cms-0.6.3 app/javascript/panda_cms/controllers/slug_controller.js
panda_cms-0.6.2 app/javascript/panda_cms/controllers/slug_controller.js
panda_cms-0.6.1 app/javascript/panda_cms/controllers/slug_controller.js
panda_cms-0.6.0 app/javascript/panda_cms/controllers/slug_controller.js