Advertisement
altair

jQuery.dialogHelper

Feb 5th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" />
  2. /// <reference path="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.17/jquery-ui.min.js" />
  3.  
  4. (function ($) {
  5.     $.fn.dialogHelper = function (options) {
  6.  
  7.         // Read settings
  8.         var settings = $.extend({
  9.             dataDialogOpen: "dialog-open",
  10.             dataDialogOptions: "dialog-options",
  11.             dataDialogMessage: "dialog-message",
  12.             dataConfirmPrompt: "confirm-prompt",
  13.             dataConfirmTitle: "confirm-title",
  14.             dataConfirmButtons: "confirm-buttons",
  15.             dataConfirmDone: "confirm-done",
  16.             confirmTitle: "Confirmation",
  17.             confirmYes: "Yes",
  18.             confirmNo: "No",
  19.             showEffect: "clip",
  20.             hideEffect: "clip"
  21.         }, options);
  22.  
  23.         // Initialize general dialogs
  24.         $("*[data-" + settings.dataDialogOpen + "]").each(function () {
  25.             var dialogElement = $("#" + $(this).data(settings.dataDialogOpen));
  26.  
  27.             // Create dialog
  28.             dialogElement.dialog({
  29.                 autoOpen: false,
  30.                 modal: true,
  31.                 show: settings.showEffect,
  32.                 hide: settings.hideEffect
  33.             });
  34.  
  35.             // Set dialog options
  36.             if (dialogElement.data(settings.dataDialogOptions) != null) {
  37.                 var options = dialogElement.data(settings.dataDialogOptions).split(";");
  38.                 for (var i = 0; i < options.length; i++) {
  39.                     var option = options[i].split(":");
  40.                     dialogElement.dialog("option", option[0], option[1]);
  41.                 }
  42.             }
  43.  
  44.             // Recreate all buttons as dialog buttons
  45.             var buttons = [];
  46.             $("input[type=submit]", dialogElement).each(function () {
  47.                 var button = $(this);
  48.                 button.hide();
  49.                 buttons.push({ text: button.val(), click: function () { button.click() } });
  50.             });
  51.             if (buttons.length != 0) dialogElement.dialog("option", "buttons", buttons);
  52.  
  53.             // Move to <form runat=server>
  54.             dialogElement.parent().appendTo($("form:first"));
  55.  
  56.             // Handle click
  57.             $(this).click(function () {
  58.                 dialogElement.dialog("open");
  59.                 return false;
  60.             });
  61.         });
  62.  
  63.         // Initialize message dialogs
  64.         $("*[data-" + settings.dataDialogMessage + "]").each(function () {
  65.             $(this).dialog({
  66.                 modal: true,
  67.                 show: settings.showEffect,
  68.                 hide: settings.hideEffect,
  69.                 title: $(this).data(settings.dataDialogMessage),
  70.                 resizable: false,
  71.                 buttons: { OK: function () { $(this).dialog("close"); } }
  72.             });
  73.         });
  74.  
  75.         // Click confirmation
  76.         $("*[data-" + settings.dataConfirmPrompt + "]").click(function () {
  77.             var clickedObject = $(this);
  78.  
  79.             // Get href from this element or its children
  80.             var href = clickedObject.attr("href");
  81.             if (href == null) href = clickedObject.children("*[href]").attr("href");
  82.             if (href && href.indexOf("javascript:") == 0) href = null;
  83.  
  84.             if (clickedObject.data(settings.dataConfirmDone) != true) {
  85.                 // Get button names
  86.                 var title = settings.confirmTitle;
  87.                 var buttonYes = settings.confirmYes;
  88.                 var buttonNo = settings.confirmNo;
  89.                 if (clickedObject.data(settings.dataConfirmTitle) != null) title = $(this).data(settings.dataConfirmTitle);
  90.                 if (clickedObject.data(settings.dataConfirmButtons) != null) {
  91.                     var buttonNames = $(this).data(settings.dataConfirmButtons).split(";");
  92.                     buttonYes = buttonNames[0];
  93.                     buttonNo = buttonNames[1];
  94.                 }
  95.  
  96.                 // Display jQuery UI confirmation dialog
  97.                 $("<div><p>" + clickedObject.data(settings.dataConfirmPrompt) + "</p></div>").dialog({
  98.                     modal: true,
  99.                     show: settings.showEffect,
  100.                     hide: settings.hideEffect,
  101.                     resizable: false,
  102.                     title: title,
  103.                     buttons: [{
  104.                         text: buttonYes,
  105.                         click: function () {
  106.                             $(this).dialog("close");
  107.                             if (href) {
  108.                                 window.location = href;
  109.                             } else {
  110.                                 clickedObject.data(settings.dataConfirmDone, true);
  111.                                 clickedObject.click();
  112.                             }
  113.                         }
  114.                     },
  115.                     {
  116.                         text: buttonNo,
  117.                         click: function () {
  118.                             $(this).dialog("close");
  119.                         }
  120.                     }]
  121.                 });
  122.                 clickedObject.data(settings.dataConfirmDone, false);
  123.             }
  124.             return clickedObject.data(settings.dataConfirmDone);
  125.         });
  126.  
  127.     }
  128. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement