From 3f98397d5a4a9309bbe16119d044f239a9068bc3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Mon, 9 Oct 2017 00:32:19 +0530 Subject: [PATCH] Organize code using javascript object literal. --- .editorconfig | 2 +- webroot/js/local.js | 173 +++++++++++++++++++++++++------------------- 2 files changed, 99 insertions(+), 76 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6511313f..3a050c04 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,7 +17,7 @@ indent_size = 2 [*.js] indent_style = space -indent_size = 2 +indent_size = 4 [Makefile] indent_style = tab diff --git a/webroot/js/local.js b/webroot/js/local.js index ac977aa1..3de938e0 100644 --- a/webroot/js/local.js +++ b/webroot/js/local.js @@ -1,92 +1,115 @@ -$(function() { - - var bulkActionForm = $('.bulk-actions'); - if (bulkActionForm.length) { - bulkActionForm.submit(function (e) { - var action = $('.bulk-actions .bulk-action-submit select').val(); - if (!action) { - return e.preventDefault(); - } +var CrudView = { + bulkActionForm: function (selector) { + var bulkActionForm = $(selector); + if (bulkActionForm.length) { + bulkActionForm.submit(function (e) { + var action = $('.bulk-action-submit select', bulkActionForm).val(); + if (!action) { + return e.preventDefault(); + } - bulkActionForm.attr('action', action); - }); - } + bulkActionForm.attr('action', action); + }); + } + }, - $('[role=datetime-picker]').each(function() { + datePicker: function (selector) { + $(selector).each(function() { + var picker = $(this); + var date = null; - var picker = $(this); - var date = null; + if (picker.data('timestamp') && picker.data('timezone-offset')) { + var timezoneOffset = picker.data('timezone-offset'); + date = new Date(picker.data('timestamp') * 1000); - if (picker.data('timestamp') && picker.data('timezone-offset')) { - var timezoneOffset = picker.data('timezone-offset'); - date = new Date(picker.data('timestamp') * 1000); + picker.parents('form').on('submit', function () { + var timezoneDiff = timezoneOffset + date.getTimezoneOffset(); + var currentDate = picker.data('DateTimePicker').date(); + var convertedDate = currentDate.add(timezoneDiff, 'minutes'); + picker.data('DateTimePicker').date(convertedDate); + }); + } - picker.parents('form').on('submit', function () { - var timezoneDiff = timezoneOffset + date.getTimezoneOffset(); - var currentDate = picker.data('DateTimePicker').date(); - var convertedDate = currentDate.add(timezoneDiff, 'minutes'); - picker.data('DateTimePicker').date(convertedDate); + picker.datetimepicker({ + locale: $(this).data('locale'), + format: $(this).data('format'), + date: date ? date : picker.val() }); - } - - picker.datetimepicker({ - locale: $(this).data('locale'), - format: $(this).data('format'), - date: date ? date : picker.val() }); - }); + }, - $('select:not(.autocomplete, .no-selectize)').selectize({plugins: ['remove_button']}); + selectize: function (selector) { + $(selector).selectize({plugins: ['remove_button']}); + }, - $('input.autocomplete, select.autocomplete').each(function (i, e) { - e = $(e); - e.selectize({ - maxItems: e.data('max-items') || 1, - maxOptions: e.data('max-options') || 10, - hideSelected: e.data('hide-selected'), - closeAfterSelect: e.data('close-after-select'), - create: !e.data('exact-match'), - persist: false, - render: { - 'option_create': function(data, escape) { - return '
🔍 ' + escape(data.input) + '
'; - } - }, - load: function (query, callback) { - var data = {}; + autocomplete: function (selector) { + $(selector).each(function (i, e) { + e = $(e); + e.selectize({ + maxItems: e.data('max-items') || 1, + maxOptions: e.data('max-options') || 10, + hideSelected: e.data('hide-selected'), + closeAfterSelect: e.data('close-after-select'), + create: !e.data('exact-match'), + persist: false, + render: { + 'option_create': function(data, escape) { + return '
🔍 ' + escape(data.input) + '
'; + } + }, + load: function (query, callback) { + var data = {}; - data[e.data('filter-field') || e.attr('name')] = query; + data[e.data('filter-field') || e.attr('name')] = query; - if (e.data('dependent-on') && $('#' + e.data('dependent-on')).val()) { - data[e.data('dependent-on-field')] = $('#' + e.data('dependent-on')).val(); - } - $.ajax({ - url: e.data('url'), - dataType: 'json', - data: data, - error: function() { - callback(); - }, - success: function(res) { - callback($.map(res.data, function (name, id) { - return {value: id, text: name}; - })); + if (e.data('dependent-on') && $('#' + e.data('dependent-on')).val()) { + data[e.data('dependent-on-field')] = $('#' + e.data('dependent-on')).val(); } - }); - } + $.ajax({ + url: e.data('url'), + dataType: 'json', + data: data, + error: function() { + callback(); + }, + success: function(res) { + callback($.map(res.data, function (name, id) { + return {value: id, text: name}; + })); + } + }); + } + }); }); - }); + }, + + dirtyForms: function () { + $.DirtyForms.dialog = false; + $('form[data-dirty-check=1]').dirtyForms(); + }, + + dropdown: function () { + $('.dropdown-toggle').dropdown(); - $.DirtyForms.dialog = false; - $('form[data-dirty-check=1]').dirtyForms(); + // recommended hack to get dropdowns correctly work inside responsive table + $('.table-responsive').on('show.bs.dropdown', function () { + $('.table-responsive').css( "overflow", "inherit" ); + }); + $('.table-responsive').on('hide.bs.dropdown', function () { + $('.table-responsive').css( "overflow", "auto" ); + }) + }, - $('.dropdown-toggle').dropdown(); + initialize: function() { + this.bulkActionForm('.bulk-actions'); + this.datePicker('[role=datetime-picker]'); + this.selectize('select:not(.autocomplete, .no-selectize)'); + this.autocomplete('input.autocomplete, select.autocomplete'); + this.dirtyForms(); + this.dropdown(); + } +}; - // recommended hack to get dropdowns correctly work inside responsive table - $('.table-responsive').on('show.bs.dropdown', function () { - $('.table-responsive').css( "overflow", "inherit" ); - }); - $('.table-responsive').on('hide.bs.dropdown', function () { - $('.table-responsive').css( "overflow", "auto" ); - }) +$(function() { + CrudView.initialize(); });