Create a gist now

Instantly share code, notes, and snippets.

Publish Hooks class for xJSFL
/**
* Publish Hook class
*
* Features:
*
* - Runs pre and post publish hooks (any file on your hard drive, even other JSFL scripts)
* - Has a simple XUL UI to browse for files
* - Can load and save different profiles, say one for games, one for web
* - Stores setting in an XML config file, but could just as easily store those settings per document using document data
* - Can be set as Active or Inactive should you decide you need to disable it
*
* See the blog post here: http://www.xjsfl.com/blog/publish-hooks-library-for-flash
*
* @param {String} name An optional name to give the publish hook
*/
function PublishHooks(name)
{
// class properties
this.name = name ? String(name).replace(/\W/g, '-') : this.name;
this.config = new Config('publish hooks');
this.active = this.config.get(this.name + '.active');
// callback ids
this.preId = 'onPublish:' + this.name;
this.postId = 'onPublished:' + this.name;
// set callbacks if not already set
if( ! xjsfl.events.get(this.preId) )
{
xjsfl.events.add(DocumentEvent.PUBLISH, this.onPublish, this.preId, this);
xjsfl.events.add(DocumentEvent.PUBLISHED, this.onPublished, this.postId, this);
}
}
PublishHooks.prototype =
{
// --------------------------------------------------------------------------------
// properties
active :true,
config :null,
name :'default',
preId :'',
postId :'',
// --------------------------------------------------------------------------------
// methods
// handlers
onAccept:function (pre, post, active)
{
this.config.remove(this.name);
this.config.set(this.name + '.active', active);
this.config.set(this.name + '.pre', pre);
this.config.set(this.name + '.post', post);
this.active = active;
trace('Publish hooks updated for "' +this.name+ '".');
},
onPublish:function ()
{
this.run('pre');
},
onPublished:function ()
{
this.run('post');
},
// private methods
run:function(type)
{
if(this.active)
{
var uri = this.config.get(this.name + '.' + type);
var file = new File(uri);
if(file.exists)
{
trace('Running "' +this.name+ '" ' +type+ '-publish hook: "' + file.path + '"');
file.run();
}
}
},
// public methods
/**
* Show the UI
*/
edit:function()
{
var that = this;
function onAccept()
{
that.onAccept.apply(that, arguments);
}
XUL.create('file:Pre-publish hook,file:Post-publish hook,checkbox:Active,title:"' +this.name.toSentenceCase()+ '"Publish Hooks', onAccept, null, this.config.get(this.name));
return this;
},
/**
* Remove callbacks
*/
remove:function()
{
xjsfl.events.remove(DocumentEvent.PUBLISH, this.preId);
xjsfl.events.remove(DocumentEvent.PUBLISHED, this.postId);
}
}
xjsfl.classes.register('PublishHooks', PublishHooks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment