Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($, undefined) {
- /**
- * Nette ajax dialogs extension:
- */
- $.nette.ext('dialogs', {
- /**
- * This method is called after extension addition:
- */
- init: function() {
- // Register "public" members:
- var that = this;
- $.nette.showDialog = function(url, snippet, args, options) {
- that.showDialog(url, snippet, args, options);
- }
- },
- /**
- * When ajax request returns check whether there is a dialog command:
- */
- success: function(payload) {
- // Close selected dialogs:
- if (payload.dialogs) {
- for (var i in payload.dialogs) {
- // Close all dialogs:
- if (i == 'all') {
- $('.nette-dialog').dialog('close').remove();
- break;
- }
- // Close only one dialog:
- $('.nette-dialog-' + i).dialog('close').remove();
- }
- }
- }
- }, {
- dialog: null,
- datePickerClass : 'datepicker',
- /**
- * Downloads a form snippet from given URL
- * and displays it in jQuery UI dialog.
- * @param {string} url URL to retrieve the form from
- * @param {string} snippet Snippet to be loaded
- * @param {object} args Arguments to pass along the request
- * @param {object} options Options to pass to the modal dialog
- */
- showDialog: function(url, snippet, args, options) {
- // Default dialog options:
- var that = this;
- options = options || {};
- options = $.extend(true, {
- autoOpen: true,
- width: 500,
- hide: 'fade',
- modal: true,
- close: function() {
- that.dialog.remove();
- }
- }, options);
- // Create dialog:
- this.dialog = $('<div>').addClass('nette-dialog')
- .addClass('nette-dialog-' + snippet)
- .appendTo('body').dialog(options);
- // Dialog has to be updated after each snippet refresh:
- $.nette.watchSnippet(snippet, function() {
- that.updateDialog(that.dialog);
- });
- // Change snippet name to the element id:
- snippet = 'snippet--' + snippet;
- this.dialog.attr('id', snippet);
- // Download form snippet and place it to the dialog:
- $.nette.post(url,args || {}, function(payload) {
- // If the snippet was received:
- if (payload.snippets && payload.snippets[snippet]) {
- $.nette.updateSnippets(payload);
- that.updateDialog(that.dialog);
- } else {
- that.showError('Nepodařilo se stáhnout formulář');
- }
- });
- },
- /**
- * Displays an error message inside the dialog.
- * @param {string} message Message to display
- */
- showError: function(message) {
- if (this.dialog) {
- this.dialog.text(message);
- } else {
- alert(message);
- }
- },
- /**
- * Updates dialog to look cool and working.
- * @param {jQuery} dialog Dialog div element
- */
- updateDialog: function(dialog) {
- // Remove first h1 and make a title from it:
- if (dialog.find('h1')) {
- var heading = dialog.find('h1').first();
- dialog.dialog('option', 'title', heading.text());
- heading.remove();
- }
- // Style dialog inputs:
- dialog.find('input[type=text], input[type=date], input[type=time]').addClass('text ui-widget-content ui-corner-all');
- // Center dialog:
- dialog.dialog({position: "center"});
- // Initialize the form:
- dialog.find('form').addClass('ajax');
- var fst = dialog.find('input[type=text]').first();
- // If the first input is a datepicker, do not focus it because it would pop up immediatelly.
- if (fst.attr('class').toLowerCase().indexOf(this.datePickerClass) == -1 || this.datePickerClass == null) {
- fst.focus();
- }
- $.nette.load();
- }
- });
- /**
- * Nette snippet watchdog extension.
- *
- * This extension allows to call a function when certain
- * snippet comes be redrawn
- */
- $.nette.ext('watchdog', {
- /**
- * This method is called after extension addition:
- */
- init: function() {
- // Register "public" members:
- var that = this;
- $.nette.watchSnippet = function(name, callback) {
- that.addWatch(name, callback);
- }
- $.nette.updateSnippets = function(payload) {
- that.updateSnippets(payload);
- }
- },
- /**
- * When ajax request returns some snippet, check whether it is watched.
- */
- success: function(payload) {
- if (payload.snippets) {
- var that = this;
- // Ensure the callbacks will be executed after the snippets have been redrawn:
- setTimeout(function() {
- that.fireCallbacks(payload);
- }, 0);
- }
- }
- }, {
- snippets: [],
- /**
- * Adds a watch for requested snippet.
- * @param {string} name Snippet name
- * @param {function} callback Callback function
- */
- addWatch: function(name, callback) {
- name = 'snippet--' + name;
- if (this.snippets[name] == undefined) {
- this.snippets[name] = [];
- }
- this.snippets[name].push(callback);
- },
- /**
- * Fires all associated callbacks.
- */
- fireCallbacks: function(payload) {
- for (var s in payload.snippets) {
- // If the snippet is on the list:
- if (this.snippets[s] != undefined) {
- for (var f in this.snippets[s]) {
- var fnc = this.snippets[s][f];
- fnc(payload);
- }
- }
- }
- },
- /**
- * Updates snippets using snippets extension and
- * given payload.
- */
- updateSnippets: function(payload) {
- if (payload.snippets) {
- for (var i in payload.snippets) {
- $.nette.ext('snippets').updateSnippet(i, payload.snippets[i]);
- }
- }
- }
- });
- /**
- * Extra post and get extension
- *
- * This extension adds extra methods replacing $.post by $.nette.post and
- * $.get by $.nette.get which (beside default capabilities) also call event
- * handlers of other Nette extensions (automatic snippet dispatching after
- * ajax call etc).
- */
- $.nette.ext('extrapost', {
- /**
- * This method is called after extension addition:
- */
- init: function() {
- // $.post complementary method:
- $.nette.post = function(url, data, success, dataType) {
- $.fn.netteAjax(null, {
- url: url,
- data: data,
- success: success,
- dataType: dataType,
- type: 'post'
- });
- }
- // $.get complementary method:
- $.nette.get = function(url, data, success, dataType) {
- $.fn.netteAjax(null, {
- url: url,
- data: data,
- success: success,
- dataType: dataType,
- type: 'get'
- });
- }
- }
- });
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement