diff --git a/modules/backend/ServiceProvider.php b/modules/backend/ServiceProvider.php index e7fe2e613..eafe022ea 100644 --- a/modules/backend/ServiceProvider.php +++ b/modules/backend/ServiceProvider.php @@ -46,9 +46,9 @@ class ServiceProvider extends ModuleServiceProvider 'label' => 'Date picker', 'alias' => 'datepicker' ]); - $manager->registerFormWidget('Backend\FormWidgets\Grid', [ - 'label' => 'Grid', - 'alias' => 'grid' + $manager->registerFormWidget('Backend\FormWidgets\DataGrid', [ + 'label' => 'Data Grid', + 'alias' => 'datagrid' ]); }); diff --git a/modules/backend/formwidgets/Grid.php b/modules/backend/formwidgets/DataGrid.php similarity index 92% rename from modules/backend/formwidgets/Grid.php rename to modules/backend/formwidgets/DataGrid.php index 1a9620ba4..d5f7484df 100644 --- a/modules/backend/formwidgets/Grid.php +++ b/modules/backend/formwidgets/DataGrid.php @@ -9,12 +9,12 @@ use Backend\Classes\FormWidgetBase; * @package october\backend * @author Alexey Bobkov, Samuel Georges */ -class Grid extends FormWidgetBase +class DataGrid extends FormWidgetBase { /** * {@inheritDoc} */ - public $defaultAlias = 'grid'; + public $defaultAlias = 'datagrid'; /** * @var array Grid columns @@ -35,7 +35,7 @@ class Grid extends FormWidgetBase public function render() { $this->prepareVars(); - return $this->makePartial('grid'); + return $this->makePartial('datagrid'); } /** @@ -105,9 +105,9 @@ class Grid extends FormWidgetBase public function loadAssets() { $this->addCss('vendor/handsontable/jquery.handsontable.full.css', 'core'); - $this->addCss('css/grid.css', 'core'); + $this->addCss('css/datagrid.css', 'core'); $this->addJs('vendor/handsontable/jquery.handsontable.full.js', 'core'); - $this->addJs('js/grid.js', 'core'); + $this->addJs('js/datagrid.js', 'core'); } /** diff --git a/modules/backend/formwidgets/datagrid/assets/js/datagrid.js b/modules/backend/formwidgets/datagrid/assets/js/datagrid.js new file mode 100644 index 000000000..e5467e370 --- /dev/null +++ b/modules/backend/formwidgets/datagrid/assets/js/datagrid.js @@ -0,0 +1,113 @@ +/* + * DataGrid plugin + * + * Data attributes: + * - data-control="datagrid" - enables the plugin on an element + * - data-option="value" - an option with a value + * + * JavaScript API: + * $('a#someElement').dataGrid({ option: 'value' }) + * + * Dependences: + * - Some other plugin (filename.js) + */ + ++function ($) { "use strict"; + + // DATAGRID CLASS DEFINITION + // ============================ + + var DataGrid = function(element, options) { + var self = this + this.options = options + this.$el = $(element) + + this.staticWidths = this.calculateColumnWidths() + + // Init + var handsontableOptions = { + colHeaders: this.options.columnHeaders, + colWidths: function(columnIndex) { + return self.staticWidths[columnIndex] + }, + columns: this.options.columns, + startRows: this.options.startRows + } + + this.$el.handsontable(handsontableOptions) + + $(window).on('resize', function(){ + self.staticWidths = self.calculateColumnWidths() + }) + } + + DataGrid.DEFAULTS = { + startRows: null, + columnHeaders: null, + columnWidths: null, + columns: null + } + + DataGrid.prototype.calculateColumnWidths = function() { + + var widths = this.options.columnWidths, + totalWidth = this.$el.innerWidth(), + usedWidth = 0, + unsetWidthCounts = 0, + staticWidths = [] + + $.each(widths, function() { + var width = parseInt(this) + usedWidth += width + if (width <= 0) unsetWidthCounts++ + }) + + $.each(widths, function() { + if (this > 0) { + staticWidths.push(this) + } else { + var remainingWidth = ((totalWidth - usedWidth) / unsetWidthCounts) - 2 + staticWidths.push(Math.max(remainingWidth, 100)) + } + }) + + return staticWidths + } + + // DATAGRID PLUGIN DEFINITION + // ============================ + + var old = $.fn.dataGrid + + $.fn.dataGrid = function (option) { + var args = Array.prototype.slice.call(arguments, 1), result + this.each(function () { + var $this = $(this) + var data = $this.data('oc.datagrid') + var options = $.extend({}, DataGrid.DEFAULTS, $this.data(), typeof option == 'object' && option) + if (!data) $this.data('oc.datagrid', (data = new DataGrid(this, options))) + if (typeof option == 'string') result = data[option].apply(data, args) + if (typeof result != 'undefined') return false + }) + + return result ? result : this + } + + $.fn.dataGrid.Constructor = DataGrid + + // DATAGRID NO CONFLICT + // ================= + + $.fn.dataGrid.noConflict = function () { + $.fn.dataGrid = old + return this + } + + // DATAGRID DATA-API + // =============== + + $(document).on('render', function(){ + $('div[data-control=datagrid]').dataGrid() + }) + +}(window.jQuery); \ No newline at end of file diff --git a/modules/backend/formwidgets/grid/assets/vendor/handsontable/README.md b/modules/backend/formwidgets/datagrid/assets/vendor/handsontable/README.md similarity index 100% rename from modules/backend/formwidgets/grid/assets/vendor/handsontable/README.md rename to modules/backend/formwidgets/datagrid/assets/vendor/handsontable/README.md diff --git a/modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.css b/modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.css similarity index 100% rename from modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.css rename to modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.css diff --git a/modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.full.css b/modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.full.css similarity index 100% rename from modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.full.css rename to modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.full.css diff --git a/modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.full.js b/modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.full.js similarity index 100% rename from modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.full.js rename to modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.full.js diff --git a/modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.js b/modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.js similarity index 100% rename from modules/backend/formwidgets/grid/assets/vendor/handsontable/jquery.handsontable.js rename to modules/backend/formwidgets/datagrid/assets/vendor/handsontable/jquery.handsontable.js diff --git a/modules/backend/formwidgets/datagrid/partials/_datagrid.htm b/modules/backend/formwidgets/datagrid/partials/_datagrid.htm new file mode 100644 index 000000000..1edcd5342 --- /dev/null +++ b/modules/backend/formwidgets/datagrid/partials/_datagrid.htm @@ -0,0 +1,12 @@ +
+ + \ No newline at end of file diff --git a/modules/backend/formwidgets/grid/partials/_grid.htm b/modules/backend/formwidgets/grid/partials/_grid.htm deleted file mode 100644 index 57301fd94..000000000 --- a/modules/backend/formwidgets/grid/partials/_grid.htm +++ /dev/null @@ -1,35 +0,0 @@ - - -