Last active
September 19, 2017 13:21
-
-
Save mrded/7134428 to your computer and use it in GitHub Desktop.
Drupal: Example of reloading ctools modals.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ctools_include('ajax'); | |
ctools_include('modal'); | |
ctools_modal_add_js(); | |
drupal_add_library('module', 'fancybox.modal'); // Include js here. | |
$path = 'some_path/nojs'; | |
$content = ctools_modal_text_button($text, $path, '', $class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
// Make sure our objects are defined. | |
Drupal.CTools = Drupal.CTools || {}; | |
Drupal.CTools.Modal = Drupal.CTools.Modal || {}; | |
/** | |
* Display the modal | |
* | |
* @todo -- document the settings. | |
*/ | |
Drupal.CTools.Modal.show = function(choice) { | |
$.fancybox.showActivity(); | |
}; | |
/** | |
* Hide the modal | |
*/ | |
Drupal.CTools.Modal.dismiss = function() { | |
Drupal.CTools.Modal.unmodalContent(); | |
$.fancybox.close(); | |
}; | |
/** | |
* Handler to prepare the modal for the response | |
*/ | |
Drupal.CTools.Modal.clickAjaxLink = function() { | |
Drupal.CTools.Modal.show(); | |
return false; | |
}; | |
// The following are implementations of AJAX responder commands. | |
/** | |
* AJAX responder command to place HTML within the modal. | |
*/ | |
Drupal.CTools.Modal.modal_display = function(ajax, response, status) { | |
// Detach behaviors from previous content (if any). | |
Drupal.CTools.Modal.unmodalContent(); | |
// Hide progress indicator. | |
$.fancybox.hideActivity(); | |
var s = { | |
padding: 0, | |
hideOnOverlayClick: false, | |
type: 'html', | |
content: response.output, | |
title: response.title, | |
titleShow: false | |
}; | |
s.onComplete = function() { | |
var content = $('#fancybox-content'); | |
content.wrapInner('<div id="modulename-fancybox-modal" />'); | |
Drupal.attachBehaviors(content); | |
}; | |
$.fancybox(s); | |
} | |
/** | |
* AJAX responder command to dismiss the modal. | |
*/ | |
Drupal.CTools.Modal.modal_dismiss = function(command) { | |
Drupal.CTools.Modal.dismiss(); | |
$('link.ctools-temporary-css').remove(); | |
} | |
/** | |
* Display loading | |
*/ | |
//Drupal.CTools.AJAX.commands.modal_loading = function(command) { | |
Drupal.CTools.Modal.modal_loading = function(command) { | |
$.fancybox.showActivity(); | |
} | |
/** | |
* unmodalContent | |
* @param content (The jQuery object to remove) | |
* @param animation (fadeOut, slideUp, show) | |
* @param speed (valid animation speeds slow, medium, fast or # in ms) | |
*/ | |
Drupal.CTools.Modal.unmodalContent = function(content, animation, speed) { | |
var modalContent = $('#fancybox-content'); | |
if (modalContent.size()) { | |
Drupal.detachBehaviors(modalContent); | |
//modalContent.empty(); | |
} | |
}; | |
/** | |
* Bind links that will open modals to the appropriate function. | |
*/ | |
Drupal.behaviors.ZZCToolsModal = { | |
attach: function(context) { | |
// Bind links | |
// Note that doing so in this order means that the two classes can be | |
// used together safely. | |
/* | |
* @todo remimplement the warm caching feature | |
$('a.ctools-use-modal-cache', context).once('ctools-use-modal', function() { | |
$(this).click(Drupal.CTools.Modal.clickAjaxCacheLink); | |
Drupal.CTools.AJAX.warmCache.apply(this); | |
}); | |
*/ | |
$('area.ctools-use-modal, a.ctools-use-modal', context).once('ctools-use-modal', function() { | |
var $this = $(this); | |
$this.click(Drupal.CTools.Modal.clickAjaxLink); | |
// Create a drupal ajax object | |
var element_settings = {}; | |
if ($this.attr('href')) { | |
element_settings.url = $this.attr('href'); | |
element_settings.event = 'click'; | |
element_settings.progress = { type: 'none' }; | |
} | |
var base = $this.attr('href'); | |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); | |
}); | |
// Bind buttons | |
$('input.ctools-use-modal, button.ctools-use-modal', context).once('ctools-use-modal', function() { | |
var $this = $(this); | |
$this.click(Drupal.CTools.Modal.clickAjaxLink); | |
var button = this; | |
var element_settings = {}; | |
// AJAX submits specified in this manner automatically submit to the | |
// normal form action. | |
element_settings.url = Drupal.CTools.Modal.findURL(this); | |
element_settings.event = 'click'; | |
var base = $this.attr('id'); | |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); | |
// Make sure changes to settings are reflected in the URL. | |
$('.' + $(button).attr('id') + '-url').change(function() { | |
Drupal.ajax[base].options.url = Drupal.CTools.Modal.findURL(button); | |
}); | |
}); | |
// Bind our custom event to the form submit | |
$('#modulename-fancybox-modal form', context).once('ctools-use-modal', function() { | |
var $this = $(this); | |
var element_settings = {}; | |
element_settings.url = $this.attr('action'); | |
element_settings.event = 'submit'; | |
element_settings.progress = { 'type': 'throbber' } | |
var base = $this.attr('id'); | |
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); | |
Drupal.ajax[base].form = $this; | |
$('input[type=submit], button', this).click(function(event) { | |
Drupal.ajax[base].element = this; | |
this.form.clk = this; | |
// An empty event means we were triggered via .click() and | |
// in jquery 1.4 this won't trigger a submit. | |
if (event.bubbles == undefined) { | |
$(this.form).trigger('submit'); | |
return false; | |
} | |
}); | |
}); | |
// Bind a click handler to allow elements with the 'ctools-close-modal' | |
// class to close the modal. | |
$('.ctools-close-modal', context).once('ctools-close-modal') | |
.click(function() { | |
Drupal.CTools.Modal.dismiss(); | |
return false; | |
}); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment