1
0
Fork 0
mirror of https://github.com/kbparagua/paloma synced 2023-03-27 23:21:17 -04:00
This commit is contained in:
kbparagua 2013-08-27 16:45:53 +08:00
parent 23052f0e82
commit fae6760a33
29 changed files with 21 additions and 859 deletions

View file

@ -11,13 +11,13 @@
}
// Remove any callback details if any
$('.paloma-callback-details[data-id!=' + id + ']').remove();
$('.paloma-callback-details[data-id!=' + <%= id %> + ']').remove();
var run = function(){
var callbacks = <%= callbacks.to_json.html_safe %>;
for (var i = 0, len = callbacks.length; i < len; i++){
Paloma.execute(callback[i]);
Paloma.execute(callbacks[i]);
}
};

View file

@ -130,9 +130,9 @@ module Paloma
end
def parse_action action
def parse_action action = nil
action ||= self.action_name
action.split('_').map(&:titleize).join
action.camelize(:lower)
end
end

View file

@ -1,130 +0,0 @@
// For testing only
window.filtersExecuted = window.filtersExecuted || {before : [], after : []};
(function(){
var filter = new Paloma.FilterScope('/');
var _x = Paloma.variableContainer;
// To-Be-Skipped Before Filters
filter.as('Standard Skip Before Filter').before_all().perform(function(params){
filtersExecuted.before.push('Standard Skip Before Filter');
});
filter.as('Only Skip Before Filter').before_all().perform(function(params){
filtersExecuted.before.push('Only Skip Before Filter');
});
filter.as('Except Skip Before Filter').before_all().perform(function(params){
filtersExecuted.before.push('Except Skip Before Filter');
});
// To-Be-Skipped After Filters
filter.as('Standard Skip After Filter').after_all().perform(function(params){
filtersExecuted.after.push('Standard Skip After Filter');
});
filter.as('Only Skip After Filter').after_all().perform(function(params){
filtersExecuted.after.push('Only Skip After Filter');
});
filter.as('Except Skip After Filter').after_all().perform(function(params){
filtersExecuted.after.push('Except Skip After Filter');
});
// To-Be-Skipped Around Filters
filter.as('Standard Skip Around Filter').around_all().perform(function(params){
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Standard Skip Around Filter');
});
filter.as('Only Skip Around Filter').around_all().perform(function(params){
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Only Skip Around Filter');
});
filter.as('Except Skip Around Filter').around_all().perform(function(params){
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Except Skip Around Filter');
});
// Before
filter.as('Application Standard Before').
before('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.before.push('Application Standard Before');
});
filter.as('Application Before All').
before_all().perform(function(params)
{
filtersExecuted.before.push('Application Before All');
});
filter.as('Application Except Before').
except_before('basic_action').
perform(function(params)
{
filtersExecuted.before.push('Application Except Before');
});
// After
filter.as('Application Standard After').
after('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.after.push('Application Standard After');
});
filter.as('Application After All').
after_all().perform(function(params)
{
filtersExecuted.after.push('Application After All');
});
filter.as('Application Except After').
except_after('basic_action').
perform(function(params)
{
filtersExecuted.after.push('Application Except After');
});
// Around
filter.as('Application Standard Around').
around('basic_action', 'another_basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Application Standard Around');
});
filter.as('Application Around All').
around_all().perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Application Around All');
});
filter.as('Application Except Around').
except_around('basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Application Except Around');
});
})();

View file

@ -1,17 +0,0 @@
(function(){
Paloma.locals['/'] = {};
locals = Paloma.locals['/'];
// Locals starts here
locals.applicationHelperMethod = function(){
return "I'm from application locals!"
};
locals.toBeOverriden = function(){
return "Override me!"
};
// Locals ends here
})();

View file

