stimulus-datatables
Version:
Run DataTables under Rails Turbolinks
137 lines (103 loc) • 6.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _stimulus = require("stimulus");
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var dt_id = 0;
var StimulusDataTables =
/*#__PURE__*/
function (_Controller) {
_inherits(StimulusDataTables, _Controller);
function StimulusDataTables() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, StimulusDataTables);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(StimulusDataTables)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_this), "isTable", function () {
return _this.element.nodeName === 'TABLE';
});
_defineProperty(_assertThisInitialized(_this), "isDataTable", function () {
return _this.element.className.includes('dataTable');
});
_defineProperty(_assertThisInitialized(_this), "isPreview", function () {
return document.documentElement.hasAttribute('data-turbolinks-preview');
});
_defineProperty(_assertThisInitialized(_this), "isLive", function () {
return _this.dataTable;
});
_defineProperty(_assertThisInitialized(_this), "isBooting", function () {
return _this.isTable() && !_this.isDataTable() && !_this.isPreview() && !_this.isLive();
});
_defineProperty(_assertThisInitialized(_this), "debug", function (msg) {
var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
if (!_this.config || !_this.config.debug) return;
_this.log(msg, extra);
});
_defineProperty(_assertThisInitialized(_this), "log", function (msg) {
var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var id = _this.element.id;
var pad = msg.length < 10 ? 10 - msg.length : 0;
console.log('DT', _this.dt_id || 0, msg, ' '.repeat(pad), id, extra);
});
_defineProperty(_assertThisInitialized(_this), "_teardown", function () {
return _this.teardown();
});
return _this;
}
_createClass(StimulusDataTables, [{
key: "initialize",
value: function initialize() {
if (!this.isBooting()) return false;
this.dt_id = ++dt_id;
this.element.dt = this; // Setting scrollY fixes page reload bug in autoWidth.
var pre_config = Object.assign({
scrollY: undefined
}, this.config);
var config_s = this.data.get('config');
var config = config_s ? JSON.parse(config_s) : {};
this.config = Object.assign({}, pre_config, config);
this.debug('initialize', {
config: this.config
});
return this.config;
}
}, {
key: "connect",
value: function connect() {
if (!this.isBooting()) return false; // Register the teardown listener and start up DataTable.
document.addEventListener('turbolinks:before-render', this._teardown);
this.dataTable = window.jQuery(this.element).DataTable(Object.assign({}, this.config));
this.debug('connect', {
dt: this
});
return this.config;
}
}, {
key: "teardown",
value: function teardown(event) {
if (!this.isLive()) return false;
document.removeEventListener('turbolinks:before-render', this._teardown);
this.dataTable.destroy();
this.dataTable = undefined;
this.debug('teardown');
return this.config;
}
}]);
return StimulusDataTables;
}(_stimulus.Controller);
var _default = StimulusDataTables;
exports["default"] = _default;
;