/*! Datatables altEditor 1.0 */ /** * @summary altEditor * @description Lightweight editor for DataTables * @version 1.0 * @file dataTables.editor.lite.js * @author kingkode (www.kingkode.com) * @contact www.kingkode.com/contact * @copyright Copyright 2016 Kingkode * * This source file is free software, available under the following license: * MIT license - http://datatables.net/license/mit * * This source file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details. * * For details please refer to: http://www.kingkode.com */ (function(factory) { if (typeof define === 'function' && define.amd) { // AMD define(['jquery', 'datatables.net'], function($) { return factory($, window, document); }); } else if (typeof exports === 'object') { // CommonJS module.exports = function(root, $) { if (!root) { root = window; } if (!$ || !$.fn.dataTable) { $ = require('datatables.net')(root, $).$; } return factory($, root, root.document); }; } else { // Browser factory(jQuery, window, document); } }(function($, window, document, undefined) { 'use strict'; var DataTable = $.fn.dataTable; var _instance = 0; /** * altEditor provides modal editing of records for Datatables * * @class altEditor * @constructor * @param {object} oTD DataTables settings object * @param {object} oConfig Configuration object for altEditor */ var altEditor = function(dt, opts) { if (!DataTable.versionCheck || !DataTable.versionCheck('1.10.8')) { throw ("Warning: altEditor requires DataTables 1.10.8 or greater"); } // User and defaults configuration object this.c = $.extend(true, {}, DataTable.defaults.altEditor, altEditor.defaults, opts ); /** * @namespace Settings object which contains customisable information for altEditor instance */ this.s = { /** @type {DataTable.Api} DataTables' API instance */ dt: new DataTable.Api(dt), /** @type {String} Unique namespace for events attached to the document */ namespace: '.altEditor' + (_instance++) }; /** * @namespace Common and useful DOM elements for the class instance */ this.dom = { /** @type {jQuery} altEditor handle */ modal: $('
'), }; /* Constructor logic */ this._constructor(); } $.extend(altEditor.prototype, { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /** * Initialise the RowReorder instance * * @private */ _constructor: function() { // console.log('altEditor Enabled') var that = this; var dt = this.s.dt; this._setup(); dt.on('destroy.altEditor', function() { dt.off('.altEditor'); $(dt.table().body()).off(that.s.namespace); $(document.body).off(that.s.namespace); }); }, /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Private methods * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /** * Setup dom and bind button actions * * @private */ _setup: function() { // console.log('Setup'); var that = this; var dt = this.s.dt; // add modal $('body').append('\' + data + ''); $('#altEditor-modal').find('.modal-footer').html("\ "); }); $('#altEditor-modal').modal('show'); $('#altEditor-modal input[0]').focus(); }, _editRowData: function() { var that = this; var dt = this.s.dt; var data = []; $('form[name="altEditor-form"] input').each(function(i) { var addToList = true; var value = $(this).val(); if($(this).attr('type') == "radio" && $(this).prop('checked') == false) { addToList = false; } value = $(this).attr('type') == "radio" ? $(this).val() == "1" : value; if (addToList){ data.push({ "value": value, "dataType": $(this).attr('data-type') }); } }); var editButtonCurrentText = $("#editRowBtn").text(); $("#editRowBtn").addClass('disabled'); $("#editRowBtn").text("Saving.."); that._emitEvent("update-row", [ JSON.stringify(data), function(isUpdated) { //set error message and other properties based on whether update is successfull or not var alertAdditionClasses = "alert-success"; var alertMessage = "This record has been updated"; var alertHeading = "Success"; if (!isUpdated) { alertAdditionClasses = "alert-danger"; alertMessage = "Error occurred while updating this record"; alertHeading = "Error"; } //create alert element html and append it to modal var messageHTML = '\
' + data + ''); $('#altEditor-modal').find('.modal-footer').html("\ "); }); $('#altEditor-modal').modal('show'); $('#altEditor-modal input[0]').focus(); }, _deleteRow: function() { var that = this; var dt = this.s.dt; var data = []; $('form[name="altEditor-form"] input').each(function(i) { var addToList = true; var value = $(this).val(); value = $(this).val(); console.log("Value : " + value); if (addToList){ data.push({ "value": value, "dataType": $(this).attr('data-type') }); } }); $('#altEditor-modal .modal-body .alert').remove(); var message = '
' + data + ''); $('#altEditor-modal').find('.modal-footer').html("\ "); }); $('#altEditor-modal').modal('show'); $('#altEditor-modal input[0]').focus(); }, _addRowData: function() { console.log('add row') var that = this; var dt = this.s.dt; var data = []; $('form[name="altEditor-form"] input').each(function(i) { var addToList = true; var value = $(this).val(); if($(this).attr('type') == "radio" && $(this).prop('checked') == false) { addToList = false; } value = $(this).attr('type') == "radio" ? $(this).val() == "1" : value; if (addToList){ data.push({ "value": value, "dataType": $(this).attr('data-type') }); } }); var editButtonCurrentText = $("#editRowBtn").text(); $("#addRowBtn").addClass('disabled'); $("#addRowBtn").text("Saving.."); console.log(JSON.stringify(data)); that._emitEvent("add-row", [ JSON.stringify(data), function(isAdded) { //set error message and other properties based on whether update is successfull or not var alertAdditionClasses = "alert-success"; var alertMessage = "This record has been added"; var alertHeading = "Success"; if (!isAdded) { alertAdditionClasses = "alert-danger"; alertMessage = "Error occurred while adding this record"; alertHeading = "Error"; } //create alert element html and append it to modal var messageHTML = '\