@ -1,96 +0,0 @@
// For testing only
window.filtersExecuted = window.filtersExecuted || {before : [], after : []};
(function(){
var filter = new Paloma.FilterScope('bar');
var _x = Paloma.variableContainer;
filter.skip_before_filter('Standard Skip Before Filter');
filter.skip_before_filter('Only Skip Before Filter').only('basic_action');
filter.skip_before_filter('Except Skip Before Filter').except('yet_another_basic_action');
filter.skip_after_filter('Standard Skip After Filter');
filter.skip_after_filter('Only Skip After Filter').only('basic_action');
filter.skip_after_filter('Except Skip After Filter').except('yet_another_basic_action');
filter.skip_around_filter('Standard Skip Around Filter');
filter.skip_around_filter('Only Skip Around Filter').only('basic_action');
filter.skip_around_filter('Except Skip Around Filter').except('yet_another_basic_action');
// Before
filter.as('Standard Before').
before('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.before.push('Standard Before');
});
filter.as('Before All').
before_all().perform(function(params)
{
filtersExecuted.before.push('Before All');
});
filter.as('Except Before').
except_before('basic_action').
perform(function(params)
{
filtersExecuted.before.push('Except Before');
});
// After
filter.as('Standard After').
after('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.after.push('Standard After');
});
filter.as('After All').
after_all().perform(function(params)
{
filtersExecuted.after.push('After All');
});
filter.as('Except After').
except_after('basic_action').
perform(function(params)
{
filtersExecuted.after.push('Except After');
});
// Around
filter.as('Standard Around').
around('basic_action', 'another_basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Standard Around');
});
filter.as('Around All').
around_all().perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Around All');
});
filter.as('Except Around').
except_around('basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Except Around');
});
})();

View file

@ -1 +0,0 @@
Paloma.callbacks['bar'] = {};

View file

@ -1,3 +0,0 @@
//= require ./_locals.js
//= require ./_filters.js
//= require_tree .

View file

@ -1,5 +0,0 @@
Paloma.callbacks['bar']['another_basic_action'] = function(params)
{
window.callback = "['bar']['another_basic_action']";
window.params = params;
};

View file

@ -1,5 +0,0 @@
Paloma.callbacks['bar']['basic_action'] = function(params)
{
window.callback = "['bar']['basic_action']";
window.params = params;
};

View file

@ -1,4 +0,0 @@
Paloma.callbacks['bar']['different_params'] = function(params)
{
window.params = params;
};

View file

@ -1,5 +0,0 @@
Paloma.callbacks['bar']['yet_another_basic_action'] = function(params)
{
window.callback = "['bar']['yet_another_basic_action']";
window.params = params;
};

View file

@ -2,18 +2,8 @@
var self = Paloma.Resource.create('Foo');
self.prototype.basicAction = function(){
window.callback = "['foo']['basic_action']";
window.params = params;
window.helperMethodValue = _l.helperMethod();
window.helperVariableValue = _l.helperVariable;
window.overriden = _l.toBeOverriden();
_x.xVisibility.push('Foo');
window.locals = _l;
self.prototype.basicAction = function(params){
console.log(params);
};
})();

View file

@ -1,24 +0,0 @@
(function(){
var filter = new Paloma.FilterScope('foo');
var _x = Paloma.variableContainer;
filter.as('Before Foo').
before_all().perform(function(params){
_x.xVisibility = ['Before Foo'];
});
filter.as('After Foo').
after_all().perform(function(params){
_x.xVisibility.push('After Foo');
});
filter.as('Around Foo').
around_all().perform(function(params){
_x.xVisibility.push('Around Foo');
window.xVisibilityFinal = _x.xVisibility;
});
})();

View file

@ -1,21 +0,0 @@
(function(){
Paloma.callbacks['foo'] = {};
Paloma.locals['foo'] = {};
locals = Paloma.locals['foo'];
// Locals starts here
locals.helperMethod = function(){
return 100;
};
locals.helperVariable = 99;
locals.toBeOverriden = function(){
return "Override!"
};
// Locals ends here
Paloma.inheritLocals({from : '/', to : 'foo'});
})();

View file

@ -1,3 +0,0 @@
//= require ./_locals.js
//= require ./_filters.js
//= require_tree .

View file

@ -1,21 +0,0 @@
(function(){
var _x = Paloma.variableContainer,
_L= Paloma.locals,
_l = _L['foo'];
Paloma.callbacks['foo']['basic_action'] = function(params)
{
window.callback = "['foo']['basic_action']";
window.params = params;
window.helperMethodValue = _l.helperMethod();
window.helperVariableValue = _l.helperVariable;
window.overriden = _l.toBeOverriden();
_x.xVisibility.push('Foo');
window.locals = _l;
};
})();

View file

@ -1,6 +0,0 @@
Paloma.callbacks['foo']['skip_callback'] = function(params)
{
// Must not reach this code.
window.callback = "foo/skip_callback";
window.params = params;
};

View file

@ -1,6 +1,2 @@
//= require paloma_core.js
//= require ./_filters.js
//= require ./_locals.js
//= require ./foo/_manifest.js
//= require ./bar/_manifest.js
//= require ./sample_namespace/_manifest.js
//= require paloma.js
//= require_tree .

