Recently I had to perform some actions before opening and before closing a dialog in a website. I had multiple dialogs throughout the website, the thing in common among them, was that all dialogs used the awesome jQueryUI dialog plugin, so I could take advantage of that..
Understanding the innards of JavaScript allows us to do some cool things. Not saying they’re should be done, or that they are the right thing to do, just saying you can :)
So.. prototypes…
functionoverrideDialogBehaviors(){varoldOpen=$.ui.dialog.prototype.open;$.ui.dialog.prototype.open=function(){console.log('Do whatever you want before opening a dialog');oldOpen.call(this);}varoldClose=$.ui.dialog.prototype.close;$.ui.dialog.prototype.close=function(event){console.log('Do whatever you want before closing a dialog');oldClose.call(this,event);}}
Digging a bit into the jQueryUI source code, we can find the open and close dialog methods, they are responsible for showing and hiding the dialogs, respectively. Overriding their behavior, without losing any functionality is easy by simply setting them again, and calling the old method again, with all previous parameteres.
In order to be able to perform the interception successfully, we can use the call() method, more information about it can be found in Mozillas JavaScript Reference.
The idea is simple, store the old open and close methods in a variable, redefine the dialog methods, do whatever we want to and call the old method again, using the stored methods, with the same parameters and scope, keeping all functionality.
Every time after starting a new fresh install of Drupal in my Linux box I always have to set some permissions to the hierarchy structure to make it work properly.
As I always need to hunt the commands over again, I’ve decided to put everything in one place so I can easily remember.
# Include your user in www-data group
# This command needs to be run only once
# If your user already exists you may instead need use
# sudo usermod -a -G www-data brunops
useradd -G www-data brunops
# File User/Group owners
sudo chown-R brunops:www-data /path/to/folder --preserve-root# Drupal file permissions
# Execution perm for files
sudo find /path/to/folder -type f -print0 | xargs -0sudo chmod 644 --preserve-root# Execution perm for folders
sudo find /path/to/folder -type d -print0 | xargs -0sudo chmod 755 --preserve-root
Latest version of the code can be checked in my Gist.