This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from plone/patternslib-merge
Patternslib merge: Removed the Mockup registry in favor of Patternslib's
- Loading branch information
Showing
11 changed files
with
272 additions
and
270 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
define([ | ||
'jquery' | ||
], function($) { | ||
'use strict'; | ||
|
||
var parser = { | ||
getOptions: function getOptions($el, patternName, options) { | ||
/* This is the Mockup parser. It parses a DOM element for pattern | ||
* configuration options. | ||
*/ | ||
options = options || {}; | ||
// get options from parent element first, stop if element tag name is 'body' | ||
if ($el.length !== 0 && !$.nodeName($el[0], 'body')) { | ||
options = getOptions($el.parent(), patternName, options); | ||
} | ||
// collect all options from element | ||
var elOptions = {}; | ||
if ($el.length !== 0) { | ||
elOptions = $el.data('pat-' + patternName); | ||
if (elOptions) { | ||
// parse options if string | ||
if (typeof(elOptions) === 'string') { | ||
var tmpOptions = {}; | ||
$.each(elOptions.split(';'), function(i, item) { | ||
item = item.split(':'); | ||
item.reverse(); | ||
var key = item.pop(); | ||
key = key.replace(/^\s+|\s+$/g, ''); // trim | ||
item.reverse(); | ||
var value = item.join(':'); | ||
value = value.replace(/^\s+|\s+$/g, ''); // trim | ||
tmpOptions[key] = value; | ||
}); | ||
elOptions = tmpOptions; | ||
} | ||
} | ||
} | ||
return $.extend(true, {}, options, elOptions); | ||
} | ||
}; | ||
return parser; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,102 @@ | ||
/* Base Pattern | ||
*/ | ||
|
||
|
||
define([ | ||
'jquery', | ||
'mockup-registry' | ||
], function($, Registry) { | ||
'pat-registry', | ||
'mockup-parser', | ||
"pat-logger" | ||
], function($, Registry, mockupParser, logger) { | ||
'use strict'; | ||
var log = logger.getLogger("Mockup Base"); | ||
|
||
var initMockup = function initMockup($el, options, trigger) { | ||
var name = this.prototype.name; | ||
var log = logger.getLogger("pat." + name); | ||
var pattern = $el.data('pattern-' + name); | ||
if (pattern === undefined && Registry.patterns[name]) { | ||
try { | ||
pattern = new Registry.patterns[name]($el, mockupParser.getOptions($el, name, options)); | ||
} catch (e) { | ||
log.error('Failed while initializing "' + name + '" pattern.'); | ||
} | ||
$el.data('pattern-' + name, pattern); | ||
} | ||
return pattern; | ||
}; | ||
|
||
// Base Pattern | ||
var Base = function($el, options) { | ||
this.$el = $el; | ||
this.options = $.extend(true, {}, this.defaults || {}, options || {}); | ||
this.init(); | ||
this.trigger('init'); | ||
this.emit('init'); | ||
}; | ||
|
||
Base.prototype = { | ||
constructor: Base, | ||
on: function(eventName, eventCallback) { | ||
this.$el.on(eventName + '.' + this.name + '.patterns', eventCallback); | ||
}, | ||
trigger: function(eventName, args) { | ||
emit: function(eventName, args) { | ||
// args should be a list | ||
if (args === undefined) { | ||
args = []; | ||
} | ||
this.$el.trigger(eventName + '.' + this.name + '.patterns', args); | ||
} | ||
}; | ||
Base.extend = function(NewPattern) { | ||
var Base = this, Constructor; | ||
|
||
if (NewPattern && NewPattern.hasOwnProperty('constructor')) { | ||
Constructor = NewPattern.constructor; | ||
Base.extend = function(patternProps) { | ||
/* Helper function to correctly set up the prototype chain for new patterns. | ||
*/ | ||
var parent = this; | ||
var child; | ||
|
||
// Check that the required configuration properties are given. | ||
if (!patternProps) { | ||
throw new Error("Pattern configuration properties required when calling Base.extend"); | ||
} | ||
|
||
// The constructor function for the new subclass is either defined by you | ||
// (the "constructor" property in your `extend` definition), or defaulted | ||
// by us to simply call the parent's constructor. | ||
if (patternProps.hasOwnProperty('constructor')) { | ||
child = patternProps.constructor; | ||
} else { | ||
Constructor = function() { Base.apply(this, arguments); }; // TODO: arguments from where | ||
child = function() { parent.apply(this, arguments); }; | ||
} | ||
|
||
var Surrogate = function() { this.constructor = Constructor; }; | ||
Surrogate.prototype = Base.prototype; | ||
Constructor.prototype = new Surrogate(); | ||
Constructor.extend = Base.extend; | ||
// Allow patterns to be extended indefinitely | ||
child.extend = Base.extend; | ||
|
||
$.extend(true, Constructor.prototype, NewPattern); | ||
// Static properties required by the Patternslib registry | ||
child.init = initMockup; | ||
child.jquery_plugin = true; | ||
child.trigger = patternProps.trigger; | ||
|
||
Constructor.__super__ = Base.prototype; // TODO: needed? | ||
// Set the prototype chain to inherit from `parent`, without calling | ||
// `parent`'s constructor function. | ||
var Surrogate = function() { this.constructor = child; }; | ||
Surrogate.prototype = parent.prototype; | ||
child.prototype = new Surrogate(); | ||
|
||
Registry.register(Constructor); | ||
// Add pattern's configuration properties (instance properties) to the subclass, | ||
$.extend(true, child.prototype, patternProps); | ||
|
||
return Constructor; | ||
}; | ||
// Set a convenience property in case the parent's prototype is needed | ||
// later. | ||
child.__super__ = parent.prototype; | ||
|
||
// Register the pattern in the Patternslib registry. | ||
if (!patternProps.name) { | ||
log.warn("This mockup pattern without a name attribute will not be registered!"); | ||
} else if (!patternProps.trigger) { | ||
log.warn("The mockup pattern '"+patternProps.name+"' does not have a trigger attribute, it will not be registered."); | ||
} else { | ||
Registry.register(child, patternProps.name); | ||
} | ||
return child; | ||
}; | ||
return Base; | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Tests for the Mockup parser | ||
define([ | ||
'expect', | ||
'sinon', | ||
'jquery', | ||
'mockup-parser', | ||
], function(expect, sinon, $, parser) { | ||
'use strict'; | ||
window.mocha.setup('bdd'); | ||
describe('The Mockup parser', function () { | ||
it("can read pattern configuraion options from the DOM", function() { | ||
var $el = $('' + | ||
'<div data-pat-example="{"name1": "value1",' + | ||
' "name2": "value2"}">' + | ||
' <div class="pat-example"' + | ||
' data-pat-example="name2: something;' + | ||
' some-thing-name4: value4;' + | ||
' some-stuff: value5"/>' + | ||
'</div>'); | ||
|
||
var options = parser.getOptions( | ||
$('.pat-example', $el), | ||
'example', | ||
{ name3: 'value3'} | ||
); | ||
expect(options.name1).to.equal('value1'); | ||
expect(options.name2).to.equal('something'); | ||
expect(options.name3).to.equal('value3'); | ||
expect(options['some-thing-name4']).to.equal('value4'); | ||
expect(options['some-stuff']).to.equal('value5'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.