Class: Brick::Rails::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/brick/rails/engine.rb

Overview

Constant Summary collapse

JS_CHANGEOUT =
"function changeout(href, param, value, trimAfter) {
  var hrefParts = href.split(\"?\");
  var params = hrefParts.length > 1 ? hrefParts[1].split(\"&\") : [];
  if (param === undefined || param === null || param === -1) {
    hrefParts = hrefParts[0].split(\"://\");
    var pathParts = hrefParts[hrefParts.length - 1].split(\"/\").filter(function (pp) {return pp !== \"\";});
    if (value === undefined) {
      // A couple possibilities if it's namespaced, starting with two parts in the path -- and then try just one
      if (pathParts.length > 3)
        return [pathParts.slice(1, 4).join('/'), pathParts.slice(1, 3).join('/')];
      else
        return [pathParts.slice(1, 3).join('/'), pathParts[1]];
    } else {
      var queryString = param ? \"?\" + params.join(\"&\") : \"\";
      return hrefParts[0] + \"://\" + pathParts[0] + \"/\" + value + queryString;
    }
  }
  if (trimAfter) {
    var pathParts = hrefParts[0].split(\"/\");
    while (pathParts.lastIndexOf(trimAfter) !== pathParts.length - 1) pathParts.pop();
    hrefParts[0] = pathParts.join(\"/\");
  }
  params = params.reduce(function (s, v) { var parts = v.split(\"=\"); if (parts[1]) s[parts[0]] = parts[1]; return s; }, {});
  if (value === undefined) return params[param];
  params[param] = value;
  var finalParams = Object.keys(params).reduce(function (s, v) { if (params[v]) s.push(v + \"=\" + params[v]); return s; }, []).join(\"&\");
  return hrefParts[0] + (finalParams.length > 0 ? \"?\" + finalParams : \"\");
}

// This PageTransitionEvent fires when the page first loads, as well as after any other history
// transition such as when using the browser's Back and Forward buttons.
window.addEventListener(\"pageshow\", linkSchemas);
var brickSchema,
    brickTestSchema;
function linkSchemas() {
  var schemaSelect = document.getElementById(\"schema\");
  var tblSelect = document.getElementById(\"tbl\");
  if (tblSelect) { // Always present for Brick pages
    // Used to be:  var i = # {::Brick.config.path_prefix ? '0' : 'schemaSelect ? 1 : 0'},
    var changeoutList = changeout(location.href);
    for (var i = 0; i < changeoutList.length; ++i) {
      tblSelect.value = changeoutList[i];
      if (tblSelect.value !== \"\") break;
    }

    tblSelect.addEventListener(\"change\", function () {
      var lhr = changeout(location.href, null, this.value);
      if (brickSchema) lhr = changeout(lhr, \"_brick_schema\", schemaSelect.value);
      location.href = lhr;
    });

    if (schemaSelect) { // First drop-down is only present if multitenant
      if (brickSchema = changeout(location.href, \"_brick_schema\")) {
        [... document.getElementsByTagName(\"A\")].forEach(function (a) { a.href = changeout(a.href, \"_brick_schema\", brickSchema); });
      }
      if (schemaSelect.options.length > 1) {
        schemaSelect.value = brickSchema || brickTestSchema || \"public\";
        schemaSelect.addEventListener(\"change\", function () {
          // If there's an ID then remove it (trim after selected table)
          location.href = changeout(location.href, \"_brick_schema\", this.value, tblSelect.value);
        });
      }
    }
    tblSelect.focus();

    [... document.getElementsByTagName(\"FORM\")].forEach(function (form) {
      if (brickSchema)
        form.action = changeout(form.action, \"_brick_schema\", brickSchema);
      form.addEventListener('submit', function (ev) {
        [... ev.target.getElementsByTagName(\"SELECT\")].forEach(function (select) {
          if (select.value === \"^^^brick_NULL^^^\") select.value = null;
        });
        // Take outer <div> tag off the HTML being returned by any Trix editor
        [... document.getElementsByTagName(\"TRIX-EDITOR\")].forEach(function (trix) {
          var trixHidden = trix.inputElement;
          if (trixHidden) trixHidden.value = trixHidden.value.slice(5, -6);
        });
        return true;
      });
    });
  }
};
"
BRICK_SVG =
"<svg version=\"1.1\" style=\"display: inline; padding-left: 0.5em;\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  viewBox=\"0 0 58 58\" height=\"1.4em\" xml:space=\"preserve\">
<g>
  <polygon style=\"fill:#C2615F;\" points=\"58,15.831 19.106,35.492 0,26.644 40,6\"/>
  <polygon style=\"fill:#6D4646;\" points=\"19,52 0,43.356 0,26.644 19,35\"/>
  <polygon style=\"fill:#894747;\" points=\"58,31.559 19,52 19,35 58,15.831\"/>
</g>
</svg>
"