Advertisement
Guest User

nette.ajax.dialogs

a guest
Sep 20th, 2012
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($, undefined) {
  2.  
  3.  
  4. /**
  5.  * Nette ajax dialogs extension:
  6.  */
  7. $.nette.ext('dialogs', {
  8.    
  9.         /**
  10.          * This method is called after extension addition:
  11.          */
  12.         init: function() {
  13.             // Register "public" members:
  14.             var that = this;
  15.             $.nette.showDialog = function(url, snippet, args, options) {
  16.                 that.showDialog(url, snippet, args, options);
  17.             }
  18.         },
  19.        
  20.         /**
  21.          * When ajax request returns check whether there is a dialog command:
  22.          */
  23.         success: function(payload) {
  24.  
  25.             // Close selected dialogs:
  26.             if (payload.dialogs) {
  27.                 for (var i in payload.dialogs) {
  28.                    
  29.                     // Close all dialogs:
  30.                     if (i == 'all') {
  31.                         $('.nette-dialog').dialog('close').remove();
  32.                         break;
  33.                     }
  34.                    
  35.                     // Close only one dialog:
  36.                     $('.nette-dialog-' + i).dialog('close').remove();
  37.                 }
  38.             }
  39.  
  40.         }
  41.        
  42. }, {
  43.    
  44.     dialog: null,
  45.     datePickerClass : 'datepicker',
  46.    
  47.     /**
  48.      * Downloads a form snippet from given URL
  49.      * and displays it in jQuery UI dialog.
  50.      * @param {string} url URL to retrieve the form from
  51.      * @param {string} snippet Snippet to be loaded
  52.      * @param {object} args Arguments to pass along the request
  53.      * @param {object} options Options to pass to the modal dialog
  54.      */
  55.     showDialog: function(url, snippet, args, options) {
  56.        
  57.         // Default dialog options:
  58.         var that = this;
  59.         options = options || {};
  60.         options = $.extend(true, {
  61.             autoOpen: true,
  62.             width: 500,
  63.             hide: 'fade',
  64.             modal: true,
  65.             close: function() {
  66.                 that.dialog.remove();
  67.             }
  68.         }, options);
  69.  
  70.         // Create dialog:
  71.         this.dialog = $('<div>').addClass('nette-dialog')
  72.             .addClass('nette-dialog-' + snippet)            
  73.             .appendTo('body').dialog(options);
  74.        
  75.         // Dialog has to be updated after each snippet refresh:
  76.         $.nette.watchSnippet(snippet, function() {
  77.             that.updateDialog(that.dialog);
  78.         });
  79.        
  80.         // Change snippet name to the element id:
  81.         snippet = 'snippet--' + snippet;
  82.         this.dialog.attr('id', snippet);
  83.  
  84.         // Download form snippet and place it to the dialog:
  85.         $.nette.post(url,args || {}, function(payload) {
  86.             // If the snippet was received:
  87.             if (payload.snippets && payload.snippets[snippet]) {
  88.                 $.nette.updateSnippets(payload);
  89.                 that.updateDialog(that.dialog);
  90.             } else {
  91.                 that.showError('Nepodařilo se stáhnout formulář');
  92.             }
  93.         });
  94.    
  95.     },
  96.    
  97.     /**
  98.      * Displays an error message inside the dialog.
  99.      * @param {string} message Message to display
  100.      */
  101.     showError: function(message) {
  102.         if (this.dialog) {
  103.             this.dialog.text(message);
  104.         } else {
  105.             alert(message);
  106.         }
  107.     },
  108.    
  109.     /**
  110.      * Updates dialog to look cool and working.
  111.      * @param {jQuery} dialog Dialog div element
  112.      */
  113.     updateDialog: function(dialog) {
  114.  
  115.         // Remove first h1 and make a title from it:
  116.         if (dialog.find('h1')) {
  117.             var heading = dialog.find('h1').first();
  118.             dialog.dialog('option', 'title', heading.text());
  119.             heading.remove();                    
  120.         }
  121.        
  122.         // Style dialog inputs:
  123.         dialog.find('input[type=text], input[type=date], input[type=time]').addClass('text ui-widget-content ui-corner-all');
  124.        
  125.         // Center dialog:
  126.         dialog.dialog({position: "center"});
  127.        
  128.         // Initialize the form:
  129.         dialog.find('form').addClass('ajax');
  130.         var fst = dialog.find('input[type=text]').first();
  131.        
  132.         // If the first input is a datepicker, do not focus it because it would pop up immediatelly.
  133.         if (fst.attr('class').toLowerCase().indexOf(this.datePickerClass) == -1 || this.datePickerClass == null) {
  134.             fst.focus();
  135.         }
  136.         $.nette.load();        
  137.        
  138.     }
  139.    
  140. });
  141.  
  142.  
  143.  
  144. /**
  145.  * Nette snippet watchdog extension.
  146.  *
  147.  * This extension allows to call a function when certain
  148.  * snippet comes be redrawn
  149.  */
  150. $.nette.ext('watchdog', {
  151.    
  152.         /**
  153.          * This method is called after extension addition:
  154.          */
  155.         init: function() {
  156.             // Register "public" members:
  157.             var that = this;
  158.             $.nette.watchSnippet = function(name, callback) {
  159.                 that.addWatch(name, callback);
  160.             }
  161.             $.nette.updateSnippets = function(payload) {
  162.                 that.updateSnippets(payload);
  163.             }
  164.         },
  165.        
  166.         /**
  167.          * When ajax request returns some snippet, check whether it is watched.
  168.          */
  169.         success: function(payload) {
  170.             if (payload.snippets) {
  171.                 var that = this;
  172.                 // Ensure the callbacks will be executed after the snippets have been redrawn:
  173.                 setTimeout(function() {
  174.                     that.fireCallbacks(payload);
  175.                 }, 0);                
  176.             }
  177.         }
  178.        
  179. }, {
  180.    
  181.     snippets: [],
  182.    
  183.     /**
  184.      * Adds a watch for requested snippet.
  185.      * @param {string} name Snippet name
  186.      * @param {function} callback Callback function
  187.      */
  188.     addWatch: function(name, callback) {
  189.         name = 'snippet--' + name;
  190.         if (this.snippets[name] == undefined) {
  191.             this.snippets[name] = [];
  192.         }
  193.         this.snippets[name].push(callback);
  194.     },
  195.    
  196.     /**
  197.      * Fires all associated callbacks.
  198.      */
  199.     fireCallbacks: function(payload) {
  200.         for (var s in payload.snippets) {
  201.            
  202.             // If the snippet is on the list:
  203.             if (this.snippets[s] != undefined) {
  204.                 for (var f in this.snippets[s]) {
  205.                     var fnc = this.snippets[s][f];
  206.                     fnc(payload);
  207.                 }
  208.             }
  209.            
  210.         }
  211.     },
  212.    
  213.     /**
  214.      * Updates snippets using snippets extension and
  215.      * given payload.
  216.      */
  217.     updateSnippets: function(payload) {
  218.         if (payload.snippets) {
  219.             for (var i in payload.snippets) {
  220.                 $.nette.ext('snippets').updateSnippet(i, payload.snippets[i]);
  221.             }
  222.         }
  223.     }
  224.        
  225.    
  226. });
  227.  
  228.  
  229.  
  230. /**
  231.  * Extra post and get extension
  232.  *
  233.  * This extension adds extra methods replacing $.post by $.nette.post and
  234.  * $.get by $.nette.get which (beside default capabilities) also call event
  235.  * handlers of other Nette extensions (automatic snippet dispatching after
  236.  * ajax call etc).
  237.  */
  238. $.nette.ext('extrapost', {
  239.    
  240.         /**
  241.          * This method is called after extension addition:
  242.          */
  243.         init: function() {
  244.            
  245.             // $.post complementary method:
  246.             $.nette.post = function(url, data, success, dataType) {
  247.                 $.fn.netteAjax(null, {
  248.                     url: url,
  249.                     data: data,
  250.                     success: success,
  251.                     dataType: dataType,
  252.                     type: 'post'
  253.                 });
  254.             }
  255.            
  256.             // $.get complementary method:
  257.             $.nette.get = function(url, data, success, dataType) {
  258.                 $.fn.netteAjax(null, {
  259.                     url: url,
  260.                     data: data,
  261.                     success: success,
  262.                     dataType: dataType,
  263.                     type: 'get'
  264.                 });
  265.             }
  266.            
  267.         }
  268. });
  269.  
  270. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement