/*
* Gijgo JavaScript Library v1.3.0
* http://gijgo.com/
*
* Copyright 2014, 2017 gijgo.com
* Released under the MIT license
*/
if (typeof (gj) === 'undefined') {
gj = {};
}
gj.widget = function () {
var self = this;
self.xhr = null;
self.generateGUID = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};
self.mouseX = function (e) {
if (e) {
if (e.pageX) {
return e.pageX;
} else if (e.clientX) {
return e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}
}
return null;
};
self.mouseY = function (e) {
if (e) {
if (e.pageY) {
return e.pageY;
} else if (e.clientY) {
return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}
}
return null;
};
};
gj.widget.prototype.init = function (jsConfig, type) {
var option, clientConfig, fullConfig;
clientConfig = $.extend(true, {}, this.getHTMLConfig() || {});
$.extend(true, clientConfig, jsConfig || {});
fullConfig = this.getConfig(clientConfig, type);
this.attr('data-guid', fullConfig.guid);
this.data(fullConfig);
// Initialize events configured as options
for (option in fullConfig) {
if (gj[type].events.hasOwnProperty(option)) {
this.on(option, fullConfig[option]);
delete fullConfig[option];
}
}
// Initialize all plugins
for (plugin in gj[type].plugins) {
if (gj[type].plugins.hasOwnProperty(plugin)) {
gj[type].plugins[plugin].configure(this, fullConfig, clientConfig);
}
}
return this;
};
gj.widget.prototype.getConfig = function (clientConfig, type) {
var config, uiLibrary, iconsLibrary, plugin;
config = $.extend(true, {}, gj[type].config.base);
uiLibrary = clientConfig.hasOwnProperty('uiLibrary') ? clientConfig.uiLibrary : config.uiLibrary;
if (gj[type].config[uiLibrary]) {
$.extend(true, config, gj[type].config[uiLibrary]);
}
iconsLibrary = clientConfig.hasOwnProperty('iconsLibrary') ? clientConfig.iconsLibrary : config.iconsLibrary;
if (gj[type].config[iconsLibrary]) {
$.extend(true, config, gj[type].config[iconsLibrary]);
}
for (plugin in gj[type].plugins) {
if (gj[type].plugins.hasOwnProperty(plugin)) {
$.extend(true, config, gj[type].plugins[plugin].config.base);
if (gj[type].plugins[plugin].config[uiLibrary]) {
$.extend(true, config, gj[type].plugins[plugin].config[uiLibrary]);
}
if (gj[type].plugins[plugin].config[iconsLibrary]) {
$.extend(true, config, gj[type].plugins[plugin].config[iconsLibrary]);
}
}
}
$.extend(true, config, clientConfig);
if (!config.guid) {
config.guid = this.generateGUID();
}
return config;
}
gj.widget.prototype.getHTMLConfig = function () {
var result = this.data(),
attrs = this[0].attributes;
if (attrs['width']) {
result.width = attrs['width'].nodeValue;
}
if (attrs['height']) {
result.height = attrs['height'].nodeValue;
}
if (attrs['align']) {
result.align = attrs['align'].nodeValue;
}
if (result && result.source) {
result.dataSource = result.source;
delete result.source;
}
return result;
};
gj.widget.prototype.createDoneHandler = function () {
var $widget = this;
return function (response) {
if (typeof (response) === 'string' && JSON) {
response = JSON.parse(response);
}
gj[$widget.data('type')].methods.render($widget, response);
};
};
gj.widget.prototype.createErrorHandler = function () {
var $widget = this;
return function (response) {
if (response && response.statusText && response.statusText !== 'abort') {
alert(response.statusText);
}
};
};
gj.widget.prototype.reload = function (params) {
var ajaxOptions, result, data = this.data(), type = this.data('type');
if (data.dataSource === undefined) {
gj[type].methods.useHtmlDataSource(this, data);
}
$.extend(data.params, params);
if ($.isArray(data.dataSource)) {
result = gj[type].methods.filter(this);
gj[type].methods.render(this, result);
} else if (typeof(data.dataSource) === 'string') {
ajaxOptions = { url: data.dataSource, data: data.params };
if (this.xhr) {
this.xhr.abort();
}
this.xhr = $.ajax(ajaxOptions).done(this.createDoneHandler()).fail(this.createErrorHandler());
} else if (typeof (data.dataSource) === 'object') {
if (!data.dataSource.data) {
data.dataSource.data = {};
}
$.extend(data.dataSource.data, data.params);
ajaxOptions = $.extend(true, {}, data.dataSource); //clone dataSource object
if (ajaxOptions.dataType === 'json' && typeof(ajaxOptions.data) === 'object') {
ajaxOptions.data = JSON.stringify(ajaxOptions.data);
}
if (!ajaxOptions.success) {
ajaxOptions.success = this.createDoneHandler();
}
if (!ajaxOptions.error) {
ajaxOptions.error = this.createErrorHandler();
}
if (this.xhr) {
this.xhr.abort();
}
this.xhr = $.ajax(ajaxOptions);
}
return this;
}
gj.documentManager = {
events: {},
subscribeForEvent: function (eventName, widgetId, callback) {
if (!gj.documentManager.events[eventName] || gj.documentManager.events[eventName].length === 0) {
gj.documentManager.events[eventName] = [{ widgetId: widgetId, callback: callback }];
$(document).on(eventName, gj.documentManager.executeCallbacks);
} else if (!gj.documentManager.events[eventName][widgetId]) {
gj.documentManager.events[eventName].push({ widgetId: widgetId, callback: callback });
} else {
throw "Event " + eventName + " for widget with guid='" + widgetId + "' is already attached.";
}
},
executeCallbacks: function (e) {
var callbacks = gj.documentManager.events[e.type];
if (callbacks) {
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].callback(e);
}
}
},
unsubscribeForEvent: function (eventName, widgetId) {
var success = false,
events = gj.documentManager.events[eventName];
if (events) {
for (var i = 0; i < events.length; i++) {
if (events[i].widgetId === widgetId) {
events.splice(i, 1);
success = true;
if (events.length === 0) {
$(document).off(eventName);
delete gj.documentManager.events[eventName];
}
}
}
}
if (!success) {
throw 'The "' + eventName + '" for widget with guid="' + widgetId + '" can\'t be removed.';
}
}
};
if (typeof (gj.dialog) === 'undefined') {
gj.dialog = {
plugins: {},
messages: []
};
}
gj.dialog.messages['en-us'] = {
Close: 'Close',
DefaultTitle: 'Dialog'
};
/* global window alert jQuery */
/**
* @widget Dialog
* @plugin Base
*/
gj.dialog.config = {
base: {
/** If set to true, the dialog will automatically open upon initialization.
* If false, the dialog will stay hidden until the open() method is called.
* @type boolean
* @default true
* @example True
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example False
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
autoOpen: true,
/** Specifies whether the dialog should close when it has focus and the user presses the escape (ESC) key.
* @type boolean
* @default true
* @example True
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example False
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
closeOnEscape: true,
/** Specifies whether the dialog should have a close button in right part of dialog header.
* @type boolean
* @default true
* @example True
*
*
Dialog
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
*
*
*
* @example False
*
*
Dialog
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
*
*
*
*
*
*/
closeButtonInHeader: true,
/** If set to true, the dialog will be draggable by the title bar.
* @type boolean
* @default true
* @example True
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example False
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
draggable: true,
/** The height of the dialog.
* @additionalinfo Support string and number values. The number value sets the height in pixels.
* The only supported string value is "auto" which will allow the dialog height to adjust based on its content.
* @type (number|string)
* @default "auto"
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
height: 'auto',
/** The language that needs to be in use.
* @type string
* @default 'en-us'
* @example French.Default
*
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example French.Custom
*
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
locale: 'en-us',
/** The minimum height in pixels to which the dialog can be resized.
* @type number
* @default undefined
* @example sample
*
The minimum height of this dialog is set to 200 px. Try to resize it for testing.
*
*/
minHeight: undefined,
/** The maximum height in pixels to which the dialog can be resized.
* @type number
* @default undefined
* @example sample
*
The maximum height of this dialog is set to 300 px. Try to resize it for testing.
*
*/
maxHeight: undefined,
/** The width of the dialog.
* @type number
* @default 300
* @example Fixed.Width
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example Auto.Width
*
*
*
*
*/
width: 300,
/** The minimum width in pixels to which the dialog can be resized.
* @type number
* @default undefined
* @example sample
*
The minimum width of this dialog is set to 200 px. Try to resize it for testing.
*
*/
minWidth: undefined,
/** The maximum width in pixels to which the dialog can be resized.
* @type number
* @default undefined
* @example sample
*
The maximum width of this dialog is set to 400 px. Try to resize it for testing.
*
*/
maxWidth: undefined,
/** If set to true, the dialog will have modal behavior.
* Modal dialogs create an overlay below the dialog, but above other page elements and you can't interact with them.
* @type boolean
* @default false
* @example True
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example False
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
modal: false,
/** If set to true, the dialog will be resizable.
* @type boolean
* @default false
* @example True
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example False
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
resizable: false,
/** The title of the dialog. Can be also set through the title attribute of the html element.
* @type string
* @default "Dialog"
* @example Js.Config
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example Html.Config
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*/
title: undefined,
/** The name of the UI library that is going to be in use. Currently we support only jQuery UI, Foundation, Material Design Lite and Bootstrap.
* @additionalinfo The css files for jQuery UI, Foundation, Material Design Lite or Bootstrap should be manually included to the page where the dialog is in use.
* @type string (jqueryui|bootstrap|materialdesign|foundation)
* @default undefined
* @example jQueryUI
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example jQueryUI.Theme
*
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example Foundation
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example Bootstrap
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
* @example Material.Design
*
*
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
initialized: function ($dialog) {
$dialog.trigger("initialized");
},
/**
* Triggered before the dialog is opened.
* @event opening
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
opening: function ($dialog) {
$dialog.trigger("opening");
},
/**
* Triggered when the dialog is opened.
* @event opened
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
opened: function ($dialog) {
$dialog.trigger("opened");
},
/**
* Triggered before the dialog is closed.
* @event closing
* @param {object} e - event data
* @example sample
*
Close the dialog in order to fire closing event.
*
*
*/
closing: function ($dialog) {
$dialog.trigger("closing");
},
/**
* Triggered when the dialog is closed.
* @event closed
* @param {object} e - event data
* @example sample
*
Close the dialog in order to fire closed event.
*
*
*/
closed: function ($dialog) {
$dialog.trigger("closed");
},
/**
* Triggered while the dialog is being dragged.
* @event drag
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
drag: function ($dialog) {
$dialog.trigger("drag");
},
/**
* Triggered when the user starts dragging the dialog.
* @event dragStart
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
dragStart: function ($dialog) {
$dialog.trigger("dragStart");
},
/**
* Triggered after the dialog has been dragged.
* @event dragStop
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
dragStop: function ($dialog) {
$dialog.trigger("dragStop");
},
/**
* Triggered while the dialog is being resized.
* @event resize
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
resize: function ($dialog) {
$dialog.trigger("resize");
},
/**
* Triggered when the user starts resizing the dialog.
* @event resizeStart
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
resizeStart: function ($dialog) {
$dialog.trigger("resizeStart");
},
/**
* Triggered after the dialog has been resized.
* @event resizeStop
* @param {object} e - event data
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
resizeStop: function ($dialog) {
$dialog.trigger("resizeStop");
}
};
gj.dialog.methods = {
init: function (jsConfig) {
this.attr('data-type', 'dialog');
gj.widget.prototype.init.call(this, jsConfig, 'dialog');
gj.dialog.methods.localization(this);
gj.dialog.methods.initialize(this);
gj.dialog.events.initialized(this);
return this;
},
localization: function($dialog) {
var data = $dialog.data();
if (typeof (data.title) === 'undefined') {
data.title = gj.dialog.messages[data.locale].DefaultTitle;
}
},
getHTMLConfig: function () {
var result = gj.widget.prototype.getHTMLConfig.call(this),
attrs = this[0].attributes;
if (attrs['title']) {
result.title = attrs['title'].nodeValue;
}
return result;
},
initialize: function ($dialog) {
var data = $dialog.data(),
$header, $body;
$dialog.addClass(data.style.content);
gj.dialog.methods.setSize($dialog);
if (data.closeOnEscape) {
$(document).keyup(function (e) {
if (e.keyCode === 27) {
$dialog.close();
}
});
}
$body = $dialog.children('div[data-role="body"]');
if ($body.length === 0) {
$body = $('').addClass(data.style.body);
$dialog.wrapInner($body);
} else {
$body.addClass(data.style.body);
}
$header = gj.dialog.methods.renderHeader($dialog);
$dialog.children('div[data-role="footer"]').addClass(data.style.footer);
$dialog.find('[data-role="close"]').on('click', function () {
$dialog.close();
});
if (data.draggable && $.fn.draggable) {
gj.dialog.methods.draggable($dialog, $header);
}
if (data.resizable && $.fn.draggable) {
gj.dialog.methods.resizable($dialog);
}
gj.dialog.methods.setPosition($dialog);
if (data.modal) {
$dialog.wrapAll('');
}
if (data.autoOpen) {
$dialog.open();
}
},
setSize: function ($dialog) {
var data = $dialog.data();
if (data.width) {
$dialog.css("width", data.width);
}
if (data.height) {
$dialog.css("height", data.height);
}
},
renderHeader: function ($dialog) {
var $header, $title, $closeButton, data = $dialog.data();
$header = $dialog.children('div[data-role="header"]');
if ($header.length === 0) {
$header = $('');
$dialog.prepend($header);
}
$header.addClass(data.style.header);
$title = $header.find('[data-role="title"]');
if ($title.length === 0) {
$title = $('
' + data.title + '
');
$header.append($title);
}
$title.addClass(data.style.headerTitle);
$closeButton = $header.find('[data-role="close"]');
if ($closeButton.length === 0 && data.closeButtonInHeader) {
$closeButton = $('');
$closeButton.addClass(data.style.headerCloseButton);
$header.prepend($closeButton);
} else if ($closeButton.length > 0 && data.closeButtonInHeader === false) {
$closeButton.hide();
} else {
$closeButton.addClass(data.style.headerCloseButton);
}
return $header;
},
setPosition: function ($dialog) {
var left = ($(window).width() / 2) - ($dialog.width() / 2),
top = ($(window).height() / 2) - ($dialog.height() / 2);
$dialog.css('position', 'absolute');
$dialog.css('left', left > 0 ? left : 0);
$dialog.css('top', top > 0 ? top : 0);
},
draggable: function ($dialog, $header) {
$dialog.appendTo('body');
$header.addClass('gj-draggable');
$dialog.draggable({
handle: $header,
start: function () {
$dialog.addClass('gj-unselectable');
gj.dialog.events.dragStart($dialog);
},
stop: function () {
$dialog.removeClass('gj-unselectable');
gj.dialog.events.dragStop($dialog);
}
});
},
resizable: function ($dialog) {
var config = {
'drag': gj.dialog.methods.resize,
'start': function () {
$dialog.addClass('gj-unselectable');
gj.dialog.events.resizeStart($dialog);
},
'stop': function () {
this.removeAttribute('style');
$dialog.removeClass('gj-unselectable');
gj.dialog.events.resizeStop($dialog);
}
};
$dialog.append($('').draggable($.extend(true, { horizontal: false }, config)));
$dialog.append($('').draggable($.extend(true, { vertical: false }, config)));
$dialog.append($('').draggable($.extend(true, { horizontal: false }, config)));
$dialog.append($('').draggable($.extend(true, { vertical: false }, config)));
$dialog.append($('').draggable($.extend(true, {}, config)));
$dialog.append($('').draggable($.extend(true, {}, config)));
$dialog.append($('').draggable($.extend(true, {}, config)));
$dialog.append($('').draggable($.extend(true, {}, config)));
},
resize: function (e, offset) {
var $el, $dialog, data, height, width, top, left, result = false;
$el = $(this);
$dialog = $el.parent();
data = $dialog.data();
//TODO: Include margins in the calculations
if ($el.hasClass('gj-resizable-n')) {
height = $dialog.height() - offset.top;
top = $dialog.offset().top + offset.top;
} else if ($el.hasClass('gj-resizable-e')) {
width = $dialog.width() + offset.left;
} else if ($el.hasClass('gj-resizable-s')) {
height = $dialog.height() + offset.top;
} else if ($el.hasClass('gj-resizable-w')) {
width = $dialog.width() - offset.left;
left = $dialog.offset().left + offset.left;
} else if ($el.hasClass('gj-resizable-ne')) {
height = $dialog.height() - offset.top;
top = $dialog.offset().top + offset.top;
width = $dialog.width() + offset.left;
} else if ($el.hasClass('gj-resizable-nw')) {
height = $dialog.height() - offset.top;
top = $dialog.offset().top + offset.top;
width = $dialog.width() - offset.left;
left = $dialog.offset().left + offset.left;
} else if ($el.hasClass('gj-resizable-se')) {
height = $dialog.height() + offset.top;
width = $dialog.width() + offset.left;
} else if ($el.hasClass('gj-resizable-sw')) {
height = $dialog.height() + offset.top;
width = $dialog.width() - offset.left;
left = $dialog.offset().left + offset.left;
}
if (height && (!data.minHeight || height >= data.minHeight) && (!data.maxHeight || height <= data.maxHeight)) {
$dialog.height(height);
if (top) {
$dialog.css('top', top);
}
result = true;
}
if (width && (!data.minWidth || width >= data.minWidth) && (!data.maxWidth || width <= data.maxWidth)) {
$dialog.width(width);
if (left) {
$dialog.css('left', left);
}
result = true;
}
if (result) {
gj.dialog.events.resize($dialog);
}
return result;
},
open: function ($dialog) {
var $footer;
gj.dialog.events.opening($dialog);
$dialog.css('display', 'block');
$dialog.closest('div[data-role="modal"]').css('display', 'block');
$footer = $dialog.children('div[data-role="footer"]');
if ($footer.length && $footer.outerHeight()) {
$dialog.children('div[data-role="body"]').css('margin-bottom', $footer.outerHeight());
}
gj.dialog.events.opened($dialog);
return $dialog;
},
close: function ($dialog) {
if ($dialog.is(':visible')) {
gj.dialog.events.closing($dialog);
$dialog.css('display', 'none');
$dialog.closest('div[data-role="modal"]').css('display', 'none');
gj.dialog.events.closed($dialog);
}
return $dialog;
},
isOpen: function ($dialog) {
return $dialog.is(':visible');
},
destroy: function ($dialog, keepHtml) {
var data = $dialog.data();
if (data) {
if (keepHtml === false) {
$dialog.remove();
} else {
$dialog.close();
$dialog.off();
$dialog.removeData();
$dialog.removeAttr('data-type');
$dialog.removeClass(data.style.content);
$dialog.find('[data-role="header"]').removeClass(data.style.header);
$dialog.find('[data-role="title"]').removeClass(data.style.headerTitle);
$dialog.find('[data-role="close"]').remove();
$dialog.find('[data-role="body"]').removeClass(data.style.body);
$dialog.find('[data-role="footer"]').removeClass(data.style.footer);
}
}
return $dialog;
}
};
/**
* @widget Dialog
* @plugin Base
*/
gj.dialog.widget = function ($element, arguments) {
var self = this,
methods = gj.dialog.methods;
/**
* Opens the dialog.
* @method
* @fires opening, opened
* @return dialog
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*/
self.open = function () {
return methods.open(this);
}
/**
* Close the dialog.
* @method
* @fires closinging, closed
* @return dialog
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
*/
self.close = function () {
return methods.close(this);
}
/**
* Check if the dialog is currently open.
* @method
* @return boolean
* @example sample
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
*
*/
self.isOpen = function () {
return methods.isOpen(this);
}
/**
* Destroy the dialog.
* @method
* @param {boolean} keepHtml - If this flag is set to false, the dialog html markup will be removed from the HTML dom tree.
* @return void
* @example Keep.HTML.Markup
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
* @example Remove.HTML.Markup
*
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
*
*
*
*/
self.destroy = function (keepHtml) {
return methods.destroy(this, keepHtml);
}
$.extend($element, self);
if ('dialog' !== $element.attr('data-type')) {
methods.init.apply($element, arguments);
}
return $element;
};
gj.dialog.widget.prototype = new gj.widget();
gj.dialog.widget.constructor = gj.dialog.widget;
gj.dialog.widget.prototype.getHTMLConfig = gj.dialog.methods.getHTMLConfig;
(function ($) {
$.fn.dialog = function (method) {
var $widget;
if (this && this.length) {
if (typeof method === 'object' || !method) {
return new gj.dialog.widget(this, arguments);
} else {
$widget = new gj.dialog.widget(this, null);
if ($widget[method]) {
return $widget[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
throw 'Method ' + method + ' does not exist.';
}
}
}
};
})(jQuery);
/* global window alert jQuery */
/**
* @widget Draggable
* @plugin Base
*/
if (typeof (gj.draggable) === 'undefined') {
gj.draggable = {};
}
gj.draggable.config = {
base: {
/** If specified, restricts dragging from starting unless the mousedown occurs on the specified element.
* Only elements that descend from the draggable element are permitted.
* @type jquery element
* @default undefined
* @example sample
*
*
*
Handle for dragging
*
*
*/
handle: undefined,
/** If set to false, restricts dragging on vertical direction.
* @type Boolean
* @default true
* @example sample
*
*
* drag me
* (dragging on vertical direction is disabled)
*
*
*/
vertical: true,
/** If set to false, restricts dragging on horizontal direction.
* @type Boolean
* @default true
* @example sample
*
*
* drag me
* (dragging on horizontal direction is disabled)
*
*
*/
horizontal: true
}
};
gj.draggable.methods = {
init: function (jsConfig) {
var $handleEl, $dragEl = this;
gj.widget.prototype.init.call(this, jsConfig, 'draggable');
$dragEl.attr('data-draggable', 'true');
$handleEl = gj.draggable.methods.getHandleElement($dragEl);
$handleEl.on('mousedown', function (e) {
$dragEl.attr('data-draggable-dragging', true);
$dragEl.removeAttr('data-draggable-x');
$dragEl.removeAttr('data-draggable-y');
$dragEl.css('position', 'absolute');
gj.documentManager.subscribeForEvent('mousemove', $dragEl.data('guid'), gj.draggable.methods.createMouseMoveHandler($dragEl));
});
gj.documentManager.subscribeForEvent('mouseup', $dragEl.data('guid'), gj.draggable.methods.createMouseUpHandler($dragEl));
return $dragEl;
},
getHandleElement: function ($dragEl) {
var $handle = $dragEl.data('handle');
return ($handle && $handle.length) ? $handle : $dragEl;
},
createMouseUpHandler: function ($dragEl) {
return function (e) {
if ($dragEl.attr('data-draggable-dragging') === 'true') {
$dragEl.attr('data-draggable-dragging', false);
gj.documentManager.unsubscribeForEvent('mousemove', $dragEl.data('guid'));
gj.draggable.events.stop($dragEl, {
left: $dragEl.mouseX(e),
top: $dragEl.mouseY(e)
});
}
};
},
createMouseMoveHandler: function ($dragEl) {
return function (e) {
var x, y, offsetX, offsetY, prevX, prevY;
if ($dragEl.attr('data-draggable-dragging') === 'true') {
x = $dragEl.mouseX(e);
y = $dragEl.mouseY(e);
prevX = $dragEl.attr('data-draggable-x');
prevY = $dragEl.attr('data-draggable-y');
if (prevX && prevY) {
offsetX = $dragEl.data('horizontal') ? x - parseInt(prevX, 10) : 0;
offsetY = $dragEl.data('vertical') ? y - parseInt(prevY, 10) : 0;
if (false !== gj.draggable.events.drag($dragEl, offsetX, offsetY, x, y)) {
gj.draggable.methods.move($dragEl, offsetX, offsetY);
}
} else {
gj.draggable.events.start($dragEl);
}
$dragEl.attr('data-draggable-x', x);
$dragEl.attr('data-draggable-y', y);
}
}
},
move: function ($dragEl, offsetX, offsetY) {
var target = $dragEl.get(0),
top = target.style.top ? parseInt(target.style.top) : $dragEl.position().top,
left = target.style.left ? parseInt(target.style.left) : $dragEl.position().left;
target.style.top = (top + offsetY) + 'px';
target.style.left = (left + offsetX) + 'px';
},
destroy: function ($dragEl) {
if ($dragEl.attr('data-draggable') === 'true') {
gj.documentManager.unsubscribeForEvent('mouseup', $dragEl.data('guid'));
$dragEl.removeData();
$dragEl.removeAttr('data-guid');
$dragEl.removeAttr('data-draggable');
$dragEl.off('drag').off('start').off('stop');
gj.draggable.methods.getHandleElement($dragEl).off('mousedown');
}
return $dragEl;
}
};
gj.draggable.events = {
/**
* Triggered while the mouse is moved during the dragging, immediately before the current move happens.
*
* @event drag
* @param {object} e - event data
* @param {object} offset - Current offset position as { top, left } object.
* @param {object} mousePosition - Current mouse position as { top, left } object.
* @example sample
*
*
*
*/
start: function ($dragEl) {
$dragEl.triggerHandler('start');
},
/**
* Triggered when dragging stops.
*
* @event stop
* @param {object} e - event data
* @param {object} mousePosition - Current mouse position as { top, left } object.
* @example sample
*
*
* drag me
*
*
*/
stop: function ($dragEl, mousePosition) {
$dragEl.triggerHandler('stop', [mousePosition]);
}
};
gj.draggable.widget = function ($element, arguments) {
var self = this,
methods = gj.draggable.methods;
if (!$element.destroy) {
/** Remove draggable functionality from the element.
* @method
* @return jquery element
* @example sample
*
*
*
Drag Me
*
*/
self.destroy = function () {
return methods.destroy(this);
};
}
$.extend($element, self);
if ('true' !== $element.attr('data-draggable')) {
methods.init.apply($element, arguments);
}
return $element;
};
gj.draggable.widget.prototype = new gj.widget();
gj.draggable.widget.constructor = gj.draggable.widget;
(function ($) {
$.fn.draggable = function (method) {
var $widget;
if (this && this.length) {
if (typeof method === 'object' || !method) {
return new gj.draggable.widget(this, arguments);
} else {
$widget = new gj.draggable.widget(this, null);
if ($widget[method]) {
return $widget[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
throw 'Method ' + method + ' does not exist.';
}
}
}
};
})(jQuery);
/* global window alert jQuery */
/**
* @widget Droppable
* @plugin Base
*/
if (typeof (gj.droppable) === 'undefined') {
gj.droppable = {};
}
gj.droppable.config = {
/** If specified, the class will be added to the droppable while draggable is being hovered over the droppable.
* @type string
* @default undefined
* @example sample
*
*
Drop Here
*
Drag Me
*
*/
hoverClass: undefined
};
gj.droppable.methods = {
init: function (jsConfig) {
var $dropEl = this;
gj.widget.prototype.init.call(this, jsConfig, 'droppable');
$dropEl.attr('data-droppable', 'true');
gj.documentManager.subscribeForEvent('mousedown', $dropEl.data('guid'), gj.droppable.methods.createMouseDownHandler($dropEl));
gj.documentManager.subscribeForEvent('mousemove', $dropEl.data('guid'), gj.droppable.methods.createMouseMoveHandler($dropEl));
gj.documentManager.subscribeForEvent('mouseup', $dropEl.data('guid'), gj.droppable.methods.createMouseUpHandler($dropEl));
return $dropEl;
},
createMouseDownHandler: function ($dropEl) {
return function (e) {
$dropEl.isDragging = true;
}
},
createMouseMoveHandler: function ($dropEl) {
return function (e) {
if ($dropEl.isDragging) {
var hoverClass = $dropEl.data('hoverClass'),
mousePosition = {
left: $dropEl.mouseX(e),
top: $dropEl.mouseY(e)
},
newIsOver = gj.droppable.methods.isOver($dropEl, mousePosition);
if (newIsOver != $dropEl.isOver) {
if (newIsOver) {
if (hoverClass) {
$dropEl.addClass(hoverClass);
}
gj.droppable.events.over($dropEl, mousePosition);
} else {
if (hoverClass) {
$dropEl.removeClass(hoverClass);
}
gj.droppable.events.out($dropEl);
}
}
$dropEl.isOver = newIsOver;
}
}
},
createMouseUpHandler: function ($dropEl) {
return function (e) {
var mousePosition = {
left: $dropEl.mouseX(e),
top: $dropEl.mouseY(e)
};
$dropEl.isDragging = false;
if (gj.droppable.methods.isOver($dropEl, mousePosition)) {
gj.droppable.events.drop($dropEl);
}
}
},
isOver: function ($dropEl, mousePosition) {
var offsetTop = $dropEl.offset().top;// + parseInt($dropEl.css("border-top-width")) + parseInt($dropEl.css("margin-top")) + parseInt($dropEl.css("padding-top")),
offsetLeft = $dropEl.offset().left;// + parseInt($dropEl.css("border-left-width")) + parseInt($dropEl.css("margin-left")) + parseInt($dropEl.css("padding-left"));
return mousePosition.left > offsetLeft && mousePosition.left < (offsetLeft + $dropEl.outerWidth(true))
&& mousePosition.top > offsetTop && mousePosition.top < (offsetTop + $dropEl.outerHeight(true));
},
destroy: function ($dropEl) {
if ($dropEl.attr('data-droppable') === 'true') {
gj.documentManager.unsubscribeForEvent('mousedown', $dropEl.data('guid'));
gj.documentManager.unsubscribeForEvent('mousemove', $dropEl.data('guid'));
gj.documentManager.unsubscribeForEvent('mouseup', $dropEl.data('guid'));
$dropEl.removeData();
$dropEl.removeAttr('data-guid');
$dropEl.removeAttr('data-droppable');
$dropEl.off('drop').off('over').off('out');
}
return $dropEl;
}
};
gj.droppable.events = {
/** Triggered when a draggable element is dropped.
* @event drop
* @param {object} e - event data
* @example sample
*
*
Drop Here
*
Drag Me
*
*/
drop: function ($dropEl, offsetX, offsetY) {
$dropEl.trigger('drop', [{ top: offsetY, left: offsetX }]);
},
/** Triggered when a draggable element is dragged over the droppable.
* @event over
* @param {object} e - event data
* @param {object} mousePosition - Current mouse position as { top, left } object.
* @example sample
*
*
Drop Here
*
Drag Me
*
*/
over: function ($dropEl, mousePosition) {
$dropEl.trigger('over', [mousePosition]);
},
/** Triggered when a draggable element is dragged out of the droppable.
* @event out
* @param {object} e - event data
* @example sample
*
*
Drop Here
*
Drag Me
*
*/
out: function ($dropEl) {
$dropEl.trigger('out');
}
};
gj.droppable.widget = function ($element, arguments) {
var self = this,
methods = gj.droppable.methods;
self.isOver = false;
self.isDragging = false;
/** Removes the droppable functionality.
* @method
* @return jquery element
* @example sample
*
*
*
*
*
Drop Here
*
Drag Me
*
*/
self.destroy = function () {
return methods.destroy(this);
}
self.isOver = function (mousePosition) {
return methods.isOver(this, mousePosition);
}
$.extend($element, self);
if ('true' !== $element.attr('data-droppable')) {
methods.init.apply($element, arguments);
}
return $element;
};
gj.droppable.widget.prototype = new gj.widget();
gj.droppable.widget.constructor = gj.droppable.widget;
(function ($) {
$.fn.droppable = function (method) {
var $widget;
if (this && this.length) {
if (typeof method === 'object' || !method) {
return new gj.droppable.widget(this, arguments);
} else {
$widget = new gj.droppable.widget(this, null);
if ($widget[method]) {
return $widget[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
throw 'Method ' + method + ' does not exist.';
}
}
}
};
})(jQuery);
if (typeof (gj.grid) === 'undefined') {
gj.grid = {
plugins: {},
messages: []
};
}
gj.grid.messages['en-us'] = {
First: 'First',
Previous: 'Previous',
Next: 'Next',
Last: 'Last',
Page: 'Page',
FirstPageTooltip: 'First Page',
PreviousPageTooltip: 'Previous Page',
NextPageTooltip: 'Next Page',
LastPageTooltip: 'Last Page',
Refresh: 'Refresh',
Of: 'of',
DisplayingRecords: 'Displaying records',
Edit: 'Edit',
Delete: 'Delete',
Update: 'Update',
Cancel: 'Cancel',
NoRecordsFound: 'No records found.'
};
/* global window alert jQuery gj */
/**
* @widget Grid
* @plugin Base
*/
gj.grid.config = {
base: {
/** The data source for the grid.
* @additionalinfo If set to string, then the grid is going to use this string as a url for ajax requests to the server.
* If set to object, then the grid is going to use this object as settings for the jquery ajax function.
* If set to array, then the grid is going to use the array as data for rows.
* @type (string|object|array)
* @default undefined
* @example Remote.JS.Configuration
*
*
* @example Remote.Html.Configuration
*
*
*
*
#
*
Name
*
PlaceOfBirth
*
*
*
*
* @example Local.DataSource
*
*
* @example Html.DataSource
*
*
*
*
#
*
Name
*
Place Of Birth
*
*
*
*
*
1
*
Hristo Stoichkov
*
Plovdiv, Bulgaria
*
*
*
2
*
Ronaldo Luis Nazario de Lima
*
Rio de Janeiro, Brazil
*
*
*
3
*
David Platt
*
Chadderton, Lancashire, England
*
*
*
*
* @example Remote.Custom.Render
*
*
* @example Remote.Custom.Error
*
*
*/
dataSource: undefined,
/** An array that holds the configurations of each column from the grid.
* @type array
* @example JS.Configuration
*
*
*/
columns: [],
/** Auto generate column for each field in the datasource when set to true.
* @type array
* @example sample
*
*
*/
autoGenerateColumns: false,
/** An object that holds the default configuration settings of each column from the grid.
* @type object
* @example sample
*
*
*/
defaultColumnSettings: {
/** If set to true the column will not be displayed in the grid. By default all columns are displayed.
* @alias column.hidden
* @type boolean
* @default false
* @example sample
*
*
*/
hidden: false,
/** The width of the column. Numeric values are treated as pixels.
* If the width is undefined the width of the column is not set and depends on the with of the table(grid).
* @alias column.width
* @type number|string
* @default undefined
* @example sample
*
*
*/
width: undefined,
/** Indicates if the column is sortable.
* If set to true the user can click the column header and sort the grid by the column source field.
* @alias column.sortable
* @type boolean|object
* @default false
* @example Remote.Base.Theme
*
*
* @example Local.Custom
*
*
* @example Remote.Bootstrap.3
*
*
* @example Remote.Bootstrap.4.FontAwesome
*
*
* @example Remote.Material.Design
*
*
*/
sortable: false,
/** Indicates the type of the column.
* @alias column.type
* @type checkbox|icon
* @default undefined
* @example sample
*
*
*/
type: undefined,
/** The caption that is going to be displayed in the header of the grid.
* @alias column.title
* @type string
* @default undefined
* @example sample
*
*
*/
title: undefined,
/** The field name to which the column is bound.
* If the column.title is not defined this value is used as column.title.
* @alias column.field
* @type string
* @default undefined
* @example sample
*
*
*/
field: undefined,
/** This setting control the alignment of the text in the cell.
* @alias column.align
* @type left|right|center|justify|initial|inherit
* @default "left"
* @example sample
*
*
*/
align: 'left',
/** The name(s) of css class(es) that are going to be applied to all cells inside that column, except the header cell.
* @alias column.cssClass
* @type string
* @default undefined
* @example sample
*
*
*
*/
cssClass: undefined,
/** The name(s) of css class(es) that are going to be applied to the header cell of that column.
* @alias column.headerCssClass
* @type string
* @default undefined
* @example sample
*
*
*
*/
headerCssClass: undefined,
/** The text for the cell tooltip.
* @alias column.tooltip
* @type string
* @default undefined
* @example sample
*
*
*/
tooltip: undefined,
/** Css class for icon that is going to be in use for the cell.
* This setting can be in use only with combination of type icon.
* @alias column.icon
* @type string
* @default undefined
* @example sample
*
*
*/
icon: undefined,
/** Configuration object with event names as keys and functions as values that are going to be bind to each cell from the column.
* Each function is going to receive event information as a parameter with info in the "data" field for id, field name and record data.
* @alias column.events
* @type object
* @default undefined
* @example javascript.configuration
*
*
* @example html.configuration
*
*
*
*
ID
*
Name
*
Place Of Birth
*
*
*
*
*
*/
events: undefined,
/** Format the date when the type of the column is date.
* This configuration setting is going to work only if you have implementation of format method for the Date object.
* You can use external libraries like http://blog.stevenlevithan.com/archives/date-time-format for that.
* @alias column.format
* @type string
* @default undefined
* @example sample
*
*
*
*
*/
format: undefined,
/** Number of decimal digits after the decimal point.
* @alias column.decimalDigits
* @type number
* @default undefined
*/
decimalDigits: undefined,
/** Template for the content in the column.
* Use curly brackets "{}" to wrap the names of data source columns from server response.
* @alias column.tmpl
* @type string
* @default undefined
* @example sample
*
*
*/
tmpl: undefined,
/** If set to true stop event propagation when event occur.
* @alias column.stopPropagation
* @type boolean
* @default false
* @example sample
*
*
*/
stopPropagation: false,
/** A renderer is an 'interceptor' function which can be used to transform data (value, appearance, etc.) before it is rendered.
* @additionalinfo If the renderer function return a value, then this value is going to be automatically set as value of the cell.
* If the renderer function doesn't return a value, then you have to set the content of the cell manually.
* @alias column.renderer
* @type function
* @default undefined
* @param {string} value - the record field value
* @param {object} record - the data of the row record
* @param {object} $cell - the current table cell presented as jquery object
* @param {object} $displayEl - inner div element for display of the cell value presented as jquery object
* @param {string} id - the id of the record
* @example sample
*
*
*/
renderer: undefined
},
mapping: {
/** The name of the object in the server response, that contains array with records, that needs to be display in the grid.
* @alias mapping.dataField
* @type string
* @default "records"
*/
dataField: 'records',
/** The name of the object in the server response, that contains the number of all records on the server.
* @alias mapping.totalRecordsField
* @type string
* @default "total"
*/
totalRecordsField: 'total'
},
params: {},
paramNames: {
/** The name of the parameter that is going to send the name of the column for sorting.
* The "sortable" setting for at least one column should be enabled in order this parameter to be in use.
* @alias paramNames.sortBy
* @type string
* @default "sortBy"
*/
sortBy: 'sortBy',
/** The name of the parameter that is going to send the direction for sorting.
* The "sortable" setting for at least one column should be enabled in order this parameter to be in use.
* @alias paramNames.direction
* @type string
* @default "direction"
*/
direction: 'direction'
},
/** The name of the UI library that is going to be in use. Currently we support jQuery UI, Bootstrap and Material Design.
* @additionalinfo The css files for jQuery UI, Bootstrap or Material Design should be manually included to the page where the grid is in use.
* @type (base|jqueryui|bootstrap|bootstrap4|materialdesign)
* @default "base"
* @example base.theme
*
*
* @example jqueryui
*
*
* @example Bootstrap
*
*
* @example Material.Design.With.Icons
*
*
* @example Material.Design.Without.Icons
*
*
* @example Bootstrap.4.Font.Awesome
*
*
*/
uiLibrary: 'base',
/** The name of the icons library that is going to be in use. Currently we support Material Icons, Font Awesome and Glyphicons.
* @additionalinfo If you use Bootstrap 3 as uiLibrary, then the iconsLibrary is set to Glyphicons by default.
* If you use Material Design as uiLibrary, then the iconsLibrary is set to Material Icons by default.
* The css files for Material Icons, Font Awesome or Glyphicons should be manually included to the page where the grid is in use.
* @type (materialicons|fontawesome|glyphicons)
* @default undefined
* @example base.theme
*
*
*/
iconsLibrary: undefined,
/** The type of the row selection.
* If the type is set to multiple the user will be able to select more then one row from the grid.
* @type (single|multiple)
* @default "single"
* @example Multiple.Checkbox
*
*
* @example Single.Checkbox
*
*
*/
selectionType: 'single',
/** The type of the row selection mechanism.
* @additionalinfo If this setting is set to "basic" when the user select a row, then this row will be highlighted.
* If this setting is set to "checkbox" a column with checkboxes will appear as first row of the grid and when the user select a row, then this row will be highlighted and the checkbox selected.
* @type (basic|checkbox)
* @default "basic"
* @example sample
*
*
*/
selectionMethod: 'basic',
/** When this setting is enabled the content of the grid will be loaded automatically after the creation of the grid.
* @type boolean
* @default true
* @example disabled
*
*
* @example enabled
*
*
*/
autoLoad: true,
/** The text that is going to be displayed if the grid is empty.
* @type string
* @default "No records found."
* @example sample
*
*
*/
notFoundText: 'No records found.',
/** Width of the grid.
* @type number
* @default undefined
* @example sample
*
*
*/
width: undefined,
/** Minimum width of the grid.
* @type number
* @default undefined
*/
minWidth: undefined,
/** The size of the font in the grid.
* @type string
* @default undefined
* @example sample
*
*
*/
fontSize: undefined,
/** Name of column that contains the record id.
* @additionalinfo If you set primary key, we assume that this number is unique for each records presented in the grid.
* For example this should contains the column with primary key from your relation db table.
* If the primaryKey is undefined, we autogenerate id for each record in the table by starting from 1.
* @type string
* @default undefined
* @example defined
*
*
* @example undefined
*
*
*/
primaryKey: undefined,
/** The language that needs to be in use.
* @type string
* @default 'en-us'
* @example French.Bootstrap.Default
*
*
*
*/
beforeEmptyRowInsert: function ($grid, $row) {
$grid.triggerHandler('beforeEmptyRowInsert', [$row]);
},
/**
* Event fired before data binding takes place.
*
* @event dataBinding
* @param {object} e - event data
* @param {array} records - the list of records
* @example sample
*
*
*/
dataBinding: function ($grid, records) {
$grid.triggerHandler('dataBinding', [records]);
},
/**
* Event fires after the loading of the data in the grid.
*
* @event dataBound
* @param {object} e - event data
* @param {array} records - the list of records
* @param {number} totalRecords - the number of the all records that can be presented in the grid
* @example sample
*
*
*/
dataBound: function ($grid, records, totalRecords) {
$grid.triggerHandler('dataBound', [records, totalRecords]);
},
/**
* Event fires after insert of a row in the grid during the loading of the data.
* @event rowDataBound
* @param {object} e - event data
* @param {object} $row - the row presented as jquery object
* @param {string} id - the id of the record
* @param {object} record - the data of the row record
* @example sample
*
*
*/
rowDataBound: function ($grid, $row, id, record) {
$grid.triggerHandler('rowDataBound', [$row, id, record]);
},
/**
* Event fires after insert of a cell in the grid during the loading of the data
*
* @event cellDataBound
* @param {object} e - event data
* @param {object} $displayEl - inner div element for display of the cell value presented as jquery object
* @param {string} id - the id of the record
* @param {object} column - the column configuration data
* @param {object} record - the data of the row record
* @example sample
*
*
*/
cellDataBound: function ($grid, $displayEl, id, column, record) {
$grid.triggerHandler('cellDataBound', [$displayEl, id, column, record]);
},
/**
* Event fires on selection of row
*
* @event rowSelect
* @param {object} e - event data
* @param {object} $row - the row presented as jquery object
* @param {string} id - the id of the record
* @param {object} record - the data of the row record
* @example sample
*
*
*/
rowSelect: function ($grid, $row, id, record) {
$grid.triggerHandler('rowSelect', [$row, id, record]);
},
/**
* Event fires on un selection of row
*
* @event rowUnselect
* @param {object} e - event data
* @param {object} $row - the row presented as jquery object
* @param {string} id - the id of the record
* @param {object} record - the data of the row record
* @example sample
*
*
*/
rowUnselect: function ($grid, $row, id, record) {
$grid.triggerHandler('rowUnselect', [$row, id, record]);
},
/**
* Event fires before deletion of row in the grid.
* @event rowRemoving
* @param {object} e - event data
* @param {object} $row - the row presented as jquery object
* @param {string} id - the id of the record
* @param {object} record - the data of the row record
* @example sample
*
*
*
*/
rowRemoving: function ($grid, $row, id, record) {
$grid.triggerHandler('rowRemoving', [$row, id, record]);
},
/**
* Event fires when the grid.destroy method is called.
*
* @event destroying
* @param {object} e - event data
* @example sample
*
*
*
*
*/
destroying: function ($grid) {
$grid.triggerHandler('destroying');
},
/**
* Event fires when column is hidding
*
* @event columnHide
* @param {object} e - event data
* @param {object} column - The data about the column that is hidding
* @example sample
*
*
*/
columnHide: function ($grid, column) {
$grid.triggerHandler('columnHide', [column]);
},
/**
* Event fires when column is showing
*
* @event columnShow
* @param {object} e - event data
* @param {object} column - The data about the column that is showing
* @example sample
*
*
*/
columnShow: function ($grid, column) {
$grid.triggerHandler('columnShow', [column]);
},
/**
* Event fires when grid is initialized.
*
* @event initialized
* @param {object} e - event data
* @example sample
*
*
*/
initialized: function ($grid) {
$grid.triggerHandler('initialized');
},
/**
* Event fires when the grid data is filtered.
*
* @additionalinfo This event is firing only when you use local dataSource, because the filtering with remote dataSource needs to be done on the server side.
* @event dataFiltered
* @param {object} e - event data
* @param {object} records - The records after the filtering.
* @example sample
*
*
*/
dataFiltered: function ($grid, records) {
$grid.triggerHandler('dataFiltered', [records]);
}
};
/*global gj $*/
gj.grid.methods = {
init: function (jsConfig) {
this.attr('data-type', 'grid');
gj.widget.prototype.init.call(this, jsConfig, 'grid');
gj.grid.methods.initialize(this);
if (this.data('autoLoad')) {
this.reload();
}
return this;
},
getConfig: function (jsConfig, type) {
var config = gj.widget.prototype.getConfig.call(this, jsConfig, type);
gj.grid.methods.setDefaultColumnConfig(config.columns, config.defaultColumnSettings);
return config;
},
setDefaultColumnConfig: function (columns, defaultColumnSettings) {
var column, i;
if (columns && columns.length) {
for (i = 0; i < columns.length; i++) {
column = $.extend(true, {}, defaultColumnSettings);
$.extend(true, column, columns[i]);
columns[i] = column;
}
}
},
getHTMLConfig: function () {
var result = gj.widget.prototype.getHTMLConfig.call(this);
result.columns = [];
this.find('thead > tr > th').each(function () {
var $el = $(this),
title = $el.text(),
config = gj.widget.prototype.getHTMLConfig.call($el);
config.title = title;
if (!config.field) {
config.field = title;
}
if (config.events) {
config.events = gj.grid.methods.eventsParser(config.events);
}
result.columns.push(config);
});
return result;
},
eventsParser: function (events) {
var result = {}, list, i, key, func, position;
list = events.split(',');
for (i = 0; i < list.length; i++) {
position = list[i].indexOf(':');
if (position > 0) {
key = $.trim(list[i].substr(0, position));
func = $.trim(list[i].substr(position + 1, list[i].length));
result[key] = eval('window.' + func); //window[func]; //TODO: eveluate functions from string
}
}
return result;
},
initialize: function ($grid) {
var data = $grid.data(),
$wrapper = $grid.parent('div[data-role="wrapper"]');
if ($wrapper.length === 0) {
$wrapper = $('').addClass(data.style.wrapper); //The css class needs to be added before the wrapping, otherwise doesn't work.
$grid.wrap($wrapper);
} else {
$wrapper.addClass(data.style.wrapper);
}
if (data.width) {
$grid.parent().css('width', data.width);
}
if (data.minWidth) {
$grid.css('min-width', data.minWidth);
}
if (data.fontSize) {
$grid.css('font-size', data.fontSize);
}
$grid.addClass(data.style.table);
if ('checkbox' === data.selectionMethod) {
data.columns = [{
title: '',
field: data.primaryKey || '',
width: data.defaultIconColumnWidth,
align: 'center',
type: 'checkbox',
role: 'selectRow',
events: {
click: function (e) {
gj.grid.methods.setSelected($grid, e.data.id, $(this).closest('tr'));
}
}
}].concat(data.columns);
}
$grid.append($(''));
gj.grid.methods.renderHeader($grid);
gj.grid.methods.appendEmptyRow($grid, ' ');
gj.grid.events.initialized($grid);
},
renderHeader: function ($grid) {
var data, columns, style, $thead, $row, $cell, i, $checkAllBoxes;
data = $grid.data();
columns = data.columns;
style = data.style.header;
$thead = $grid.children('thead');
if ($thead.length === 0) {
$thead = $('');
$grid.prepend($thead);
}
$row = $('
');
for (i = 0; i < columns.length; i += 1) {
$cell = $('
');
if (columns[i].width) {
$cell.attr('width', columns[i].width);
} else if (columns[i].type === 'checkbox') {
$cell.attr('width', data.defaultIconColumnWidth);
}
$cell.addClass(style.cell);
if (columns[i].headerCssClass) {
$cell.addClass(columns[i].headerCssClass);
}
$cell.css('text-align', columns[i].align || 'left');
if (columns[i].sortable) {
$cell.addClass(style.sortable);
$cell.on('click', gj.grid.methods.createSortHandler($grid, $cell, columns[i]));
}
if ('checkbox' === data.selectionMethod && 'multiple' === data.selectionType &&
'checkbox' === columns[i].type && 'selectRow' === columns[i].role) {
$checkAllBoxes = $cell.find('input[data-role="selectAll"]');
if ($checkAllBoxes.length === 0) {
$checkAllBoxes = $('');
$cell.append($checkAllBoxes);
}
$checkAllBoxes.off('click').on('click', function () {
if (this.checked) {
$grid.selectAll();
} else {
$grid.unSelectAll();
}
});
} else {
$cell.append($('').html(typeof (columns[i].title) === 'undefined' ? columns[i].field : columns[i].title));
}
if (columns[i].hidden) {
$cell.hide();
}
$row.append($cell);
}
$thead.empty().append($row);
},
createSortHandler: function ($grid, $cell, column) {
return function () {
var data, params = {};
if ($grid.count() > 0) {
data = $grid.data();
params[data.paramNames.sortBy] = column.field;
column.direction = (column.direction === 'asc' ? 'desc' : 'asc');
params[data.paramNames.direction] = column.direction;
$grid.reload(params);
}
};
},
updateHeader: function ($grid) {
var $sortIcon,
data = $grid.data(),
style = data.style.header,
sortBy = data.params[data.paramNames.sortBy],
direction = data.params[data.paramNames.direction];
$grid.find('thead tr th [data-role="sorticon"]').remove();
if (sortBy) {
position = gj.grid.methods.getColumnPosition($grid.data('columns'), sortBy);
$cell = $grid.find('thead tr th:eq(' + position + ')');
$sortIcon = $('').append(('desc' === direction) ? data.icons.desc : data.icons.asc);
$cell.append($sortIcon);
}
},
useHtmlDataSource: function ($grid, data) {
var dataSource = [], i, j, $cells, record,
$rows = $grid.find('tbody tr[data-role != "empty"]');
for (i = 0; i < $rows.length; i++) {
$cells = $($rows[i]).find('td');
record = {};
for (j = 0; j < $cells.length; j++) {
record[data.columns[j].field] = $($cells[j]).html();
}
dataSource.push(record);
}
data.dataSource = dataSource;
},
startLoading: function ($grid) {
var $tbody, $cover, $loading, width, height, top, data;
gj.grid.methods.stopLoading($grid);
data = $grid.data();
if (0 === $grid.outerHeight()) {
return;
}
$tbody = $grid.children('tbody');
width = $tbody.outerWidth(false);
height = $tbody.outerHeight(false);
top = Math.abs($grid.parent().offset().top - $tbody.offset().top);
$cover = $('').addClass(data.style.loadingCover).css({
width: width,
height: height,
top: top
});
$loading = $('
Loading...
').addClass(data.style.loadingText);
$loading.insertAfter($grid);
$cover.insertAfter($grid);
$loading.css({
top: top + (height / 2) - ($loading.outerHeight(false) / 2),
left: (width / 2) - ($loading.outerWidth(false) / 2)
});
},
stopLoading: function ($grid) {
$grid.parent().find('div[data-role="loading-cover"]').remove();
$grid.parent().find('div[data-role="loading-text"]').remove();
},
createAddRowHoverHandler: function ($row, cssClass) {
return function () {
$row.addClass(cssClass);
};
},
createRemoveRowHoverHandler: function ($row, cssClass) {
return function () {
$row.removeClass(cssClass);
};
},
appendEmptyRow: function ($grid, caption) {
var data, $row, $cell, $wrapper;
data = $grid.data();
$row = $('
');
$cell = $('
').css({ width: '100%', 'text-align': 'center' });
$cell.attr('colspan', gj.grid.methods.countVisibleColumns($grid));
$wrapper = $('').html(caption || data.notFoundText);
$cell.append($wrapper);
$row.append($cell);
gj.grid.events.beforeEmptyRowInsert($grid, $row);
$grid.append($row);
},
autoGenerateColumns: function ($grid, records) {
var names, value, type, i, data = $grid.data();
data.columns = [];
if (records.length > 0) {
names = Object.getOwnPropertyNames(records[0]);
for (i = 0; i < names.length; i++) {
value = records[0][names[i]];
type = 'text';
if (value) {
if (typeof value === 'number') {
type = 'number';
} else if (value.indexOf('/Date(') > -1) {
type = 'date';
}
}
data.columns.push({ field: names[i], type: type });
}
gj.grid.methods.setDefaultColumnConfig(data.columns, data.defaultColumnSettings);
}
gj.grid.methods.renderHeader($grid);
},
loadData: function ($grid) {
var data, records, i, recLen, rowCount, $tbody, $rows, $row;
data = $grid.data();
records = $grid.getAll();
gj.grid.events.dataBinding($grid, records);
recLen = records.length;
gj.grid.methods.stopLoading($grid);
if (data.autoGenerateColumns) {
gj.grid.methods.autoGenerateColumns($grid, records);
}
$tbody = $grid.find('tbody');
if ('checkbox' === data.selectionMethod && 'multiple' === data.selectionType) {
$grid.find('thead input[data-role="selectAll"]').prop('checked', false);
}
$tbody.children('tr').not('[data-role="row"]').remove();
if (0 === recLen) {
$tbody.empty();
gj.grid.methods.appendEmptyRow($grid);
}
$rows = $tbody.children('tr');
rowCount = $rows.length;
for (i = 0; i < rowCount; i++) {
if (i < recLen) {
$row = $rows.eq(i);
gj.grid.methods.renderRow($grid, $row, records[i], i);
} else {
$tbody.find('tr[data-role="row"]:gt(' + (i - 1) + ')').remove();
break;
}
}
for (i = rowCount; i < recLen; i++) {
gj.grid.methods.renderRow($grid, null, records[i], i);
}
gj.grid.events.dataBound($grid, records, data.totalRecords);
},
getId: function (record, primaryKey, position) {
return (primaryKey && record[primaryKey]) ? record[primaryKey] : position;
},
renderRow: function ($grid, $row, record, position) {
var id, $cell, i, data, mode;
data = $grid.data();
if (!$row || $row.length === 0) {
mode = 'create';
$row = $('
');
$grid.children('tbody').append($row);
$row.on('mouseenter', gj.grid.methods.createAddRowHoverHandler($row, data.style.content.rowHover));
$row.on('mouseleave', gj.grid.methods.createRemoveRowHoverHandler($row, data.style.content.rowHover));
} else {
mode = 'update';
$row.removeClass(data.style.content.rowSelected).removeAttr('data-selected').off('click');
}
id = gj.grid.methods.getId(record, data.primaryKey, (position + 1));
$row.attr('data-position', position + 1);
if (data.selectionMethod !== 'checkbox') {
$row.on('click', gj.grid.methods.createRowClickHandler($grid, id));
}
for (i = 0; i < data.columns.length; i++) {
if (mode === 'update') {
$cell = $row.find('td:eq(' + i + ')');
gj.grid.methods.renderCell($grid, $cell, data.columns[i], record, id);
} else {
$cell = gj.grid.methods.renderCell($grid, null, data.columns[i], record, id);
$row.append($cell);
}
}
gj.grid.events.rowDataBound($grid, $row, id, record);
},
renderCell: function ($grid, $cell, column, record, id, mode) {
var $displayEl, key;
if (!$cell || $cell.length === 0) {
$cell = $('
').css('text-align', column.align || 'left');
$displayEl = $('');
if (column.cssClass) {
$cell.addClass(column.cssClass);
}
$cell.append($displayEl);
mode = 'create';
} else {
$displayEl = $cell.find('div[data-role="display"]');
mode = 'update';
}
gj.grid.methods.renderDisplayElement($grid, $displayEl, column, record, id, mode);
//remove all event handlers
if ('update' === mode) {
$cell.off();
$displayEl.off();
}
if (column.events) {
for (key in column.events) {
if (column.events.hasOwnProperty(key)) {
$cell.on(key, { id: id, field: column.field, record: record }, gj.grid.methods.createCellEventHandler(column, column.events[key]));
}
}
}
if (column.hidden) {
$cell.hide();
}
gj.grid.events.cellDataBound($grid, $displayEl, id, column, record);
return $cell;
},
createCellEventHandler: function (column, func) {
return function (e) {
if (column.stopPropagation) {
e.stopPropagation();
}
func.call(this, e);
};
},
renderDisplayElement: function ($grid, $displayEl, column, record, id, mode) {
var text, $checkbox;
if ('checkbox' === column.type) {
if ('create' === mode) {
$checkbox = $('').attr('type', 'checkbox').val(id);
column.role && $checkbox.attr('data-role', column.role);
$displayEl.append($checkbox);
} else {
$displayEl.find('input[type="checkbox"]').val(id).prop('checked', false);
}
} else if ('icon' === column.type) {
if ('create' === mode) {
$displayEl.append($('')
.addClass($grid.data().uiLibrary === 'bootstrap' ? 'glyphicon' : 'ui-icon')
.addClass(column.icon).css({ cursor: 'pointer' }));
column.stopPropagation = true;
}
} else if (column.tmpl) {
text = column.tmpl;
column.tmpl.replace(/\{(.+?)\}/g, function ($0, $1) {
text = text.replace($0, gj.grid.methods.formatText(record[$1], column));
});
$displayEl.html(text);
} else if (column.renderer && typeof (column.renderer) === 'function') {
text = column.renderer(record[column.field], record, $displayEl.parent(), $displayEl, id, $grid);
if (text) {
$displayEl.html(text);
}
} else {
text = gj.grid.methods.formatText(record[column.field], column);
if (!column.tooltip && text) {
$displayEl.attr('title', text);
}
$displayEl.text(text);
}
if (column.tooltip && 'create' === mode) {
$displayEl.attr('title', column.tooltip);
}
},
formatText: function (text, column) {
var dt, day, month, parts;
if (text && column.type) {
switch (column.type) {
case 'date':
if (text.indexOf('/Date(') > -1) {
dt = new Date(parseInt(text.substr(6), 10));
} else {
parts = text.match(/(\d+)/g);
// new Date(year, month, date, hours, minutes, seconds);
dt = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]); // months are 0-based
}
if (dt.format && column.format) {
text = dt.format(column.format); //using 3rd party plugin "Date Format 1.2.3 by (c) 2007-2009 Steven Levithan "
} else {
day = dt.getDate().toString().length === 2 ? dt.getDate() : '0' + dt.getDate();
month = (dt.getMonth() + 1).toString();
month = month.length === 2 ? month : '0' + month;
text = month + '/' + day + '/' + dt.getFullYear();
}
break;
}
} else {
text = (typeof (text) === 'undefined' || text === null) ? '' : text.toString();
}
if (column.decimalDigits && text) {
text = parseFloat(text).toFixed(column.decimalDigits);
}
return text;
},
setRecordsData: function ($grid, response) {
var records = [],
totalRecords = 0,
data = $grid.data();
if ($.isArray(response)) {
records = response;
totalRecords = response.length;
} else if (data && data.mapping && $.isArray(response[data.mapping.dataField])) {
records = response[data.mapping.dataField];
totalRecords = response[data.mapping.totalRecordsField];
if (!totalRecords || isNaN(totalRecords)) {
totalRecords = 0;
}
}
$grid.data('records', records);
$grid.data('totalRecords', totalRecords);
return records;
},
createRowClickHandler: function ($grid, id) {
return function () {
gj.grid.methods.setSelected($grid, id, $(this));
};
},
selectRow: function ($grid, data, $row, id) {
var $checkbox;
$row.addClass(data.style.content.rowSelected);
$row.attr('data-selected', 'true');
if ('checkbox' === data.selectionMethod) {
$checkbox = $row.find('input[type="checkbox"][data-role="selectRow"]');
$checkbox.length && !$checkbox.prop('checked') && $checkbox.prop('checked', true);
if ('multiple' === data.selectionType && $grid.getSelections().length === $grid.count(false)) {
$grid.find('thead input[data-role="selectAll"]').prop('checked', true);
}
}
gj.grid.events.rowSelect($grid, $row, id, $grid.getById(id));
},
unselectRow: function ($grid, data, $row, id) {
var $checkbox;
if ($row.attr('data-selected') === 'true') {
$row.removeClass(data.style.content.rowSelected);
if ('checkbox' === data.selectionMethod) {
$checkbox = $row.find('td input[type="checkbox"][data-role="selectRow"]');
$checkbox.length && $checkbox.prop('checked') && $checkbox.prop('checked', false);
if ('multiple' === data.selectionType) {
$grid.find('thead input[data-role="selectAll"]').prop('checked', false);
}
}
$row.removeAttr('data-selected');
gj.grid.events.rowUnselect($grid, $row, id, $grid.getById(id));
}
},
setSelected: function ($grid, id, $row) {
var data = $grid.data();
if (!$row || !$row.length) {
$row = gj.grid.methods.getRowById($grid, id);
}
if ($row) {
if ($row.attr('data-selected') === 'true') {
gj.grid.methods.unselectRow($grid, data, $row, id);
} else {
if ('single' === data.selectionType) {
$row.siblings('[data-selected="true"]').each(function () {
var $row = $(this),
id = gj.grid.methods.getId($row, data.primaryKey, $row.data('position'));
gj.grid.methods.unselectRow($grid, data, $row, id);
});
}
gj.grid.methods.selectRow($grid, data, $row, id);
}
}
return $grid;
},
selectAll: function ($grid) {
var data = $grid.data();
$grid.find('tbody tr[data-role="row"]').each(function () {
var $row = $(this);
gj.grid.methods.selectRow($grid, data, $row, $grid.get($row.data('position')));
});
$grid.find('thead input[data-role="selectAll"]').prop('checked', true);
return $grid;
},
unSelectAll: function ($grid) {
var data = $grid.data();
$grid.find('tbody tr').each(function () {
var $row = $(this);
gj.grid.methods.unselectRow($grid, data, $row, $grid.get($row.data('position')));
$row.find('input[type="checkbox"][data-role="selectRow"]').prop('checked', false);
});
$grid.find('thead input[data-role="selectAll"]').prop('checked', false);
return $grid;
},
getSelected: function ($grid) {
var result = null, selections, record, position;
selections = $grid.find('tbody>tr[data-selected="true"]');
if (selections.length > 0) {
position = $(selections[0]).data('position');
record = $grid.get(position);
result = gj.grid.methods.getId(record, $grid.data().primaryKey, position);
}
return result;
},
getSelectedRows: function ($grid) {
var data = $grid.data();
return $grid.find('tbody>tr[data-selected="true"]');
},
getSelections: function ($grid) {
var result = [], position, record, $selections = gj.grid.methods.getSelectedRows($grid);
if (0 < $selections.length) {
$selections.each(function () {
position = $(this).data('position');
record = $grid.get(position);
result.push(gj.grid.methods.getId(record, $grid.data().primaryKey, position));
});
}
return result;
},
getById: function ($grid, id) {
var result = null, i, primaryKey = $grid.data('primaryKey'), records = $grid.data('records');
if (primaryKey) {
for (i = 0; i < records.length; i++) {
if (records[i][primaryKey] == id) {
result = records[i];
break;
}
}
} else {
result = $grid.get(id);
}
return result;
},
getRecVPosById: function ($grid, id) {
var result = id, i, data = $grid.data();
if (data.primaryKey) {
for (i = 0; i < data.dataSource.length; i++) {
if (data.dataSource[i][data.primaryKey] == id) {
result = i;
break;
}
}
}
return result;
},
getRowById: function ($grid, id) {
var records = $grid.getAll(false),
primaryKey = $grid.data('primaryKey'),
$result = undefined,
position,
i;
if (primaryKey) {
for (i = 0; i < records.length; i++) {
if (records[i][primaryKey] == id) {
position = i + 1;
break;
}
}
} else {
position = id;
}
if (position) {
$result = $grid.find('tbody > tr[data-position="' + position + '"]');
}
return $result;
},
getByPosition: function ($grid, position) {
return $grid.getAll(false)[position - 1];
},
getColumnPosition: function (columns, field) {
var position = -1, i;
for (i = 0; i < columns.length; i++) {
if (columns[i].field === field) {
position = i;
break;
}
}
return position;
},
getColumnInfo: function ($grid, field) {
var i, result = {}, data = $grid.data();
for (i = 0; i < data.columns.length; i += 1) {
if (data.columns[i].field === field) {
result = data.columns[i];
break;
}
}
return result;
},
getCell: function ($grid, id, field) {
var position, $row, $result = null;
position = gj.grid.methods.getColumnPosition($grid.data('columns'), field);
if (position > -1) {
$row = gj.grid.methods.getRowById($grid, id);
$result = $row.find('td:eq(' + position + ') div[data-role="display"]');
}
return $result;
},
setCellContent: function ($grid, id, field, value) {
var column, $displayEl = gj.grid.methods.getCell($grid, id, field);
if ($displayEl) {
$displayEl.empty();
if (typeof (value) === 'object') {
$displayEl.append(value);
} else {
column = gj.grid.methods.getColumnInfo($grid, field);
gj.grid.methods.renderDisplayElement($grid, $displayEl, column, $grid.getById(id), id, 'update');
}
}
},
clone: function (source) {
var target = [];
$.each(source, function () {
target.push(this.clone());
});
return target;
},
getAll: function ($grid) {
return $grid.data('records');
},
countVisibleColumns: function ($grid) {
var columns, count, i;
columns = $grid.data().columns;
count = 0;
for (i = 0; i < columns.length; i++) {
if (columns[i].hidden !== true) {
count++;
}
}
return count;
},
clear: function ($grid, showNotFoundText) {
var data = $grid.data();
$grid.xhr && $grid.xhr.abort();
$grid.children('tbody').empty();
data.records = [];
gj.grid.methods.stopLoading($grid);
gj.grid.methods.appendEmptyRow($grid, showNotFoundText ? data.notFoundText : ' ');
gj.grid.events.dataBound($grid, [], 0);
return $grid;
},
render: function ($grid, response) {
if (response) {
gj.grid.methods.setRecordsData($grid, response);
gj.grid.methods.updateHeader($grid);
gj.grid.methods.loadData($grid);
}
return $grid;
},
filter: function ($grid) {
var field, column,
data = $grid.data(),
records = data.dataSource.slice();
if (data.params[data.paramNames.sortBy]) {
column = gj.grid.methods.getColumnInfo($grid, data.params[data.paramNames.sortBy]);
records.sort(column.sortable.sorter ? column.sortable.sorter(column.direction, column) : gj.grid.methods.createDefaultSorter(column.direction, column.field));
}
for (field in data.params) {
if (data.params[field] && !data.paramNames[field]) {
records = $.grep(records, function (e) {
return e[field].indexOf(data.params[field]) > -1;
});
}
}
gj.grid.events.dataFiltered($grid, records);
return records;
},
createDefaultSorter: function (direction, field) {
return function (recordA, recordB) {
var a = (recordA[field] || '').toString(),
b = (recordB[field] || '').toString();
return (direction === 'asc') ? a.localeCompare(b) : b.localeCompare(a);
};
},
destroy: function ($grid, keepTableTag, keepWrapperTag) {
var data = $grid.data();
if (data) {
gj.grid.events.destroying($grid);
gj.grid.methods.stopLoading($grid);
$grid.xhr && $grid.xhr.abort();
$grid.off();
if (keepWrapperTag === false && $grid.parent('div[data-role="wrapper"]').length > 0) {
$grid.unwrap();
}
$grid.removeData();
if (keepTableTag === false) {
$grid.remove();
} else {
$grid.removeClass().empty();
}
$grid.removeAttr('data-type');
}
return $grid;
},
showColumn: function ($grid, field) {
var data = $grid.data(),
position = gj.grid.methods.getColumnPosition(data.columns, field),
$cells;
if (position > -1) {
$grid.find('thead>tr').each(function() {
$(this).children('th').eq(position).show();
});
$.each($grid.find('tbody>tr'), function () {
$(this).children('td').eq(position).show();
});
data.columns[position].hidden = false;
$cells = $grid.find('tbody > tr[data-role="empty"] > td');
if ($cells && $cells.length) {
$cells.attr('colspan', gj.grid.methods.countVisibleColumns($grid));
}
gj.grid.events.columnShow($grid, data.columns[position]);
}
return $grid;
},
hideColumn: function ($grid, field) {
var data = $grid.data(),
position = gj.grid.methods.getColumnPosition(data.columns, field),
$cells;
if (position > -1) {
$grid.find('thead>tr').each(function () {
$(this).children('th').eq(position).hide();
});
$.each($grid.find('tbody>tr'), function () {
$(this).children('td').eq(position).hide();
});
data.columns[position].hidden = true;
$cells = $grid.find('tbody > tr[data-role="empty"] > td');
if ($cells && $cells.length) {
$cells.attr('colspan', gj.grid.methods.countVisibleColumns($grid));
}
gj.grid.events.columnHide($grid, data.columns[position]);
}
return $grid;
},
isLastRecordVisible: function () {
return true;
},
addRow: function ($grid, record) {
var data = $grid.data();
data.totalRecords = $grid.data('totalRecords') + 1;
gj.grid.events.dataBinding($grid, [record]);
data.records.push(record);
if ($.isArray(data.dataSource)) {
data.dataSource.push(record);
}
if (gj.grid.methods.isLastRecordVisible($grid)) {
gj.grid.methods.renderRow($grid, null, record, $grid.count() - 1);
}
gj.grid.events.dataBound($grid, [record], data.totalRecords);
return $grid;
},
updateRow: function ($grid, id, record) {
var $row = gj.grid.methods.getRowById($grid, id);
$grid.data('records')[$row.data('position') - 1] = record;
gj.grid.methods.renderRow($grid, $row, record, $row.index());
return $grid;
},
removeRow: function ($grid, id) {
var position,
data = $grid.data(),
$row = gj.grid.methods.getRowById($grid, id);
gj.grid.events.rowRemoving($grid, $row, id, $grid.getById(id));
if ($.isArray(data.dataSource)) {
position = gj.grid.methods.getRecVPosById($grid, id);
data.dataSource.splice(position, 1);
}
$grid.reload();
return $grid;
},
count: function ($grid, includeAllRecords) {
return includeAllRecords ? $grid.data().totalRecords : $grid.getAll().length;
}
};
/**
* @widget Grid
* @plugin Base
*/
gj.grid.widget = function ($grid, arguments) {
var self = this,
methods = gj.grid.methods;
/**
* Reload the data in the grid from a data source.
* @method
* @param {object} params - An object that contains a list with parameters that are going to be send to the server.
* @fires beforeEmptyRowInsert, dataBinding, dataBound, cellDataBound
* @return grid
* @example sample
*
*
*
*
*
*/
self.reload = function (params) {
methods.startLoading(this);
return gj.widget.prototype.reload.call(this, params);
};
/**
* Clear the content in the grid.
* @method
* @param {boolean} showNotFoundText - Indicates if the "Not Found" text is going to show after the clearing of the grid.
* @return grid
* @example sample
*
*
*
*
*/
self.clear = function (showNotFoundText) {
return methods.clear(this, showNotFoundText);
};
/**
* Return the number of records in the grid. By default return only the records that are visible in the grid.
* @method
* @param {boolean} includeAllRecords - include records that are not visible when you are using local dataSource.
* @return number
* @example Local.DataSource
*
*
*
*
*
* @example Remote.DataSource
*
*
*
*
*
*/
self.count = function (includeAllRecords) {
return methods.count(this, includeAllRecords);
};
/**
* Render data in the grid
* @method
* @param {object} response - An object that contains the data that needs to be loaded in the grid.
* @fires beforeEmptyRowInsert, dataBinding, dataBound, cellDataBound
* @return grid
* @example sample
*
*
*/
self.render = function (response) {
return methods.render($grid, response);
};
/**
* Destroy the grid. This method remove all data from the grid and all events attached to the grid.
* @additionalinfo The grid table tag and wrapper tag are kept by default after the execution of destroy method,
* but you can remove them if you pass false to the keepTableTag and keepWrapperTag parameters.
* @method
* @param {boolean} keepTableTag - If this flag is set to false, the table tag will be removed from the HTML dom tree.
* @param {boolean} keepWrapperTag - If this flag is set to false, the table wrapper tag will be removed from the HTML dom tree.
* @fires destroying
* @return void
* @example keep.wrapper.and.table
*
*
*
*
*
* @example remove.wrapper.and.table
*
*
*
*
*/
self.destroy = function (keepTableTag, keepWrapperTag) {
return methods.destroy(this, keepTableTag, keepWrapperTag);
};
/**
* Select a row from the grid based on id parameter.
* @method
* @param {string} id - The id of the row that needs to be selected
* @return grid
* @example sample
*
*
*
*
*
*/
self.setSelected = function (id) {
return methods.setSelected(this, id);
};
/**
* Return the id of the selected record.
* If the multiple selection method is one this method is going to return only the id of the first selected record.
* @method
* @return string
* @example sample
*
*
*
*
*/
self.getSelected = function () {
return methods.getSelected(this);
};
/**
* Return an array with the ids of the selected record.
* @method
* @return array
* @example sample
*
*
*
*
*/
self.getSelections = function () {
return methods.getSelections(this);
};
/**
* Select all records from the grid.
* @method
* @return grid
* @example sample
*
*
*
*
*/
self.selectAll = function () {
return methods.selectAll(this);
};
/**
* Unselect all records from the grid.
* @method
* @return void
* @example sample
*
*
*
*
*
*/
self.unSelectAll = function () {
return methods.unSelectAll(this);
};
/**
* Return record by id of the record.
* @method
* @param {string} id - The id of the row that needs to be returned.
* @return object
* @example sample
*
*
*
*
*/
self.getById = function (id) {
return methods.getById(this, id);
};
/**
* Return record from the grid based on position.
* @method
* @param {number} position - The position of the row that needs to be return.
* @return object
* @example sample
*
*
*
*
*/
self.get = function (position) {
return methods.getByPosition(this, position);
};
/**
* Return an array with all records presented in the grid.
* @method
* @param {boolean} includeAllRecords - include records that are not visible when you are using local dataSource.
* @return number
* @example Local.DataSource
*
*
*
*
*
* @example Remote.DataSource
*
*
*
*
*
*/
self.getAll = function (includeAllRecords) {
return methods.getAll(this, includeAllRecords);
};
/**
* Show hidden column.
* @method
* @param {string} field - The name of the field bound to the column.
* @return grid
* @example sample
*
*
*
*
*/
self.showColumn = function (field) {
return methods.showColumn(this, field);
};
/**
* Hide column from the grid.
* @method
* @param {string} field - The name of the field bound to the column.
* @return grid
* @example sample
*
*
*
*
*/
self.hideColumn = function (field) {
return methods.hideColumn(this, field);
};
/**
* Add new row to the grid.
* @method
* @param {object} record - Object with data for the new record.
* @return grid
* @example without.pagination
*
*
*
*
* @example with.pagination
*
*
*
*
*/
self.addRow = function (record) {
return methods.addRow(this, record);
};
/**
* Update row data.
* @method
* @param {string} id - The id of the row that needs to be updated
* @param {object} record - Object with data for the new record.
* @return grid
* @example sample
*
*
*/
self.updateRow = function (id, record) {
return methods.updateRow(this, id, record);
};
//TODO: needs to be removed
self.setCellContent = function (id, index, value) {
methods.setCellContent(this, id, index, value);
};
/**
* Remove row from the grid
* @additionalinfo This method is design to work only with local datasources. If you use remote datasource, you need to send a request to the server to remove the row and then reload the data in the grid.
* @method
* @param {string} id - Id of the record that needs to be removed.
* @return grid
* @example Without.Pagination
*
*
* @example With.Pagination
*
*
*/
self.removeRow = function (id) {
return methods.removeRow(this, id);
};
$.extend($grid, self);
if ('grid' !== $grid.attr('data-type')) {
methods.init.apply($grid, arguments);
}
return $grid;
}
gj.grid.widget.prototype = new gj.widget();
gj.grid.widget.constructor = gj.grid.widget;
gj.grid.widget.prototype.getConfig = gj.grid.methods.getConfig;
gj.grid.widget.prototype.getHTMLConfig = gj.grid.methods.getHTMLConfig;
(function ($) {
$.fn.grid = function (method) {
var $widget;
if (this && this.length) {
if (typeof method === 'object' || !method) {
return new gj.grid.widget(this, arguments);
} else {
$widget = new gj.grid.widget(this, null);
if ($widget[method]) {
return $widget[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
throw 'Method ' + method + ' does not exist.';
}
}
}
};
})(jQuery);
/**
* @widget Grid
* @plugin Expand Collapse Rows
*/
gj.grid.plugins.expandCollapseRows = {
config: {
base: {
/** Template for the content in the detail section of the row.
* Automatically add expand collapse column as a first column in the grid during initialization.
* @type string
* @default undefined
* @example Base.Theme
*
*
* @example Bootstrap.3
*
*
* @example Bootstrap.4.Font.Awesome
*
*
* @example Material.Design
*
*
*/
detailTemplate: undefined,
/** If set try to persist the state of expanded rows.
* You need to specify primaryKey on the initialization of the grid in order to enable this feature.
* @default true
* @example True
*
*
*
*
Expand row, then change the page and return back to the page with expanded row in order to see that the expansion is kept.
*
*
*
*
*
*/
keepExpandedRows: true
}
},
'private': {
detailExpand: function ($grid, $cell) {
var $contentRow = $cell.closest('tr'),
$detailsRow = $('
'),
$detailsCell = $('
'),
$detailsWrapper = $(''),
data = $grid.data(),
position = $contentRow.data('position'),
record = $grid.get(position),
id = gj.grid.methods.getId(record, data.primaryKey, record);
$detailsRow.append($detailsCell.append($detailsWrapper.append($contentRow.data('details'))));
$detailsRow.insertAfter($contentRow);
$cell.children('div[data-role="display"]').empty().append(data.icons.collapse);
$grid.updateDetails($contentRow);
gj.grid.plugins.expandCollapseRows.events.detailExpand($grid, $detailsRow.find('td>div'), id);
},
detailCollapse: function ($grid, $cell) {
var $contentRow = $cell.closest('tr'),
$detailsRow = $contentRow.next('tr[data-role="details"]'),
data = $grid.data(),
id = gj.grid.methods.getId($contentRow, data.primaryKey, $contentRow.data('position'));
$detailsRow.remove();
$cell.children('div[data-role="display"]').empty().append(data.icons.expand);
gj.grid.plugins.expandCollapseRows.events.detailCollapse($grid, $detailsRow.find('td>div'), id);
},
keepSelection: function($grid, id) {
var data = $grid.data();
if (data.keepExpandedRows) {
if ($.isArray(data.expandedRows)) {
if (data.expandedRows.indexOf(id) == -1) {
data.expandedRows.push(id)
}
} else {
data.expandedRows = [id];
}
}
},
removeSelection: function ($grid, id) {
var data = $grid.data();
if (data.keepExpandedRows && $.isArray(data.expandedRows) && data.expandedRows.indexOf(id) > -1) {
data.expandedRows.splice(data.expandedRows.indexOf(id), 1);
}
},
updateDetailsColSpan: function ($grid) {
var $cells = $grid.find('tbody > tr[data-role="details"] > td');
if ($cells && $cells.length) {
$cells.attr('colspan', gj.grid.methods.countVisibleColumns($grid));
}
}
},
'public': {
//TODO: add documentation
collapseAll: function () {
var $grid = this;
$grid.find('tbody tr[data-role="row"]').each(function () {
gj.grid.plugins.expandCollapseRows.private.detailCollapse($grid, $(this).find('td').first());
});
},
//TODO: add documentation
expandAll: function () {
var $grid = this;
$grid.find('tbody tr[data-role="row"]').each(function () {
gj.grid.plugins.expandCollapseRows.private.detailExpand($grid, $(this).find('td').first());
});
},
//TODO: add documentation
updateDetails: function ($contentRow) {
var $grid = this,
$detailWrapper = $contentRow.data('details'),
content = $detailWrapper.html(),
record = $grid.get($contentRow.data('position'));
$detailWrapper.html().replace(/\{(.+?)\}/g, function ($0, $1) {
var column = gj.grid.methods.getColumnInfo($grid, $1);
content = content.replace($0, gj.grid.methods.formatText(record[$1], column));
});
$detailWrapper.html(content);
}
},
'events': {
/**
* Event fires when detail row is showing
*
* @event detailExpand
* @param {object} e - event data
* @param {object} detailWrapper - the detail wrapper as jQuery object
* @param {string} id - the id of the record
* @example sample
*
*
*/
detailExpand: function ($grid, $detailWrapper, id) {
$grid.triggerHandler('detailExpand', [$detailWrapper, id]);
},
/**
* Event fires when detail row is hiding
*
* @event detailCollapse
* @param {object} e - event data
* @param {object} detailWrapper - the detail wrapper as jQuery object
* @param {string} id - the id of the record
* @example sample
*
*
*/
detailCollapse: function ($grid, $detailWrapper, id) {
$grid.triggerHandler('detailCollapse', [$detailWrapper, id]);
}
},
'configure': function ($grid) {
var column, data = $grid.data();
$.extend(true, $grid, gj.grid.plugins.expandCollapseRows.public);
if (typeof (data.detailTemplate) !== 'undefined') {
column = {
title: '',
field: data.primaryKey,
width: data.defaultIconColumnWidth,
align: 'center',
stopPropagation: true,
cssClass: 'gj-cursor-pointer gj-unselectable',
tmpl: data.icons.expand,
events: {
'click': function (e) {
var $cell = $(this), methods = gj.grid.plugins.expandCollapseRows.private;
if ($cell.closest('tr').next().attr('data-role') === 'details') {
methods.detailCollapse($grid, $cell);
methods.removeSelection($grid, e.data.id);
} else {
methods.detailExpand($grid, $(this));
methods.keepSelection($grid, e.data.id);
}
}
}
};
data.columns = [column].concat(data.columns);
$grid.on('rowDataBound', function (e, $row, id, record) {
$row.data('details', $(data.detailTemplate));
});
$grid.on('columnShow', function (e, column) {
gj.grid.plugins.expandCollapseRows.private.updateDetailsColSpan($grid);
});
$grid.on('columnHide', function (e, column) {
gj.grid.plugins.expandCollapseRows.private.updateDetailsColSpan($grid);
});
$grid.on('rowRemoving', function (e, $row, id, record) {
gj.grid.plugins.expandCollapseRows.private.detailCollapse($grid, $row.children('td').first());
});
$grid.on('dataBinding', function () {
$grid.collapseAll();
});
$grid.on('pageChanging', function () {
$grid.collapseAll();
});
$grid.on('dataBound', function () {
var i, $cell, $row, data = $grid.data();
if (data.keepExpandedRows && $.isArray(data.expandedRows)) {
for (i = 0; i < data.expandedRows.length; i++) {
$row = gj.grid.methods.getRowById($grid, data.expandedRows[i]);
if ($row && $row.length) {
$cell = $row.children('td').first(); //TODO: use data-role for selection
if ($cell && $cell.length) {
gj.grid.plugins.expandCollapseRows.private.detailExpand($grid, $cell);
}
}
}
}
});
}
}
};
/**
* @widget Grid
* @plugin Inline Editing
*/
gj.grid.plugins.inlineEditing = {
renderers: {
editManager: function (value, record, $cell, $displayEl, id, $grid) {
var data = $grid.data(),
$edit = $(data.inlineEditing.editButton).attr('data-key', id),
$delete = $(data.inlineEditing.deleteButton).attr('data-key', id),
$update = $(data.inlineEditing.updateButton).attr('data-key', id).hide(),
$cancel = $(data.inlineEditing.cancelButton).attr('data-key', id).hide();
$edit.on('click', function (e) {
$grid.edit($(this).data('key'));
$edit.hide();
$delete.hide();
$update.show();
$cancel.show();
});
$delete.on('click', function (e) {
$grid.removeRow($(this).data('key'));
});
$update.on('click', function (e) {
$grid.update($(this).data('key'));
$edit.show();
$delete.show();
$update.hide();
$cancel.hide();
});
$cancel.on('click', function (e) {
$grid.cancel($(this).data('key'));
$edit.show();
$delete.show();
$update.hide();
$cancel.hide();
});
$displayEl.empty().append($edit).append($delete).append($update).append($cancel);
}
}
};
gj.grid.plugins.inlineEditing.config = {
base: {
defaultColumnSettings: {
/** Provides a way to specify a custom editing UI for the column.
* @alias column.editor
* @type function|boolean
* @default undefined
* @example sample
*
*
*/
mode: 'click',
/** If set to true, add column with buttons for edit, delete, update and cancel at the end of the grid.
* @alias inlineEditing.managementColumn
* @type Boolean
* @default true
* @example True
*
*
*/
getChanges: function () {
return JSON.parse(sessionStorage.getItem('gj.grid.' + this.data().guid));
},
/**
* Enable edit mode for all editable cells within a row.
* @method
* @param {string} id - The id of the row that needs to be edited
* @return grid
* @example Edit.Row
*
*
*
*/
edit: function (id) {
var i, record = this.getById(id),
$cells = gj.grid.methods.getRowById(this, id).find('td'),
columns = this.data('columns');
for (i = 0; i < $cells.length; i++) {
gj.grid.plugins.inlineEditing.private.editMode(this, $($cells[i]), columns[i], record);
}
return this;
},
/**
* Update all editable cells within a row, when the row is in edit mode.
* @method
* @param {string} id - The id of the row that needs to be updated
* @return grid
* @fires rowDataChanged
* @example Update.Row
*
*
*
*/
update: function (id) {
var i, record = this.getById(id),
$cells = gj.grid.methods.getRowById(this, id).find('td'),
columns = this.data('columns');
for (i = 0; i < $cells.length; i++) {
gj.grid.plugins.inlineEditing.private.displayMode(this, $($cells[i]), columns[i], false);
}
gj.grid.plugins.inlineEditing.events.rowDataChanged(this, id, record);
return this;
},
/**
* Cancel the edition of all editable cells, when the row is in edit mode.
* @method
* @param {string} id - The id of the row where you need to undo all changes
* @return grid
* @example Cancel.Row
*
*
*
*/
cancel: function (id) {
var i, record = this.getById(id),
$cells = gj.grid.methods.getRowById(this, id).find('td'),
columns = this.data('columns');
for (i = 0; i < $cells.length; i++) {
gj.grid.plugins.inlineEditing.private.displayMode(this, $($cells[i]), columns[i], true);
}
return this;
}
};
gj.grid.plugins.inlineEditing.events = {
/**
* Event fires after inline edit of a cell in the grid.
*
* @event cellDataChanged
* @param {object} e - event data
* @param {object} $cell - the cell presented as jquery object
* @param {object} column - the column configuration data
* @param {object} record - the data of the row record
* @param {object} oldValue - the old cell value
* @param {object} newValue - the new cell value
* @example sample
*
*
*/
cellDataChanged: function ($grid, $cell, column, record, oldValue, newValue) {
$grid.triggerHandler('cellDataChanged', [$cell, column, record, oldValue, newValue]);
},
/**
* Event fires after inline edit of a row in the grid.
*
* @event rowDataChanged
* @param {object} e - event data
* @param {object} id - the id of the record
* @param {object} record - the data of the row record
* @example sample
*
*
*/
rowDataChanged: function ($grid, id, record) {
$grid.triggerHandler('rowDataChanged', [id, record]);
}
};
gj.grid.plugins.inlineEditing.configure = function ($grid, fullConfig, clientConfig) {
var data = $grid.data();
$.extend(true, $grid, gj.grid.plugins.inlineEditing.public);
if (clientConfig.inlineEditing) {
$grid.on('dataBound', function () {
$grid.find('span.gj-dirty').remove();
});
}
if (data.inlineEditing.mode === 'command') {
gj.grid.plugins.inlineEditing.private.localization(data);
if (fullConfig.inlineEditing.managementColumn) {
data.columns.push(fullConfig.inlineEditing.managementColumnConfig);
}
} else {
$grid.on('cellDataBound', function (e, $displayEl, id, column, record) {
if (column.editor) {
$displayEl.parent('td').on(data.inlineEditing.mode === 'dblclick' ? 'dblclick' : 'click', function () {
gj.grid.plugins.inlineEditing.private.editMode($grid, $displayEl.parent(), column, record);
});
}
});
}
};
/**
* @widget Grid
* @plugin Optimistic Persistence
*/
gj.grid.plugins.optimisticPersistence = {
config: {
base: {
optimisticPersistence: {
/** Array that contains a list with param names that needs to be saved in the localStorage. You need to specify guid on the initialization of the grid in order to enable this feature.
* @additionalinfo This feature is using HTML5 localStorage to store params and values.
* You can clear the data saved in localStorage when you clear your browser cache.
* @alias optimisticPersistence.localStorage
* @type array
* @default undefined
* @example sample
*
Change the page and/or page size and then refresh the grid.
*
*
*/
localStorage: undefined,
/** Array that contains a list with param names that needs to be saved in the sessionStorage. You need to specify guid on the initialization of the grid in order to enable this feature.
* @additionalinfo This feature is using HTML5 sessionStorage to store params and values.
* You can clear the data saved in sessionStorage when you open and close the browser.
* @alias optimisticPersistence.sessionStorage
* @type array
* @default undefined
* @example sample
*
Change the page and/or page size and then refresh the grid.
*
*
*/
sessionStorage: undefined
}
}
},
private: {
applyParams: function ($grid) {
var data = $grid.data(),
params = {}, storage;
storage = JSON.parse(sessionStorage.getItem('gj.grid.' + data.guid));
if (storage && storage.optimisticPersistence) {
$.extend(params, storage.optimisticPersistence);
}
storage = JSON.parse(localStorage.getItem('gj.grid.' + data.guid));
if (storage && storage.optimisticPersistence) {
$.extend(params, storage.optimisticPersistence);
}
$.extend(data.params, params);
},
saveParams: function ($grid) {
var i, param,
data = $grid.data(),
storage = { optimisticPersistence: {} };
if (data.optimisticPersistence.sessionStorage) {
for (i = 0; i < data.optimisticPersistence.sessionStorage.length; i++) {
param = data.optimisticPersistence.sessionStorage[i];
storage.optimisticPersistence[param] = data.params[param];
}
storage = $.extend(true, JSON.parse(sessionStorage.getItem('gj.grid.' + data.guid)), storage);
sessionStorage.setItem('gj.grid.' + data.guid, JSON.stringify(storage));
}
if (data.optimisticPersistence.localStorage) {
storage = { optimisticPersistence: {} };
for (i = 0; i < data.optimisticPersistence.localStorage.length; i++) {
param = data.optimisticPersistence.localStorage[i];
storage.optimisticPersistence[param] = data.params[param];
}
storage = $.extend(true, JSON.parse(localStorage.getItem('gj.grid.' + data.guid)), storage);
localStorage.setItem('gj.grid.' + data.guid, JSON.stringify(storage));
}
}
},
configure: function ($grid, fullConfig, clientConfig) {
if (fullConfig.guid) {
if (fullConfig.optimisticPersistence.localStorage || fullConfig.optimisticPersistence.sessionStorage) {
gj.grid.plugins.optimisticPersistence.private.applyParams($grid);
$grid.on('dataBound', function (e) {
gj.grid.plugins.optimisticPersistence.private.saveParams($grid);
});
}
}
}
};
/**
* @widget Grid
* @plugin Pagination
*/
gj.grid.plugins.pagination = {
config: {
base: {
style: {
pager: {
cell: '',
stateDisabled: '',
activeButton: 'gj-grid-base-active'
}
},
paramNames: {
/** The name of the parameter that is going to send the number of the page.
* The pager should be enabled in order this parameter to be in use.
* @alias paramNames.page
* @type string
* @default "page"
*/
page: 'page',
/** The name of the parameter that is going to send the maximum number of records per page.
* The pager should be enabled in order this parameter to be in use.
* @alias paramNames.limit
* @type string
* @default "limit"
*/
limit: 'limit'
},
pager: {
/** The maximum number of records that can be show by page.
* @alias pager.limit
* @type number
* @default 10
* @example local.data
*
*
* @example remote.data
*
*
*/
limit: 10,
/** Array that contains the possible page sizes of the grid.
* When this setting is set, then a drop down with the options for each page size is visualized in the pager.
* @alias pager.sizes
* @type array
* @default undefined
* @example Bootstrap.3
*
*
*
*
* @example Material.Design
*
*
*
*
*/
sizes: undefined,
/** Array that contains a list with jquery objects that are going to be used on the left side of the pager.
* @alias pager.leftControls
* @type array
* @default array
* @example Font.Awesome
*
*
*
*/
leftControls: undefined,
/** Array that contains a list with jquery objects that are going to be used on the right side of the pager.
* @alias pager.rightControls
* @type array
* @default array
*/
rightControls: undefined
}
},
jqueryui: {
style: {
pager: {
cell: 'ui-widget-header ui-state-default ui-grid-pager-cell',
stateDisabled: 'ui-state-disabled'
}
},
pager: {
leftControls: [
$(''),
$(''),
$('
').css({ "margin-right": "5px" })
];
}
},
initPagerControl: function ($control, $grid) {
var data = $grid.data();
switch ($control.data('role')) {
case 'page-size':
if (data.pager.sizes && 0 < data.pager.sizes.length) {
$control.show();
$.each(data.pager.sizes, function () {
$control.append($('').attr('value', this.toString()).text(this.toString()));
});
$control.change(function () {
var newSize = parseInt(this.value, 10);
data.params[data.paramNames.limit] = newSize;
gj.grid.plugins.pagination.private.changePage($grid, 1);
gj.grid.plugins.pagination.events.pageSizeChange($grid, newSize);
});
$control.val(data.params[data.paramNames.limit]);
} else {
$control.hide();
}
break;
case 'page-refresh':
$control.on('click', function () { $grid.reload(); });
break;
}
},
reloadPager: function ($grid, totalRecords) {
var page, limit, lastPage, firstRecord, lastRecord, data, controls, i;
data = $grid.data();
if (data.pager) {
page = (0 === totalRecords) ? 0 : parseInt(data.params[data.paramNames.page], 10);
limit = parseInt(data.params[data.paramNames.limit], 10);
lastPage = Math.ceil(totalRecords / limit);
firstRecord = (0 === page) ? 0 : (limit * (page - 1)) + 1;
lastRecord = (firstRecord + limit) > totalRecords ? totalRecords : (firstRecord + limit) - 1;
controls = $grid.find('TFOOT [data-role]');
for (i = 0; i < controls.length; i++) {
gj.grid.plugins.pagination.private.reloadPagerControl($(controls[i]), $grid, page, lastPage, firstRecord, lastRecord, totalRecords);
}
gj.grid.plugins.pagination.private.updatePagerColSpan($grid);
}
},
reloadPagerControl: function ($control, $grid, page, lastPage, firstRecord, lastRecord, totalRecords) {
var newPage;
switch ($control.data('role')) {
case 'page-first':
gj.grid.plugins.pagination.private.assignPageHandler($grid, $control, 1, page < 2);
break;
case 'page-previous':
gj.grid.plugins.pagination.private.assignPageHandler($grid, $control, page - 1, page < 2);
break;
case 'page-number':
$control.val(page).off('change').on('change', gj.grid.plugins.pagination.private.createChangePageHandler($grid, page, lastPage));
break;
case 'page-label-last':
$control.text(lastPage);
break;
case 'page-next':
gj.grid.plugins.pagination.private.assignPageHandler($grid, $control, page + 1, lastPage === page);
break;
case 'page-last':
gj.grid.plugins.pagination.private.assignPageHandler($grid, $control, lastPage, lastPage === page);
break;
case 'page-button-one':
newPage = (page === 1) ? 1 : ((page == lastPage) ? (page - 2) : (page - 1));
gj.grid.plugins.pagination.private.assignButtonHandler($grid, $control, page, newPage, lastPage);
break;
case 'page-button-two':
newPage = (page === 1) ? 2 : ((page == lastPage) ? lastPage - 1 : page);
gj.grid.plugins.pagination.private.assignButtonHandler($grid, $control, page, newPage, lastPage);
break;
case 'page-button-three':
newPage = (page === 1) ? page + 2 : ((page == lastPage) ? page : (page + 1));
gj.grid.plugins.pagination.private.assignButtonHandler($grid, $control, page, newPage, lastPage);
break;
case 'record-first':
$control.text(firstRecord);
break;
case 'record-last':
$control.text(lastRecord);
break;
case 'record-total':
$control.text(totalRecords);
break;
}
},
assignPageHandler: function ($grid, $control, newPage, disabled) {
var style = $grid.data().style.pager;
if (disabled) {
$control.addClass(style.stateDisabled).prop('disabled', true).off('click');
} else {
$control.removeClass(style.stateDisabled).prop('disabled', false).off('click').on('click', function () {
gj.grid.plugins.pagination.private.changePage($grid, newPage);
});
}
},
assignButtonHandler: function ($grid, $control, page, newPage, lastPage) {
var style = $grid.data().style.pager;
if (newPage < 1 || newPage > lastPage) {
$control.hide();
} else {
$control.show().off('click').text(newPage);
if (newPage === page) {
$control.addClass(style.activeButton);
} else {
$control.removeClass(style.activeButton).on('click', function () {
gj.grid.plugins.pagination.private.changePage($grid, newPage);
});
}
}
},
createChangePageHandler: function ($grid, currentPage, lastPage) {
return function () {
var data = $grid.data(),
newPage = parseInt(this.value, 10);
if (newPage && !isNaN(newPage) && newPage <= lastPage) {
gj.grid.plugins.pagination.private.changePage($grid, newPage);
} else {
this.value = currentPage;
alert('Please enter a valid number.');
}
};
},
changePage: function ($grid, newPage) {
var data = $grid.data();
$grid.find('TFOOT [data-role="page-number"]').val(newPage);
data.params[data.paramNames.page] = newPage;
gj.grid.plugins.pagination.events.pageChanging($grid, newPage);
$grid.reload();
},
updatePagerColSpan: function ($grid) {
var $cell = $grid.find('tfoot > tr[data-role="pager"] > th');
if ($cell && $cell.length) {
$cell.attr('colspan', gj.grid.methods.countVisibleColumns($grid));
}
},
isLastRecordVisible: function ($grid) {
var result = true,
data = $grid.data(),
limit = parseInt(data.params[data.paramNames.limit], 10),
page = parseInt(data.params[data.paramNames.page], 10),
count = $grid.count();
if (limit && page) {
result = ((page - 1) * limit) + count === data.totalRecords;
}
return result;
}
},
public: {
getAll: function (includeAllRecords) {
var limit, page, start, data = this.data();
if (!includeAllRecords && $.isArray(data.dataSource) && data.params[data.paramNames.limit] && data.params[data.paramNames.page]) {
limit = parseInt(data.params[data.paramNames.limit], 10);
page = parseInt(data.params[data.paramNames.page], 10);
start = (page - 1) * limit;
return data.records.slice(start, start + limit);
} else {
return data.records;
}
}
},
events: {
/**
* Triggered when the page size is changed.
*
* @event pageSizeChange
* @param {object} e - event data
* @param {number} newSize - The new page size
* @example sample
*
*
*/
pageSizeChange: function ($grid, newSize) {
$grid.triggerHandler('pageSizeChange', [newSize]);
},
/**
* Triggered before the change of the page.
*
* @event pageChanging
* @param {object} e - event data
* @param {number} newPage - The new page
* @example sample
*
*
*/
pageChanging: function ($grid, newSize) {
$grid.triggerHandler('pageChanging', [newSize]);
}
},
configure: function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.pagination.public);
var data = $grid.data();
if (clientConfig.pager) {
gj.grid.methods.isLastRecordVisible = gj.grid.plugins.pagination.private.isLastRecordVisible;
$grid.on('initialized', function () {
gj.grid.plugins.pagination.private.init($grid);
});
$grid.on('dataBound', function (e, records, totalRecords) {
gj.grid.plugins.pagination.private.reloadPager($grid, totalRecords);
});
$grid.on('columnShow', function () {
gj.grid.plugins.pagination.private.updatePagerColSpan($grid);
});
$grid.on('columnHide', function () {
gj.grid.plugins.pagination.private.updatePagerColSpan($grid);
});
}
}
};
/**
* @widget Grid
* @plugin Responsive Design
*/
gj.grid.plugins.responsiveDesign = {
config: {
base: {
/** The interval in milliseconds for checking if the grid is resizing.
* This setting is in use only if the resizeMonitoring setting is set to true.
* @type number
* @default 500
* @example sample
*
*
*/
resizeCheckInterval: 500,
/** This setting enables responsive behaviour of the grid where some column are invisible when there is not enough space on the screen for them.
* The visibility of the columns in this mode is driven by the column minWidth and priority settings.
* The columns without priority setting are always visible and can't hide in small screen resolutions.
* @type boolean
* @default false
* @example sample
*
*
*/
responsive: false,
/** Automatically adds hidden columns to the details section of the row.
* This setting works only if the responsive setting is set to true and the detailTemplate is set.
* You need to set priority and minWidth on the colums, that needs to be hidden in smaller screens.
* @type boolean
* @default false
* @example sample
*
*
*/
showHiddenColumnsAsDetails: false,
defaultColumn: {
/** The priority of the column compared to other columns in the grid.
* The columns are hiding based on the priorities.
* This setting is working only when the responsive setting is set to true.
* @alias column.priority
* @type number
* @default undefined
* @example sample
*
*
*/
priority: undefined,
/** The minimum width of the column.
* The column is getting invisible when there is not enough space in the grid for this minimum width.
* This setting is working only when the responsive setting is set to true and the column priority setting is set.
* @alias column.minWidth
* @type number
* @default 150
* @example sample
*
*
*/
minWidth: 150
},
style: {
rowDetailItem: ''
}
},
bootstrap: {
style: {
rowDetailItem: 'col-lg-4'
}
},
jqueryui: {
style: {
rowDetailItem: ''
}
}
},
'private': {
orderColumns: function (config) {
var result = [];
if (config.columns && config.columns.length) {
for (i = 0; i < config.columns.length; i++) {
result.push({
position: i,
field: config.columns[i].field,
minWidth: config.columns[i].width || config.columns[i].minWidth || config.defaultColumn.minWidth,
priority: config.columns[i].priority || 0
});
}
result.sort(function (a, b) {
var result = 0;
if (a.priority < b.priority) {
result = -1;
} else if (a.priority > b.priority) {
result = 1;
}
return result;
});
}
return result;
},
updateDetails: function ($grid) {
var rows, data, i, j, $row, details, $placeholder, column, tmp;
rows = $grid.find('tbody > tr[data-role="row"]');
data = $grid.data();
for (i = 0; i < rows.length; i++) {
$row = $(rows[i]);
details = $row.data('details');
for (j = 0; j < data.columns.length; j++) {
column = data.columns[j];
$placeholder = details && details.find('div[data-id="' + column.field + '"]');
if (data.columns[j].hidden) {
tmp = '' + (column.title || column.field) + ': {' + column.field + '}';
if (!$placeholder || !$placeholder.length) {
$placeholder = $('').html(tmp);
$placeholder.addClass(data.style.rowDetailItem);
if (!details || !details.length) {
details = $('');
}
details.append($placeholder);
} else {
$placeholder.empty().html(tmp);
}
} else if ($placeholder && $placeholder.length) {
$placeholder.remove();
}
}
$grid.updateDetails($row);
}
}
},
'public': {
oldWidth: undefined,
resizeCheckIntervalId: undefined,
/**
* Make the grid responsive based on the available space.
* Show column if the space for the grid is expanding and hide columns when the space for the grid is decreasing.
* @method
* @return void
* @example sample
*
*
*
*
*/
makeResponsive: function () {
var i, $column,
extraWidth = 0,
config = this.data(),
columns = gj.grid.plugins.responsiveDesign.private.orderColumns(config);
//calculate extra width
for (i = 0; i < columns.length; i++) {
$column = this.find('thead>tr>th:eq(' + columns[i].position + ')');
if ($column.is(':visible') && columns[i].minWidth < $column.width()) {
extraWidth += $column.width() - columns[i].minWidth;
}
}
//show columns
if (extraWidth) {
for (i = 0; i < columns.length; i++) {
$column = this.find('thead>tr>th:eq(' + columns[i].position + ')');
if (!$column.is(':visible') && columns[i].minWidth <= extraWidth) {
this.showColumn(columns[i].field);
extraWidth -= $column.width();
}
}
}
//hide columns
for (i = (columns.length - 1); i >= 0; i--) {
$column = this.find('thead>tr>th:eq(' + columns[i].position + ')');
if ($column.is(':visible') && columns[i].priority && columns[i].minWidth > $column.outerWidth()) {
this.hideColumn(columns[i].field);
}
}
},
},
'events': {
/**
* Event fires when the grid width is changed. The "responsive" configuration setting should be set to true in order this event to fire.
*
* @event resize
* @param {object} e - event data
* @param {number} newWidth - The new width
* @param {number} oldWidth - The old width
* @example sample
*
*
*/
resize: function ($grid, newWidth, oldWidth) {
$grid.triggerHandler('resize', [newWidth, oldWidth]);
}
},
'configure': function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.responsiveDesign.public);
if (fullConfig.responsive) {
$grid.on('initialized', function () {
$grid.makeResponsive();
$grid.oldWidth = $grid.width();
$grid.resizeCheckIntervalId = setInterval(function () {
var newWidth = $grid.width();
if (newWidth !== $grid.oldWidth) {
gj.grid.plugins.responsiveDesign.events.resize($grid, newWidth, $grid.oldWidth);
}
$grid.oldWidth = newWidth;
}, fullConfig.resizeCheckInterval);
});
$grid.on('destroy', function () {
if ($grid.resizeCheckIntervalId) {
clearInterval($grid.resizeCheckIntervalId);
}
});
$grid.on('resize', function () {
$grid.makeResponsive();
});
}
if (fullConfig.showHiddenColumnsAsDetails && gj.grid.plugins.expandCollapseRows) {
$grid.on('dataBound', function () {
gj.grid.plugins.responsiveDesign.private.updateDetails($grid);
});
$grid.on('columnHide', function () {
gj.grid.plugins.responsiveDesign.private.updateDetails($grid);
});
$grid.on('columnShow', function () {
gj.grid.plugins.responsiveDesign.private.updateDetails($grid);
});
$grid.on('rowDataBound', function () {
gj.grid.plugins.responsiveDesign.private.updateDetails($grid);
});
}
}
};
/**
* @widget Grid
* @plugin Toolbar
*/
gj.grid.plugins.toolbar = {
config: {
base: {
/** Template for the content in the toolbar. Appears in a separate row on top of the grid.
* @type string
* @default undefined
* @example sample
*
*
*/
toolbarTemplate: undefined,
/** The title of the grid. Appears in a separate row on top of the grid.
* @type string
* @default undefined
* @example sample
*
*
*/
title: undefined,
style: {
toolbar: 'gj-grid-base-toolbar'
}
},
jqueryui: {
style: {
toolbar: 'ui-widget-header ui-state-default gj-grid-ui-toolbar'
}
},
bootstrap: {
style: {
toolbar: 'gj-grid-bootstrap-toolbar'
}
}
},
private: {
init: function ($grid) {
var data, $toolbar, $title;
data = $grid.data();
$toolbar = $grid.prev('div[data-role="toolbar"]');
if (typeof (data.toolbarTemplate) !== 'undefined' || typeof (data.title) !== 'undefined' || $toolbar.length > 0) {
if ($toolbar.length === 0) {
$toolbar = $('');
$grid.before($toolbar);
}
$toolbar.addClass(data.style.toolbar);
if ($toolbar.children().length === 0 && data.toolbarTemplate) {
$toolbar.append(data.toolbarTemplate);
}
$title = $toolbar.find('[data-role="title"]');
if ($title.length === 0) {
$title = $('');
$toolbar.prepend($title);
}
if (data.title) {
$title.text(data.title);
}
if (data.minWidth) {
$toolbar.css('min-width', data.minWidth);
}
}
}
},
public: {
/**
* Get or set grid title.
* @additionalinfo When you pass value in the text parameter this value with be in use for the new title of the grid and the method will return grid object.
* When you don't pass value in the text parameter, then the method will return the text of the current grid title.
* You can use this method in a combination with toolbarTemplate only if the title is wrapped in element with data-role attribute that equals to "title".
* @method
* @param {object} text - The text of the new grid title.
* @return string or grid object
* @example text
*
*
*
*
*
* @example html.template
*
*
*
*
*
*/
title: function (text) {
var $titleEl = this.parent().find('div[data-role="toolbar"] [data-role="title"]');
if (typeof (text) !== 'undefined') {
$titleEl.text(text);
return this;
} else {
return $titleEl.text();
}
}
},
configure: function ($grid) {
$.extend(true, $grid, gj.grid.plugins.toolbar.public);
$grid.on('initialized', function () {
gj.grid.plugins.toolbar.private.init($grid);
});
}
};
/**
* @widget Grid
* @plugin Resizable Columns
*/
gj.grid.plugins.resizableColumns = {
config: {
base: {
/** If set to true, users can resize columns by dragging the edges (resize handles) of their header cells.
* @type boolean
* @default false
* @example Base.Theme
*
*
* @example Bootstrap
*
*
* @example Material.Design
*
*
*/
resizableColumns: false
}
},
private: {
init: function ($grid, config) {
var $columns, $column, i, $wrapper, $resizer, marginRight;
$columns = $grid.find('thead tr[data-role="caption"] th');
if ($columns.length) {
for (i = 0; i < $columns.length - 1; i++) {
$column = $($columns[i]);
$wrapper = $('');
marginRight = parseInt($column.css('padding-right'), 10) + 3;
$resizer = $('').css('margin-right', '-' + marginRight + 'px');
if ($.fn.draggable) {
$resizer.draggable({
start: function () {
$grid.addClass('gj-unselectable');
$grid.addClass('gj-grid-resize-cursor');
},
stop: function () {
$grid.removeClass('gj-unselectable');
$grid.removeClass('gj-grid-resize-cursor');
this.style.removeProperty('top');
this.style.removeProperty('left');
this.style.removeProperty('position');
},
drag: gj.grid.plugins.resizableColumns.private.createResizeHandle($grid, $column, config.columns[i])
});
}
$column.append($wrapper.append($resizer));
}
}
},
createResizeHandle: function ($grid, $column, column) {
return function (e, offset) {
var newWidth, currentWidth = parseInt($column.attr('width'), 10);
if (!currentWidth) {
currentWidth = $column.outerWidth();
}
if (offset && offset.left) {
newWidth = currentWidth + offset.left;
column.width = newWidth;
$column.attr('width', newWidth);
}
};
}
},
public: {
},
configure: function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.resizableColumns.public);
if (fullConfig.resizableColumns) {
$grid.on('initialized', function () {
gj.grid.plugins.resizableColumns.private.init($grid, fullConfig);
});
}
}
};
/**
* @widget Grid
* @plugin Row Reorder
*/
gj.grid.plugins.rowReorder = {
config: {
base: {
/** If set to true, enable row reordering with drag and drop.
* @type boolean
* @default false
* @example Base.Theme.Sample
*
Drag and Drop rows in order to reorder them.
*
*
* @example Bootstrap.Sample
*
Drag and Drop rows in order to reorder them.
*
*
* @example JQueryUI.Sample
*
Drag and Drop rows in order to reorder them.
*
*
*/
rowReorder: false,
/** If set, enable row reordering only when you try to drag cell from the configured column.
* Accept only field names of columns.
* @type string
* @default undefined
* @example sample
*
*
*/
rowReorderColumn: undefined,
/** If set, update the value in the field for all records. Accept only field names of columns.
* @type string
* @default undefined
* @example Visible.OrderNumber
*
*
* @example Hidden.OrderNumber
*
*
*
*/
orderNumberField: undefined,
style: {
targetRowIndicatorTop: 'gj-grid-row-reorder-indicator-top',
targetRowIndicatorBottom: 'gj-grid-row-reorder-indicator-bottom'
}
}
},
private: {
init: function ($grid) {
var i, columnPosition, $row,
$rows = $grid.find('tbody tr[data-role="row"]');
if ($grid.data('rowReorderColumn')) {
columnPosition = gj.grid.methods.getColumnPosition($grid.data('columns'), $grid.data('rowReorderColumn'));
}
for (i = 0; i < $rows.length; i++) {
$row = $($rows[i]);
if (typeof (columnPosition) !== 'undefined') {
$row.find('td:eq(' + columnPosition + ')').on('mousedown', gj.grid.plugins.rowReorder.private.createRowMouseDownHandler($grid, $row));
} else {
$row.on('mousedown', gj.grid.plugins.rowReorder.private.createRowMouseDownHandler($grid, $row));
}
}
},
createRowMouseDownHandler: function ($grid, $trSource) {
return function (e) {
var $dragEl = $grid.clone();
$('body').append($dragEl);
$dragEl.attr('data-role', 'draggable-clone').css('cursor', 'move');
$dragEl.children('thead').remove().children('tfoot').remove();
$dragEl.find('tbody tr:not([data-position="' + $trSource.data('position') + '"])').remove();
$dragEl.draggable({
stop: gj.grid.plugins.rowReorder.private.createDragStopHandler($grid, $trSource)
});
$dragEl.css({
position: 'absolute', top: $trSource.offset().top, left: $trSource.offset().left, width: $trSource.width()
});
if ($trSource.attr('data-droppable') === 'true') {
$trSource.droppable('destroy');
}
$trSource.siblings('tr[data-role="row"]').each(function () {
var $dropEl = $(this);
if ($dropEl.attr('data-droppable') === 'true') {
$dropEl.droppable('destroy');
}
$dropEl.droppable({
over: gj.grid.plugins.rowReorder.private.createDroppableOverHandler($trSource),
out: gj.grid.plugins.rowReorder.private.droppableOut
});
});
$dragEl.trigger('mousedown');
};
},
createDragStopHandler: function ($grid, $trSource) {
return function (e, mousePosition) {
$('table[data-role="draggable-clone"]').draggable('destroy').remove();
$trSource.siblings('tr[data-role="row"]').each(function () {
var $trTarget = $(this),
targetPosition = $trTarget.data('position'),
sourcePosition = $trSource.data('position'),
data = $grid.data(),
$rows, $row, i, record, id;
if ($trTarget.droppable('isOver', mousePosition)) {
if (targetPosition < sourcePosition) {
$trTarget.before($trSource);
} else {
$trTarget.after($trSource);
}
data.records.splice(targetPosition - 1, 0, data.records.splice(sourcePosition - 1, 1)[0]);
$rows = $trTarget.parent().find('tr[data-role="row"]');
for (i = 0; i < $rows.length; i++) {
$($rows[i]).attr('data-position', i + 1);
}
if (data.orderNumberField) {
for (i = 0; i < data.records.length; i++) {
data.records[i][data.orderNumberField] = i + 1;
}
for (i = 0; i < $rows.length; i++) {
$row = $($rows[i]);
id = gj.grid.methods.getId($row, data.primaryKey, $row.attr('data-position'));
record = gj.grid.methods.getByPosition($grid, $row.attr('data-position'));
$grid.setCellContent(id, data.orderNumberField, record[data.orderNumberField]);
}
}
}
$trTarget.removeClass('gj-grid-base-top-border');
$trTarget.removeClass('gj-grid-base-bottom-border');
$trTarget.droppable('destroy');
});
}
},
createDroppableOverHandler: function ($trSource) {
return function (e) {
var $trTarget = $(this),
targetPosition = $trTarget.data('position'),
sourcePosition = $trSource.data('position');
if (targetPosition < sourcePosition) {
$trTarget.addClass('gj-grid-base-top-border');
} else {
$trTarget.addClass('gj-grid-base-bottom-border');
}
};
},
droppableOut: function () {
$(this).removeClass('gj-grid-base-top-border');
$(this).removeClass('gj-grid-base-bottom-border');
}
},
public: {
},
configure: function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.rowReorder.public);
if (fullConfig.rowReorder && $.fn.draggable && $.fn.droppable) {
$grid.on('dataBound', function () {
gj.grid.plugins.rowReorder.private.init($grid);
});
}
}
};
/**
* @widget Grid
* @plugin Column Reorder
*/
gj.grid.plugins.columnReorder = {
config: {
base: {
/** If set to true, enable column reordering with drag and drop.
* @type boolean
* @default false
* @example sample
*
Drag and Drop column headers in order to reorder the columns.
*
*
*/
columnReorder: false,
style: {
targetRowIndicatorTop: 'gj-grid-row-reorder-indicator-top',
targetRowIndicatorBottom: 'gj-grid-row-reorder-indicator-bottom'
}
}
},
private: {
init: function ($grid) {
var i, $cell,
$cells = $grid.find('thead tr th');
for (i = 0; i < $cells.length; i++) {
$cell = $($cells[i]);
$cell.on('mousedown', gj.grid.plugins.columnReorder.private.createMouseDownHandler($grid, $cell));
}
},
createMouseDownHandler: function ($grid, $thSource) {
return function (e) {
var $dragEl = $grid.clone(),
srcIndex = $thSource.index();
$('body').append($dragEl);
$dragEl.attr('data-role', 'draggable-clone').css('cursor', 'move');
$dragEl.find('thead tr th:eq(' + srcIndex + ')').siblings().remove();
$dragEl.find('tbody tr[data-role != "row"]').remove();
$dragEl.find('tbody tr td:nth-child(' + (srcIndex + 1) + ')').siblings().remove();
$dragEl.find('tfoot').remove();
$dragEl.draggable({
stop: gj.grid.plugins.columnReorder.private.createDragStopHandler($grid, $thSource)
});
$dragEl.css({
position: 'absolute', top: $thSource.offset().top, left: $thSource.offset().left, width: $thSource.width()
});
if ($thSource.attr('data-droppable') === 'true') {
$thSource.droppable('destroy');
}
$thSource.siblings('th').each(function () {
var $dropEl = $(this);
if ($dropEl.attr('data-droppable') === 'true') {
$dropEl.droppable('destroy');
}
$dropEl.droppable({
over: gj.grid.plugins.columnReorder.private.createDroppableOverHandler($grid, $thSource),
out: gj.grid.plugins.columnReorder.private.droppableOut
});
});
$dragEl.trigger('mousedown');
};
},
createDragStopHandler: function ($grid, $thSource) {
return function (e, mousePosition) {
$('table[data-role="draggable-clone"]').draggable('destroy').remove();
$thSource.siblings('th').each(function () {
var $thTarget = $(this),
data = $grid.data(),
targetPosition = gj.grid.methods.getColumnPosition(data.columns, $thTarget.data('field')),
sourcePosition = gj.grid.methods.getColumnPosition(data.columns, $thSource.data('field'));
$thTarget.removeClass('gj-grid-base-left-border').removeClass('gj-grid-base-right-border');
$thTarget.closest('table').find('tbody tr[data-role="row"] td:nth-child(' + ($thTarget.index() + 1) + ')').removeClass('gj-grid-base-left-border').removeClass('gj-grid-base-right-border');
if ($thTarget.droppable('isOver', mousePosition)) {
if (targetPosition < sourcePosition) {
$thTarget.before($thSource);
} else {
$thTarget.after($thSource);
}
gj.grid.plugins.columnReorder.private.moveRowCells($grid, sourcePosition, targetPosition);
data.columns.splice(targetPosition, 0, data.columns.splice(sourcePosition, 1)[0]);
}
$thTarget.droppable('destroy');
});
}
},
moveRowCells: function ($grid, sourcePosition, targetPosition) {
var i, $row, $rows = $grid.find('tbody tr[data-role="row"]');
for (i = 0; i < $rows.length; i++) {
$row = $($rows[i]);
if (targetPosition < sourcePosition) {
$row.find('td:eq(' + targetPosition + ')').before($row.find('td:eq(' + sourcePosition + ')'));
} else {
$row.find('td:eq(' + targetPosition + ')').after($row.find('td:eq(' + sourcePosition + ')'));
}
}
},
createDroppableOverHandler: function ($grid, $thSource) {
return function (e) {
var $thTarget = $(this),
data = $grid.data(),
targetPosition = gj.grid.methods.getColumnPosition(data.columns, $thTarget.data('field')),
sourcePosition = gj.grid.methods.getColumnPosition(data.columns, $thSource.data('field'));
if (targetPosition < sourcePosition) {
$thTarget.addClass('gj-grid-base-left-border');
$grid.find('tbody tr[data-role="row"] td:nth-child(' + ($thTarget.index() + 1) + ')').addClass('gj-grid-base-left-border');
} else {
$thTarget.addClass('gj-grid-base-right-border');
$grid.find('tbody tr[data-role="row"] td:nth-child(' + ($thTarget.index() + 1) + ')').addClass('gj-grid-base-right-border');
}
};
},
droppableOut: function () {
var $thTarget = $(this);
$thTarget.removeClass('gj-grid-base-left-border').removeClass('gj-grid-base-right-border');
$thTarget.closest('table').find('tbody tr[data-role="row"] td:nth-child(' + ($thTarget.index() + 1) + ')').removeClass('gj-grid-base-left-border').removeClass('gj-grid-base-right-border');
}
},
public: {
},
configure: function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.columnReorder.public);
if (fullConfig.columnReorder) {
$grid.on('initialized', function () {
gj.grid.plugins.columnReorder.private.init($grid);
});
}
}
};
/**
* @widget Grid
* @plugin Header Filter
*/
gj.grid.plugins.headerFilter = {
config: {
base: {
defaultColumnSettings: {
/** Indicates if the column is sortable. If set to false the header filter is hidden.
* @alias column.filterable
* @type boolean
* @default true
* @example sample
*
*
*/
filterable: true
},
/** If set to true, add filters for each column
* @type boolean
* @default object
* @example Remote.DataSource
*
*
* @example Local.DataSource
*
*
*/
headerFilter: {
/** Type of the header filter
* @alias headerFilter.type
* @type (onenterkeypress|onchange)
* @default 'onenterkeypress'
* @example OnEnterKeyPress
*
*
* @example OnChange
*
*
*/
type: 'onenterkeypress'
}
}
},
private: {
init: function ($grid) {
var i, $th, $ctrl, data = $grid.data(),
$filterTr = $('
');
for (i = 0; i < data.columns.length; i++) {
$th = $('
');
if (data.columns[i].filterable) {
$ctrl = $('');
if ('onchange' === data.headerFilter.type) {
$ctrl.on('input propertychange', function (e) {
gj.grid.plugins.headerFilter.private.reload($grid, $(this));
});
} else {
$ctrl.on('keypress', function (e) {
if (e.which == 13) {
gj.grid.plugins.headerFilter.private.reload($grid, $(this));
}
});
$ctrl.on('blur', function (e) {
gj.grid.plugins.headerFilter.private.reload($grid, $(this));
});
}
$th.append($ctrl);
}
if (data.columns[i].hidden) {
$th.hide();
}
$filterTr.append($th);
}
$grid.children('thead').append($filterTr);
},
reload: function ($grid, $ctrl) {
var params = {};
params[$ctrl.data('field')] = $ctrl.val();
$grid.reload(params);
}
},
public: {
},
events: {
},
configure: function ($grid, fullConfig, clientConfig) {
$.extend(true, $grid, gj.grid.plugins.headerFilter.public);
var data = $grid.data();
if (clientConfig.headerFilter) {
$grid.on('initialized', function () {
gj.grid.plugins.headerFilter.private.init($grid);
});
}
}
};
/**
* @widget Grid
* @plugin Grouping
*/
gj.grid.plugins.grouping = {
config: {
base: {
paramNames: {
/** The name of the parameter that is going to send the name of the column for grouping.
* The grouping should be enabled in order this parameter to be in use.
* @alias paramNames.groupBy
* @type string
* @default "groupBy"
*/
groupBy: 'groupBy',
/** The name of the parameter that is going to send the direction for grouping.
* The grouping should be enabled in order this parameter to be in use.
* @alias paramNames.groupByDirection
* @type string
* @default "groupByDirection"
*/
groupByDirection: 'groupByDirection'
},
grouping: {
/** The name of the field that needs to be in use for grouping.
* @type string
* @alias grouping.groupBy
* @default undefined
* @example Local.Data
*
*
* @example Remote.Data
*
*
* @example Material.Design
*
*
*/
groupBy: undefined,
direction: 'asc'
}
}
},
private: {
init: function ($grid) {
var previousValue, data = $grid.data();
previousValue = undefined;
$grid.on('rowDataBound', function (e, $row, id, record) {
if (previousValue !== record[data.grouping.groupBy]) {
var colspan = gj.grid.methods.countVisibleColumns($grid) - 1,
$groupRow = $('
');
$groupRow.insertBefore($row);
previousValue = record[data.grouping.groupBy];
}
$row.show();
});
data.params[data.paramNames.groupBy] = data.grouping.groupBy;
data.params[data.paramNames.groupByDirection] = data.grouping.direction;
},
grouping: function ($grid, records) {
var data = $grid.data();
records.sort(gj.grid.methods.createDefaultSorter(data.grouping.direction, data.grouping.groupBy));
},
createExpandCollapseHandler: function (data) {
return function (e) {
var $cell = $(this),
$display = $cell.children('div[data-role="display"]'),
$groupRow = $cell.closest('tr');
if ($groupRow.next(':visible').data('role') === 'row') {
$groupRow.nextUntil('[data-role="group"]').hide();
$display.empty().append(data.icons.expand);
} else {
$groupRow.nextUntil('[data-role="group"]').show();
$display.empty().append(data.icons.collapse);
}
};
}
},
public: { },
configure: function ($grid) {
var column, data = $grid.data();
$.extend(true, $grid, gj.grid.plugins.grouping.public);
if (data.grouping && data.grouping.groupBy) {
column = {
title: '',
field: '',
width: data.defaultIconColumnWidth,
align: 'center',
stopPropagation: true,
cssClass: 'gj-cursor-pointer gj-unselectable'
};
data.columns = [column].concat(data.columns);
$grid.on('initialized', function () {
gj.grid.plugins.grouping.private.init($grid);
});
$grid.on('dataFiltered', function (e, records) {
gj.grid.plugins.grouping.private.grouping($grid, records);
});
}
}
};
/* global window alert jQuery gj */
/**
* @widget Tree
* @plugin Base
*/
if (typeof(gj.tree) === 'undefined') {
gj.tree = {
plugins: {}
};
}
gj.tree.config = {
base: {
/** When this setting is enabled the content of the tree will be loaded automatically after the creation of the tree.
* @type boolean
* @default true
* @example disabled
*
*
* @example enabled
*
*
*/
autoLoad: true,
/** The type of the node selection.
* If the type is set to multiple the user will be able to select more then one node in the tree.
* @type (single|multiple)
* @default single
* @example Single.Selection
*
*
* @example Multiple.Selection
*
*
*/
selectionType: 'single',
/** This setting enable cascade selection and unselection of children
* @type boolean
* @default false
* @example Sample
*
*
*/
cascadeSelection: false,
/** The data source of tree.
* @additionalinfo If set to string, then the tree is going to use this string as a url for ajax requests to the server.
* If set to object, then the tree is going to use this object as settings for the jquery ajax function.
* If set to array, then the tree is going to use the array as data for tree nodes.
* @type (string|object|array)
* @default undefined
* @example Local.DataSource
*
*
* @example Remote.DataSource
*
*
*/
dataSource: undefined,
/** Primary key field name.
* @type string
* @default undefined
* @example sample
*
*
*/
primaryKey: undefined,
/** Text field name.
* @type string
* @default 'text'
* @example sample
*
*
*/
textField: 'text',
/** Children field name.
* @type string
* @default 'children'
* @example Custom.FieldName
*
*
*/
childrenField: 'children',
/** Image css class field name.
* @type string
* @default 'imageCssClass'
* @example Default.Name
*
*
* @example Custom.Name
*
*
*
*/
imageCssClassField: 'imageCssClass',
/** Image url field name.
* @type string
* @default 'imageUrl'
* @example Default.HTML.Field.Name
*
*
* @example Custom.HTML.Field.Name
*
*
*/
imageUrlField: 'imageUrl',
/** Image html field name.
* @type string
* @default 'imageHtml'
* @example Default.HTML.Field.Name
*
*
* @example Custom.HTML.Field.Name
*
*
*/
imageHtmlField: 'imageHtml',
/** Width of the tree.
* @type number
* @default undefined
* @example JS.Config
*
*
* @example HTML.Config
*
*
*/
width: undefined,
/** The name of the UI library that is going to be in use.
* @additionalinfo The css files for Bootstrap or Material Design Lite should be manually included if you use those as UI Library.
* @type (base|bootstrap|materialdesign)
* @default single
* @example Bootstrap.3
*
*
* @example MaterialDesign
*
*
*/
uiLibrary: 'base',
/** The name of the icons library that is going to be in use. Currently we support Material Icons, Font Awesome and Glyphicons.
* @additionalinfo If you use Bootstrap 3 as uiLibrary, then the iconsLibrary is set to Glyphicons by default.
* If you use Material Design as uiLibrary, then the iconsLibrary is set to Material Icons by default.
* The css files for Material Icons, Font Awesome or Glyphicons should be manually included to the page where the grid is in use.
* @type (materialicons|fontawesome|glyphicons)
* @default undefined
* @example Base.Theme.Material.Icons
*
*
* @example Bootstrap.4.Font.Awesome
*
*
*/
iconsLibrary: undefined,
autoGenId: 1,
indentation: 16,
style: {
wrapper: 'gj-unselectable',
list: 'gj-tree-list',
item: 'gj-tree-item',
active: 'gj-tree-base-active',
display: undefined,
leafIcon: undefined
},
icons: {
expand: '+',
collapse: '-'
}
},
bootstrap: {
indentation: 24,
style: {
wrapper: 'gj-unselectable',
list: 'gj-tree-bootstrap-list list-group',
item: 'gj-tree-item list-group-item',
active: 'active'
},
iconsLibrary: 'glyphicons'
},
bootstrap4: {
indentation: 24,
style: {
wrapper: 'gj-unselectable',
list: 'gj-tree-bootstrap-list list-group',
item: 'gj-tree-item list-group-item',
active: 'active'
}
},
materialdesign: {
indentation: 24,
style: {
wrapper: 'gj-unselectable',
list: 'gj-tree-mdl-list mdl-list',
item: 'gj-tree-item gj-tree-mdl-item mdl-list__item',
active: 'gj-tree-mdl-active',
display: 'mdl-list__item-primary-content',
//expandIcon: 'material-icons mdl-list__item-icon gj-cursor-pointer gj-mdl-icon-plus',
//collapseIcon: 'material-icons mdl-list__item-icon gj-cursor-pointer gj-mdl-icon-minus',
leafIcon: undefined
},
iconsLibrary: 'materialicons'
},
materialicons: {
indentation: 24,
style: {
expander: 'gj-tree-material-icons-expander'
},
icons: {
expand: 'add',
collapse: 'remove'
}
},
fontawesome: {
style: {
expander: 'gj-tree-font-awesome-expander'
},
icons: {
expand: '',
collapse: ''
}
},
glyphicons: {
style: {
expander: 'gj-tree-glyphicons-expander'
},
icons: {
expand: '',
collapse: ''
}
}
};
/**
* @widget Tree
* @plugin Base
*/
gj.tree.events = {
/**
* Event fires when the tree is initialized
* @event initialized
* @param {object} e - event data
* @example sample
*
*
*
*/
initialized: function ($tree) {
$tree.triggerHandler('initialized');
},
/**
* Event fired before data binding takes place.
* @event dataBinding
* @param {object} e - event data
* @example sample
*
*
*/
dataBinding: function ($tree) {
$tree.triggerHandler('dataBinding');
},
/**
* Event fires after the loading of the data in the grid.
* @event dataBound
* @param {object} e - event data
* @example sample
*
*
*/
dataBound: function ($tree) {
$tree.triggerHandler('dataBound');
},
/**
* Event fires after selection of tree node.
* @event select
* @param {object} e - event data
* @param {object} node - the node as jquery object
* @param {string} id - the id of the record
* @example sample
*
*
*/
select: function ($tree, $node, id) {
return $tree.triggerHandler('select', [$node, id]);
},
/**
* Event fires on un selection of tree node
* @event unselect
* @param {object} e - event data
* @param {object} node - the node as jquery object
* @param {string} id - the id of the record
* @example sample
*
*
*/
unselect: function ($tree, $node, id) {
return $tree.triggerHandler('unselect', [$node, id]);
},
/**
* Event fires before node expand.
* @event expand
* @param {object} e - event data
* @param {object} node - the node as jquery object
* @param {string} id - the id of the record
* @example Event.Sample
*
*
*/
expand: function ($tree, $node, id) {
return $tree.triggerHandler('expand', [$node, id]);
},
/**
* Event fires before node collapse.
* @event collapse
* @param {object} e - event data
* @param {object} node - the node as jquery object
* @param {string} id - the id of the record
* @example Event.Sample
*
*
*/
collapse: function ($tree, $node, id) {
return $tree.triggerHandler('collapse', [$node, id]);
},
/**
* Event fires before tree destroy
* @event destroying
* @param {object} e - event data
* @example Event.Sample
*
*
*
*
*/
destroying: function ($tree) {
return $tree.triggerHandler('destroying');
},
/**
* Event fires when the data is bound to node.
* @event nodeDataBound
* @param {object} e - event data
* @param {object} node - the node as jquery object
* @param {string} id - the id of the record
* @param {object} record - the data of the node record
* @example Event.Sample
*
*
*/
nodeDataBound: function ($tree, $node, id, record) {
return $tree.triggerHandler('nodeDataBound', [$node, id, record]);
}
}
/*global gj $*/
gj.tree.methods = {
init: function (jsConfig) {
this.attr('data-type', 'tree');
gj.widget.prototype.init.call(this, jsConfig, 'tree');
gj.tree.methods.initialize.call(this);
if (this.data('autoLoad')) {
this.reload();
}
return this;
},
initialize: function () {
var data = this.data(),
$root = $('
');
this.empty().addClass(data.style.wrapper).append($root);
if (data.width) {
this.width(data.width);
}
gj.tree.events.initialized(this);
},
useHtmlDataSource: function ($tree, data) {
data.dataSource = [];
},
render: function ($tree, response) {
if (response) {
if (typeof (response) === 'string' && JSON) {
response = JSON.parse(response);
}
$tree.data('records', gj.tree.methods.getRecords($tree, response));
gj.tree.methods.loadData($tree);
}
return $tree;
},
filter: function ($grid) {
return $grid.data().dataSource;
},
getRecords: function ($tree, response) {
var i, id, nodeData, result = [],
data = $tree.data();
for (i = 0; i < response.length; i++) {
id = data.primaryKey ? response[i][data.primaryKey] : data.autoGenId++;
nodeData = { id: id, data: response[i] };
if (response[i][data.childrenField] && response[i][data.childrenField].length) {
nodeData.children = gj.tree.methods.getRecords($tree, response[i][data.childrenField]);
delete response[i][data.childrenField];
}
result.push(nodeData);
}
return result;
},
loadData: function ($tree) {
var i,
records = $tree.data('records'),
$root = $tree.children('ul');
gj.tree.events.dataBinding($tree);
$root.off().empty();
for (i = 0; i < records.length; i++) {
gj.tree.methods.appendNode($tree, $root, records[i], 1);
}
gj.tree.events.dataBound($tree);
},
appendNode: function ($tree, $parent, nodeData, level, position) {
var i, $node, $newParent, $span, $img,
data = $tree.data(),
$node = $('').addClass(data.style.item),
$wrapper = $(''),
$expander = $('').addClass(data.style.expander),
$display = $('' + nodeData.data[data.textField] + '');
if (data.indentation) {
$wrapper.append('');
}
$expander.on('click', gj.tree.methods.expanderClickHandler($tree));
$wrapper.append($expander);
$display.addClass(data.style.display).on('click', gj.tree.methods.displayClickHandler($tree));
$wrapper.append($display);
$node.append($wrapper);
if (position) {
$parent.find('li:eq(' + (position - 1) + ')').before($node);
} else {
$parent.append($node);
}
if (nodeData.children && nodeData.children.length) {
$expander.empty().append(data.icons.expand);
$newParent = $('
').addClass(data.style.list).addClass('gj-hidden');
$node.append($newParent);
for (i = 0; i < nodeData.children.length; i++) {
gj.tree.methods.appendNode($tree, $newParent, nodeData.children[i], level + 1);
}
} else {
data.style.leafIcon ? $expander.addClass(data.style.leafIcon) : $expander.html(' ');
}
if (data.imageCssClassField && nodeData.data[data.imageCssClassField]) {
$('').insertBefore($display);
} else if (data.imageUrlField && nodeData.data[data.imageUrlField]) {
$span = $('');
$span.insertBefore($display);
$img = $('');
$img.attr('width', $span.width()).attr('height', $span.height());
$span.append($img);
} else if (data.imageHtmlField && nodeData.data[data.imageHtmlField]) {
$img = $(nodeData.data[data.imageHtmlField]);
$img.attr('data-role', 'image');
$img.insertBefore($display);
}
gj.tree.events.nodeDataBound($tree, $node, nodeData.id, nodeData.data);
},
expanderClickHandler: function ($tree) {
return function (e) {
var $expander = $(this),
$node = $expander.closest('li');
if ($expander.attr('data-mode') === 'close') {
$tree.expand($node);
} else {
$tree.collapse($node);
}
}
},
expand: function ($tree, $node, cascade) {
var $children, i,
$expander = $node.find('>[data-role="wrapper"]>[data-role="expander"]'),
data = $tree.data(),
id = $node.attr('data-id'),
$list = $node.children('ul');
if ($list && $list.length && gj.tree.events.expand($tree, $node, id) !== false) {
$list.show();
$expander.attr('data-mode', 'open');
$expander.empty().append(data.icons.collapse);
if (cascade) {
$children = $node.find('ul>li');
for (i = 0; i < $children.length; i++) {
gj.tree.methods.expand($tree, $($children[i]), cascade);
}
}
}
return $tree;
},
collapse: function ($tree, $node, cascade) {
var $children, i,
$expander = $node.find('>[data-role="wrapper"]>[data-role="expander"]'),
data = $tree.data(),
id = $node.attr('data-id'),
$list = $node.children('ul');
if ($list && $list.length && gj.tree.events.collapse($tree, $node, id) !== false) {
$list.hide();
$expander.attr('data-mode', 'close');
$expander.empty().append(data.icons.expand);
if (cascade) {
$children = $node.find('ul>li');
for (i = 0; i < $children.length; i++) {
gj.tree.methods.collapse($tree, $($children[i]), cascade);
}
}
}
return $tree;
},
expandAll: function ($tree) {
var i, $nodes = $tree.find('ul>li');
for (i = 0; i < $nodes.length; i++) {
gj.tree.methods.expand($tree, $($nodes[i]), true);
}
return $tree;
},
collapseAll: function ($tree) {
var i, $nodes = $tree.find('ul>li');
for (i = 0; i < $nodes.length; i++) {
gj.tree.methods.collapse($tree, $($nodes[i]), true);
}
return $tree;
},
displayClickHandler: function ($tree) {
return function (e) {
var $display = $(this),
$node = $display.closest('li'),
cascade = $tree.data().cascadeSelection;
if ($node.attr('data-selected') === 'true') {
gj.tree.methods.unselect($tree, $node, cascade);
} else {
if ($tree.data('selectionType') === 'single') {
gj.tree.methods.unselectAll($tree);
}
gj.tree.methods.select($tree, $node, cascade);
}
}
},
selectAll: function ($tree) {
var i, $nodes = $tree.find('ul>li');
for (i = 0; i < $nodes.length; i++) {
gj.tree.methods.select($tree, $($nodes[i]), true);
}
return $tree;
},
select: function ($tree, $node, cascade) {
var i, $children, data = $tree.data();
if ($node.attr('data-selected') !== 'true' && gj.tree.events.select($tree, $node, $node.attr('data-id')) !== false) {
$node.addClass(data.style.active).attr('data-selected', 'true');
if (cascade) {
$children = $node.find('ul>li');
for (i = 0; i < $children.length; i++) {
gj.tree.methods.select($tree, $($children[i]), cascade);
}
}
}
},
unselectAll: function ($tree) {
var i, $nodes = $tree.find('ul>li');
for (i = 0; i < $nodes.length; i++) {
gj.tree.methods.unselect($tree, $($nodes[i]), true);
}
return $tree;
},
unselect: function ($tree, $node, cascade) {
var i, $children, data = $tree.data();
if ($node.attr('data-selected') === 'true' && gj.tree.events.unselect($tree, $node, $node.attr('data-id')) !== false) {
$node.removeClass($tree.data().style.active).removeAttr('data-selected');
if (cascade) {
$children = $node.find('ul>li');
for (i = 0; i < $children.length; i++) {
gj.tree.methods.unselect($tree, $($children[i]), cascade);
}
}
}
},
getSelections: function ($list) {
var i, $node, children,
result = [],
$nodes = $list.children('li');
if ($nodes && $nodes.length) {
for (i = 0; i < $nodes.length; i++) {
$node = $($nodes[i]);
if ($node.attr('data-selected') === 'true') {
result.push($node.attr('data-id'));
} else if ($node.has('ul')) {
children = gj.tree.methods.getSelections($node.children('ul'));
if (children.length) {
result = result.concat(children);
}
}
}
}
return result;
},
getById: function ($tree, id, records) {
var i, result = undefined;
for (i = 0; i < records.length; i++) {
if (id == records[i].id) {
result = records[i];
break;
} else if (records[i].children && records[i].children.length) {
result = gj.tree.methods.getById($tree, id, records[i].children);
if (result) {
break;
}
}
}
return result;
},
getDataById: function ($tree, id, records) {
var result = gj.tree.methods.getById($tree, id, records);
return result ? result.data : result;
},
getDataByText: function ($tree, text, records) {
var i, id,
result = undefined,
data = $tree.data();
for (i = 0; i < records.length; i++) {
if (text === records[i].data[data.textField]) {
result = records[i].data;
break;
} else if (records[i].children && records[i].children.length) {
result = gj.tree.methods.getDataByText($tree, text, records[i].children);
if (result) {
break;
}
}
}
return result;
},
getNodeById: function ($list, id) {
var i, $node,
$result = undefined,
$nodes = $list.children('li');
if ($nodes && $nodes.length) {
for (i = 0; i < $nodes.length; i++) {
$node = $($nodes[i]);
if (id == $node.attr('data-id')) {
$result = $node;
break;
} else if ($node.has('ul')) {
$result = gj.tree.methods.getNodeById($node.children('ul'), id);
if ($result) {
break;
}
}
}
}
return $result;
},
getNodeByText: function ($list, text) {
var i, $node,
$result = undefined,
$nodes = $list.children('li');
if ($nodes && $nodes.length) {
for (i = 0; i < $nodes.length; i++) {
$node = $($nodes[i]);
if (text === $node.find('>[data-role="wrapper"]>[data-role="display"]').text()) {
$result = $node;
break;
} else if ($node.has('ul')) {
$result = gj.tree.methods.getNodeByText($node.children('ul'), text);
if ($result) {
break;
}
}
}
}
return $result;
},
addNode: function ($tree, data, $parent, position) {
var nodeData = gj.tree.methods.getRecords($tree, [data]),
level = $parent.parentsUntil('[data-type="tree"]', 'ul').length + 1;
if (!$parent || !$parent.length) {
$parent = $tree.children('ul');
}
gj.tree.methods.appendNode($tree, $parent, nodeData[0], level, position);
return $tree;
},
remove: function ($tree, $node) {
gj.tree.methods.removeDataById($tree, $node.attr('data-id'), $tree.data('records'));
$node.remove();
return $tree;
},
removeDataById: function ($tree, id, records) {
var i;
for (i = 0; i < records.length; i++) {
if (id == records[i].id) {
records.splice(i, 1);
break;
} else if (records[i].children && records[i].children.length) {
gj.tree.methods.removeDataById($tree, id, records[i].children);
}
}
},
destroy: function ($tree) {
var data = $tree.data();
if (data) {
gj.tree.events.destroying($tree);
$tree.xhr && $tree.xhr.abort();
$tree.off();
$tree.removeData();
$tree.removeAttr('data-type');
$tree.removeClass().empty();
}
return $tree;
}
}
/**
* @widget Tree
* @plugin Base
*/
gj.tree.widget = function ($element, arguments) {
var self = this,
methods = gj.tree.methods;
/**
* Reload the tree.
* @method
* @param {object} params - Params that needs to be send to the server. Only in use for remote data sources.
* @return jQuery object
* @example Method.Sample
*
*
*
*
*/
self.reload = function (params) {
return gj.widget.prototype.reload.call(this, params);
};
/**
* Render data in the tree
* @method
* @param {object} response - An object that contains the data that needs to be loaded in the tree.
* @fires dataBinding, dataBound
* @return tree
* @example sample
*
*
*/
self.render = function (response) {
return methods.render(this, response);
};
/**
* Add node to the tree.
* @method
* @param {object} data - The node data.
* @param {object} parentNode - Parent node as jquery object.
* @param {Number} position - Position where the new node need to be added.
* @return jQuery object
* @example Append.ToRoot
*
*
*
*
* @example Append.Parent
*
*
*
*
* @example Bootstrap
*
*
*
*
* @example Prepend
*
*
*
*
* @example Position
*
*
*
*
*/
self.addNode = function (data, $parentNode, position) {
return methods.addNode(this, data, $parentNode, position);
};
/**
* Remove node from the tree.
* @method
* @param {object} node - The node as jQuery object
* @return jQuery object
* @example Method.Sample
*
*
*
*
*/
self.expandAll = function () {
return methods.expandAll(this);
};
/**
* Collapse all tree nodes
* @method
* @return jQuery object
* @example Sample
*
*
*
*
*/
self.collapseAll = function () {
return methods.collapseAll(this);
};
/**
* Return node data by id of the record.
* @method
* @param {string|number} id - The id of the record that needs to be returned
* @return object
* @example sample
*
*
*
*
*/
self.getDataById = function (id) {
return methods.getDataById(this, id, this.data('records'));
};
/**
* Return node data by text.
* @method
* @param {string} text - The text of the record that needs to be returned
* @return object
* @example sample
*
*
*
*
*/
self.getDataByText = function (text) {
return methods.getDataByText(this, text, this.data('records'));
};
/**
* Return node by id of the record.
* @method
* @param {string} id - The id of the node that needs to be returned
* @return jQuery object
* @example sample
*
*
*/
self.getNodeById = function (id) {
return methods.getNodeById(this.children('ul'), id);
};
/**
* Return node by text.
* @method
* @param {string} text - The text in the node that needs to be returned
* @return jQuery object
* @example sample
*
*
*/
self.getNodeByText = function (text) {
return methods.getNodeByText(this.children('ul'), text);
};
/**
* Select all tree nodes
* @method
* @return jQuery object
* @example Sample
*
*
*
*
*/
self.selectAll = function () {
return methods.selectAll(this);
};
/**
* Unselect all tree nodes
* @method
* @return jQuery object
* @example Sample
*
*
*
*
*/
self.unselectAll = function () {
return methods.unselectAll(this);
};
/**
* Return an array with the ids of the selected nodes.
* @method
* @return array
* @example Sample
*
*
*
*
*/
self.getSelections = function () {
return methods.getSelections(this.children('ul'));
};
$.extend($element, self);
if ('tree' !== $element.attr('data-type')) {
methods.init.apply($element, arguments);
}
return $element;
};
gj.tree.widget.prototype = new gj.widget();
gj.tree.widget.constructor = gj.tree.widget;
(function ($) {
$.fn.tree = function (method) {
var $widget;
if (this && this.length) {
if (typeof method === 'object' || !method) {
return new gj.tree.widget(this, arguments);
} else {
$widget = new gj.tree.widget(this, null);
if ($widget[method]) {
return $widget[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
throw 'Method ' + method + ' does not exist.';
}
}
}
};
})(jQuery);
/**
* @widget Tree
* @plugin Checkboxes
*/
gj.tree.plugins.checkboxes = {
config: {
base: {
/** Add checkbox for each node, if set to true.
* @type Boolean
* @default undefined
* @example Base.Theme
*
*
* @example Bootstrap
*
*
Bootstrap Treeview With Checkboxes
*
*
*
* @example Material.Design
*
*
Material Design Treeview With Checkboxes
*
*
*
*/
checkboxes: undefined,
/** Name of the source field, that indicates if the checkbox is checked.
* @type string
* @default 'checked'
* @example Custom.Name
*
*
*/
checkedField: 'checked',
}
},
private: {
nodeDataBound: function ($tree, $node, id, record) {
var data = $tree.data(),
$expander = $node.find('> [data-role="wrapper"] > [data-role="expander"]'),
$checkbox = $(''),
$wrapper = $('').append($checkbox);
$checkbox = $checkbox.checkbox({ uiLibrary: data.uiLibrary });
if (record[data.checkedField]) {
$checkbox.state('checked');
}
$checkbox.on('click', function (e) {
var $node = $checkbox.closest('li'),
state = $checkbox.state();
gj.tree.plugins.checkboxes.private.updateChildrenState($node, state);
gj.tree.plugins.checkboxes.private.updateParentState($node, state);
});
$expander.after($wrapper);
},
updateParentState: function ($node, state) {
var $parentNode, $parentCheckbox, $siblingCheckboxes, allChecked, allUnchecked, parentState;
$parentNode = $node.parent('ul').parent('li');
if ($parentNode.length === 1) {
$parentCheckbox = $node.parent('ul').parent('li').find('> [data-role="wrapper"] > [data-role="checkbox"] input[type="checkbox"]');
$siblingCheckboxes = $node.siblings().find('> [data-role="wrapper"] > span[data-role="checkbox"] input[type="checkbox"]');
allChecked = (state === 'checked');
allUnchecked = (state === 'unchecked');
parentState = 'indeterminate';
$.each($siblingCheckboxes, function () {
var state = $(this).checkbox('state');
if (allChecked && state !== 'checked') {
allChecked = false;
}
if (allUnchecked && state !== 'unchecked') {
allUnchecked = false;
}
});
if (allChecked && !allUnchecked) {
parentState = 'checked';
}
if (!allChecked && allUnchecked) {
parentState = 'unchecked';
}
$parentCheckbox.checkbox('state', parentState);
gj.tree.plugins.checkboxes.private.updateParentState($parentNode, $parentCheckbox.checkbox('state'));
}
},
updateChildrenState: function ($node, state) {
var $childrenCheckboxes = $node.find('ul li [data-role="wrapper"] [data-role="checkbox"] input[type="checkbox"]');
if ($childrenCheckboxes.length > 1) {
$.each($childrenCheckboxes, function () {
$(this).checkbox('state', state);
});
}
}
},
public: {
/** Get ids of all checked nodes
* @method
* @return Array
* @example Base.Theme
*
*
*
*/
getCheckedNodes: function () {
var result = [],
checkboxes = this.find('li [data-role="checkbox"] input[type="checkbox"]');
$.each(checkboxes, function () {
var checkbox = $(this);
if (checkbox.checkbox('state') === 'checked') {
result.push(checkbox.closest('li').data('id'));
}
});
return result;
}
},
configure: function ($tree) {
$.extend(true, $tree, gj.tree.plugins.checkboxes.public);
if ($tree.data('checkboxes')) {
$tree.on('nodeDataBound', function (e, $node, id, record) {
gj.tree.plugins.checkboxes.private.nodeDataBound($tree, $node, id, record);
});
$tree.on('dataBound', function () {
$nodes = $tree.find('li[data-role="node"]');
$.each($nodes, function () {
var $node = $(this),
state = $node.find('[data-role="checkbox"] input[type="checkbox"]').checkbox('state');
if (state === 'checked') {
gj.tree.plugins.checkboxes.private.updateChildrenState($node, state);
gj.tree.plugins.checkboxes.private.updateParentState($node, state);
}
});
});
}
}
};
/**
* @widget Tree
* @plugin DragAndDrop
*/
gj.tree.plugins.dragAndDrop = {
config: {
base: {
/** Enables drag and drop functionality for each node.
* @type Boolean
* @default undefined
* @example Base.Theme
*
*
* @example Bootstrap
*