/* 1. the confirm script can be configured to set confirmation on links.each link configured must have all the data to be sent in the a. data to be displayed on the confirm box b. data to be sent in the form submitted using AJAX or FORM submit. UI-MESSAGE-OPTIONS opt.message message to display in confirm div e.g:"Are you sure you want to do this" opt.successMessage message to display on success. opt.errorMessage message to display on error. opt.confirmButtonText opt.cancelButtonText GLOBAL-OPTIONS : opt.grid selector for grid on which this confirm is called.the ajax calls are DELEGATED to all the links with the class[opt.linkClass] opt.linkClass class for each ajax link FORM-OPTIONS : confirmForm ID of the form to be submitted. isFormAjax uses ajaxSubmit if set to true. warnCondition user can define a function which returns a boolean expression. if found true the confirm button hides. warnMessage warning message. setUpForm : function(item) : item jquery wrapped link which is clicked. SECURITY-OPTIONS: opts.useAntiForgeryToken opts.token value of the token. DIALOG-OPTIONS : opts.dialog Id of the DIV which has to be changed to a dialog. (if not present the script creates one.) opts.width dialog width opts.height dialog height opts.success function attached to "OK" fired when the ajax completes successfully opts.error function atttached to "OK" fired when the ajax completes in error opt.cancel function to be attached to "Cancel" */ //===================================================================================== //opt.data function which returns a javascript object: which is set as // the data sent in the ajax call. $(this) in the function would refer to the link which is being clicked.!!! // default : the data-id field on the link is passed as Id. //opt.confirmForm if the confirm dialog contains customized form : specify the selectorID ; (function ($) { if ($.fn.gridConfirm) return false; $.fn.gridConfirm = function(opts) { var settings = $.extend({ successMessage: 'success', warnMessage: 'Set this message to warn users against unwanted action', errorMessage: 'error', message: 'Are you sure you want to do this', linkClass: 'gridConfirmLink', grid: '#grid', dialog: '#confirm-div', useOriginalDialog: false, width: 'auto', height: 'auto', severeAction: false, title: 'Confirm', confirmButtonText: 'Ok', confirmButtonClass: '', cancelButtonText: 'Cancel', cancelButtonClass: '', useAntiForgeryToken: false, isFormAjax: true, warnCondition: false, token: $('input[name="__RequestVerificationToken"]').val(), success: function(data) { $(settings.grid).html(data); }, error: function(data) { }, type: 'GET', dataType: 'html', data: function(item) { var data = { id: item.attr('data-id') }; return data; } }, opts || { }), modal = '', getDataObject = function(item) { var dataObject = settings.data(item); if (settings.useAntiForgeryToken) dataObject.__RequestVerificationToken = settings.token; return dataObject; }, initGridLinks = function() { var grid = $(settings.grid); if (!grid && grid.length != 0) { alert('grid does not exist'); return; } grid.delegate('.' + settings.linkClass, 'click', function(evt) { evt.stopPropagation(); setUpDialog($(this), settings); return false; }); }, initDialog = function() { var dialogDiv = $(settings.dialog); if (dialogDiv.length == 0) { dialogDiv = createDialog(settings); } if (settings.useOriginalDialog == false) { var buttons = getDefaultButtons(); } modal = core.modal.init(dialogDiv, { buttons: buttons || {}, title: settings.title, width: settings.width, height: settings.height, severe: settings.severeAction }); }, closeDialog = function() { modal.close(); }, getDefaultButtons = function() { var buttons = {}, confirmButtonText = settings.confirmButtonText; if (!settings.buttons) { buttons[confirmButtonText] = { click: function() { callAjax(settings); closeDialog(settings); }, primary: true }; buttons['Cancel'] = { click: function () { if (settings.cancel) { settings.cancel(); } closeDialog(settings); } }; settings.buttons = buttons; return buttons; } else { return settings.buttons; } return null; }, callAjax = function() { var dataDiv = $(settings.dialog); var item = dataDiv.data('item'); //submit the form. Note the form is already set up when the dialog is setup in initDialog. //================================= dialogwithform ================================= if (settings.confirmForm) { var dialogForm = $(settings.confirmForm); if (!settings.isFormAjax) { dialogForm.submit(); return; } dialogForm.ajaxSubmit({ datatype: settings.dataType, type: settings.type, data: getDataObject(item), success: function(data) { if (settings.success) settings.success(data, item); display(settings.successMessage, 'info', item); }, error: function(data) { if (settings.error) settings.error(data, item); display(settings.errorMessage, 'error', item); } }); return; } var ajaxDelegate = $.ajax({ url: item.attr('href'), type: settings.type, dataType: settings.dataType, data: getDataObject(item) }); if (settings.success) { ajaxDelegate.success(function(data) { if (settings.success) settings.success(data); display(settings.successMessage, 'info', item); }); } if (settings.error) { ajaxDelegate.error(function(data) { if (settings.error) settings.error(data); var errormessage = (typeof data !== 'undefined' && data.responseText !== 'undefined' && data.responseText.length > 0) ? data.responseText : settings.errorMessage; display(errormessage, 'error', item); }); } }, setUpDialog = function(item) { modal.setOptions({ title: format(settings.title, item) }); //set message on dialog if (settings.setupForm) { if (settings.resetForm) settings.resetForm(); settings.setupForm(item); } if (settings.useOriginalDialog == true) { $('div#dialog-message', modal).remove(); } else { setMessage(''); if (settings.warnCondition && settings.warnCondition()) { $(modal.element).next('.modal-buttons').find('.button').first().hide(); setMessage(format(settings.warnMessage, item)); } else { $(modal.element).next('.modal-buttons').find('.button').first().show(); setMessage(format(settings.message, item)); } } $(settings.dialog).data('item', item); modal.open(); }, createDialog = function() { var grid = $(settings.grid); var newdialog = $('
'); newdialog.append('
'); newdialog.css({ 'display': 'none' }).attr('id', settings.dialog.replace('#', '')); grid.append(newdialog); return newdialog; }, setMessage = function(msg) { var dialog = $(settings.dialog); var msgContent = $('

').html(msg); dialog.find('#dialog-message').empty().append(msgContent); }, display = function(msg, msgType, item) { var formattedMessage = format(msg, item); $.Flash(msgType, formattedMessage); }, format = function(message, item) { if (!(message instanceof Array)) return message; var s = message[0]; i = 1; while (i < message.length) { s = s.replace(new RegExp('\\{' + (i - 1) + '\\}', 'gm'), item.attr(message[i])); i++; } return s; }; initGridLinks(); initDialog(); }; $.fn.SVGConfirm =$.fn.gridConfirm; })(jQuery);