View file

@ -1,83 +0,0 @@
// For testing only
window.filtersExecuted = window.filtersExecuted || {before : [], after : []};
(function(){
var filter = new Paloma.FilterScope('sample_namespace');
var _x = Paloma.variableContainer;
// Before
filter.as('Namespaced Standard Before').
before('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.before.push('Namespaced Standard Before');
});
filter.as('Namespaced Before All').
before_all().perform(function(params)
{
filtersExecuted.before.push('Namespaced Before All');
});
filter.as('Namespaced Except Before').
except_before('basic_action').
perform(function(params)
{
filtersExecuted.before.push('Namespaced Except Before');
});
// After
filter.as('Namespaced Standard After').
after('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.after.push('Namespaced Standard After');
});
filter.as('Namespaced After All').
after_all().perform(function(params)
{
filtersExecuted.after.push('Namespaced After All');
});
filter.as('Namespaced Except After').
except_after('basic_action').
perform(function(params)
{
filtersExecuted.after.push('Namespaced Except After');
});
// Around
filter.as('Namespaced Standard Around').
around('basic_action', 'another_basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Namespaced Standard Around');
});
filter.as('Namespaced Around All').
around_all().perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Namespaced Around All');
});
filter.as('Namespaced Except Around').
except_around('basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Namespaced Except Around');
});
})();

View file

@ -1,2 +0,0 @@
//= require ./_filters.js
//= require ./baz/_manifest.js

View file

@ -1,84 +0,0 @@
// For testing only
window.filtersExecuted = window.filtersExecuted || {before : [], after : []};
(function(){
var filter = new Paloma.FilterScope('sample_namespace/baz');
var _x = Paloma.variableContainer;
// Before
filter.as('Standard Before').
before('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.before.push('Standard Before');
});
filter.as('Before All').
before_all().perform(function(params)
{
filtersExecuted.before.push('Before All');
});
filter.as('Except Before').
except_before('basic_action').
perform(function(params)
{
filtersExecuted.before.push('Except Before');
});
// After
filter.as('Standard After').
after('basic_action', 'another_basic_action').
perform(function(params)
{
filtersExecuted.after.push('Standard After');
});
filter.as('After All').
after_all().perform(function(params)
{
filtersExecuted.after.push('After All');
});
filter.as('Except After').
except_after('basic_action').
perform(function(params)
{
filtersExecuted.after.push('Except After');
});
// Around
filter.as('Standard Around').
around('basic_action', 'another_basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Standard Around');
});
filter.as('Around All').
around_all().perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Around All');
});
filter.as('Except Around').
except_around('basic_action').
perform(function(params)
{
var execution = window.callback ? 'after' : 'before';
filtersExecuted[execution].push('Except Around');
});
})();

View file

@ -1 +0,0 @@
Paloma.callbacks['sample_namespace/baz'] = {}

View file

@ -1,2 +0,0 @@
//= require ./_locals.js
//= require_tree .

View file

@ -1,5 +0,0 @@
Paloma.callbacks['sample_namespace/baz']['another_basic_action'] = function(params)
{
window.callback = "['sample_namespace/baz']['another_basic_action']";
window.params = params;
};

View file

@ -1,5 +0,0 @@
Paloma.callbacks['sample_namespace/baz']['basic_action'] = function(params)
{
window.callback = "['sample_namespace/baz']['basic_action']";
window.params = params;
};

View file

@ -1,5 +0,0 @@
Paloma.callbacks['sample_namespace/baz']['yet_another_basic_action'] = function(params)
{
window.callback = "['sample_namespace/baz']['yet_another_basic_action']";
window.params = params;
};

View file

