class GrowlerGrailsPlugin { // the plugin version def version = "0.2" // the version or versions of Grails the plugin is designed for def grailsVersion = "1.3.7 > *" // the other plugins this plugin depends on def dependsOn = [ "jquery": "1.3 > *"] // resources that are excluded from plugin packaging def pluginExcludes = [ "grails-app/views/error.gsp", "grails-app/views/idnex.gsp" ] def author = "Matthew Taylor" def authorEmail = "rhyolight@gmail.com" def title = "Growler" def description = '''\\ Uses jquery, and the jGrowl jquery plugin to allow Growl-like notifications from remote function calls. ''' // URL to the plugin's documentation def documentation = "http://grails.org/Growler+Plugin" def doWithSpring = { // TODO Implement runtime spring config (optional) } def doWithApplicationContext = { applicationContext -> replaceRemoteFunction(applicationContext) } def onChange = { event -> // TODO Implement code that is executed when any artefact that this plugin is // watching is modified and reloaded. The event contains: event.source, // event.application, event.manager, event.ctx, and event.plugin. } def onConfigChange = { event -> // TODO Implement code that is executed when the project configuration changes. // The event is the same as for 'onChange'. } /** * Replaces the JavascriptTagLib.remoteFunction with one that will handle growl attributes, or if there are none, * defer to the original remoteFunction method. */ private replaceRemoteFunction(applicationContext) { def jsTagLib = applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib') // cache the old remoteFunction to use if we don't need to Growl def oldRemoteFunction = jsTagLib.remoteFunction // the new growling remoteFunction def growlingRemoteFunction = { attrs -> // use growl if specified if (attrs.growl && attrs.growl == 'true') { // jquery ajax call that will use the response within a Growler notification out << """\$.ajax({ type: 'POST', url: '${createLink(attrs)}', success: function(msg){ \$.jGrowl( msg ); }, failure: function(msg) { \$.jGrowl( 'Something FAILED!: ' + msg ); } });""" } // otherwise, do it the normal way else { oldRemoteFunction(attrs) } } growlingRemoteFunction.delegate = jsTagLib growlingRemoteFunction.resolveStrategy = Closure.DELEGATE_FIRST // provide the new remoteFunction within the metaClass of JavascriptTagLib jsTagLib.metaClass.getRemoteFunction = { return growlingRemoteFunction } } }