@ -8,13 +8,13 @@ window.Paloma = window.Paloma || {};
P.execute = function(callback){
var resource = P.Resources[callback.resource];
var resource = P.Resources[ callback['resource'] ];
if (resource == undefined){ return false; }
var action = resource[action];
if (action == undefined){ return false; }
var instance = new resource(),
action = callback['action'];
action(callback.params);
if (instance[action] != null){ instance[action](callback['params']); }
};
@ -26,20 +26,18 @@ window.Paloma = window.Paloma || {};
options = defaultOptions(options);
var path = name.split('.'),
resource = name.pop(),
namespace = name.pop();
resource = path.pop(),
namespace = path.pop();
var constructor = function(){};
var Resource = function(){};
if (namespace != null){
P.Resources[namespace] = P.Resource[namespace] || {};
P.Resources[namespace][resource] = constructor;
return P.Resources[namespace][resource] = Resource;
}
else {
P.Resources[resource] = constructor;
return P.Resources[resource] = Resource;
}
return constructor;
};
@ -51,6 +49,8 @@ window.Paloma = window.Paloma || {};
var defaultOptions = function(options){
options = options || {};
var values = {};
values['extend'] = options['extend'] != null ? options['extend'] : null;

View file

@ -1,296 +0,0 @@
(function(){
window.Paloma = {
callbacks : {},
filterScopes : {},
locals : {},
variableContainer : {}
};
var FILTER_TYPES = {},
FILTER_TYPE_NAMES = ['BEFORE', 'AFTER', 'AROUND'],
INCLUSION_TYPES = {},
INCLUSION_TYPE_NAMES = ['ALL', 'ONLY', 'EXCEPT'];
FILTER_TYPES.BEFORE = 0;
FILTER_TYPES.AFTER = 1;
FILTER_TYPES.AROUND = 2;
INCLUSION_TYPES.ALL = 3;
INCLUSION_TYPES.ONLY = 4;
INCLUSION_TYPES.EXCEPT = 5;
Paloma.inheritLocals = function(options){
var from = Paloma.locals[options['from']],
to = Paloma.locals[options['to']];
for (var local in from){
// Overriding is allowed.
if ( to.hasOwnProperty(local) ){ continue; }
to[local] = from[local];
}
};
Paloma.FilterScope = function(name){
this.name = name;
this.filters = {};
this.skippers = {};
for (var i = 0, n = FILTER_TYPE_NAMES.length; i < n; i++){
var type = FILTER_TYPE_NAMES[i].toUpperCase();
this.filters[ FILTER_TYPES[type] ] = [];
this.skippers[ FILTER_TYPES[type] ] = [];
}
Paloma.filterScopes[name] = this;
};
// Creates a new Filter instance registered under this FilterScope.
Paloma.FilterScope.prototype.as = function(filterName){
return (new Paloma.Filter(this, filterName));
};
// skip_*_filter/s methods
(function(){
for (var i = 0, n = FILTER_TYPE_NAMES.length; i < n; i++){
var type = FILTER_TYPE_NAMES[i],
singular = 'skip_' + type.toLowerCase() + '_filter',
plural = singular + 's',
method = function(skipperType){
return function(){
return (new Paloma.Skipper(this, FILTER_TYPES[skipperType], arguments));
};
};
Paloma.FilterScope.prototype[singular] = new method(type);
Paloma.FilterScope.prototype[plural] = Paloma.FilterScope.prototype[singular];
}
})();
// Skipper class
Paloma.Skipper = function(scope, type, filters){
this.scope = scope;
this.type = type;
this.filters = Array.prototype.slice.call(filters);
this.inclusionType = INCLUSION_TYPES.ALL;
this.actions = [];
// Register this skipper on its scope.
this.scope.skippers[this.type].push(this);
};
Paloma.Skipper.prototype.skip = function(filter, action){
if (this.filters.indexOf(filter.name) == -1){ return false; }
var actionIsListed = this.actions.indexOf(action) != -1,
isAllActions = this.inclusionType == INCLUSION_TYPES.ALL,
isQualified = this.inclusionType == INCLUSION_TYPES.ONLY && actionIsListed,
isNotExcepted = this.inclusionType == INCLUSION_TYPES.EXCEPT && !actionIsListed;
return (isAllActions || isQualified || isNotExcepted);
};
Paloma.Skipper.prototype.only = function(){
this.inclusionType = INCLUSION_TYPES.ONLY;
this.actions = Array.prototype.slice.call(arguments);
};
Paloma.Skipper.prototype.except = function(){
this.inclusionType = INCLUSION_TYPES.EXCEPT;
this.actions = Array.prototype.slice.call(arguments);
};
// Filter class
Paloma.Filter = function(scope, name){
this.scope = scope;
this.name = name;
this.type = undefined;
this.inclusionType = INCLUSION_TYPES.ONLY;
this.actions = [];
this.method = undefined;
};
// Create Methods:
// - before, after, around, *_all, except_*
(function(){
var Basic = function(type){
return function(){
return this.setProperties(type, INCLUSION_TYPES.ONLY, arguments);
};
};
var All = function(type){
return function(){
return this.setProperties(type, INCLUSION_TYPES.ALL, []);
};
};
var Except = function(type){
return function(){
return this.setProperties(type, INCLUSION_TYPES.EXCEPT, arguments);
};
};
for (var i = 0, n = FILTER_TYPE_NAMES.length; i < n; i++){
var name = FILTER_TYPE_NAMES[i].toLowerCase(),
type = FILTER_TYPES[name.toUpperCase()];
Paloma.Filter.prototype[name] = new Basic(type);
Paloma.Filter.prototype[name + '_all'] = new All(type);
Paloma.Filter.prototype['except_' + name] = new Except(type);
}
})();
// End of creating methods.
// This will be the last method to be invoked when declaring a filter.
// This will set what method/function will be executed when the filter is called.
Paloma.Filter.prototype.perform = function(method){
this.method = method;
// This is the only time the filter is registered to its owner scope.
this.scope.filters[this.type].push(this);
return this;
};
Paloma.Filter.prototype.isApplicable = function(action){
var actionIsListed = this.actions.indexOf(action) != -1,
isAllActions = this.inclusionType == INCLUSION_TYPES.ALL,
isQualified = this.inclusionType == INCLUSION_TYPES.ONLY && actionIsListed,
isNotExcepted = this.inclusionType == INCLUSION_TYPES.EXCEPT && !actionIsListed;
return (isAllActions || isQualified || isNotExcepted);
};
Paloma.Filter.prototype.setProperties = function(type, inclusion, actions){
this.type = type;
this.inclusionType = inclusion;
this.actions = Array.prototype.slice.call(actions);
return this;
};
// Execute callback that corresponds to the controller and action passed.
Paloma.execute = function(controller, action, params){
var callbackFound = true;
var callback = Paloma.callbacks[controller];
callbackFound = callback != undefined;
if (callbackFound){
callback = callback[action];
callbackFound = callback != undefined;
}
// Parse parameters
params = params || {};
// Request details
var requestControllerPath = params['controller_path'].split('/');
params['controller'] = requestControllerPath.pop();
params['namespace'] = requestControllerPath.join('/');
// Callback details
var callbackControllerPath = controller.split('/');
params['callback_controller'] = callbackControllerPath.pop();
params['callback_namespace'] = callbackControllerPath.join('/');
params['callback_controller_path'] = controller;
params['callback_action'] = action;
var beforeFilters = getOrderedFilters(
FILTER_TYPES.BEFORE,
params['callback_namespace'],
controller,
action);
var afterFilters = getOrderedFilters(
FILTER_TYPES.AFTER,
params['callback_namespace'],
controller,
action);
// Start filter and callback executions
performFilters(beforeFilters, params);
if (callbackFound){ callback(params); }
performFilters(afterFilters, params);
// variableContainer is used to share variable between filters and callbacks.
// It will be cleared after it is used.
Paloma.variableContainer = [];
};
var getOrderedFilters = function(beforeOrAfter, namespace, controller, action){
var filters = [],
applicableFilters = [],
skippers = [],
scopes = [
Paloma.filterScopes['/'],
Paloma.filterScopes[namespace],
Paloma.filterScopes[controller]];
for (var i = 0, n = scopes.length; i < n; i++){
var scope = scopes[i];
if (scope == undefined){ continue; }
var mainFilters = scope.filters[beforeOrAfter],
aroundFilters = scope.filters[FILTER_TYPES.AROUND];
// Around Filters have lower precedence than before or after filters.
if (mainFilters != undefined){ filters = filters.concat(mainFilters); }
if (aroundFilters != undefined){ filters = filters.concat(aroundFilters); }
skippers = skippers.concat(
scope.skippers[beforeOrAfter],
scope.skippers[FILTER_TYPES.AROUND]);
}
// Select only applicable filters for the passed action.
for (var i = 0, n = filters.length; i < n; i++){
var filter = filters[i],
isApplicable = filter.isApplicable(action),
isNotSkipped = true;
for (var k = 0, len = skippers.length; k < len; k++){
if ( skippers[k].skip(filter, action) ){ isNotSkipped = false; break;}
}
if (isApplicable && isNotSkipped){ applicableFilters.push(filter); }
}
return applicableFilters;
};
var performFilters = function(filters, params){
for (var i = 0, n = filters.length; i < n; i++){
filters[i].method(params);
}
};
})();

View file

@ -1,4 +1,4 @@
(function(){
/*(function(){
window.Paloma = {
callbacks : {},
filterScopes : {},
@ -293,4 +293,4 @@ var performFilters = function(filters, params){
};
})();
})();*/