decorators idea
This commit is contained in:
parent
76933017bf
commit
08af2fb629
138
playground/decorators/decorator_tests.js
Normal file
138
playground/decorators/decorator_tests.js
Normal file
@ -0,0 +1,138 @@
|
||||
|
||||
QUnit.test("decorator", function (assert) {
|
||||
|
||||
var A = clazz(Object, {
|
||||
hello: function () {
|
||||
return "A";
|
||||
}
|
||||
});
|
||||
|
||||
var Decorator = clazz(Object, {
|
||||
hello: function () {
|
||||
return "B" + this.decorated() + "B";
|
||||
}
|
||||
});
|
||||
|
||||
var a = new A();
|
||||
assert.strictEqual(a.hello(), "A");
|
||||
|
||||
|
||||
a.decorateWith(new Decorator());
|
||||
var value = a.hello();
|
||||
assert.strictEqual(value, "BAB");
|
||||
|
||||
});
|
||||
|
||||
QUnit.test("super", function (assert) {
|
||||
|
||||
var A = clazz(Object, {
|
||||
hello: function () {
|
||||
return "A";
|
||||
}
|
||||
});
|
||||
|
||||
var B = clazz(A, {
|
||||
hello: function () {
|
||||
return "B" + this.super() + "B";
|
||||
}
|
||||
});
|
||||
|
||||
var C = clazz(B, {
|
||||
hello: function () {
|
||||
return "C" + this.super() + "C";
|
||||
}
|
||||
});
|
||||
|
||||
var D = clazz(B, {
|
||||
hello: function () {
|
||||
return "D";
|
||||
}
|
||||
});
|
||||
|
||||
assert.strictEqual(new C().hello(), "CBABC");
|
||||
assert.strictEqual(new D().hello(), "D");
|
||||
|
||||
});
|
||||
|
||||
QUnit.test("super - constructors", function (assert) {
|
||||
|
||||
var A = clazz(Object, {
|
||||
construct: function () {
|
||||
this.message = "A";
|
||||
}
|
||||
});
|
||||
|
||||
var B = clazz(A, {
|
||||
construct: function () {
|
||||
this.super();
|
||||
this.message = "B" + this.message + "B";
|
||||
}
|
||||
});
|
||||
|
||||
var C = clazz(B, {
|
||||
construct: function () {
|
||||
this.super();
|
||||
this.message = "C" + this.message + "C";
|
||||
}
|
||||
});
|
||||
|
||||
var D = clazz(B, {
|
||||
construct: function () {
|
||||
this.message = "D";
|
||||
}
|
||||
});
|
||||
|
||||
assert.strictEqual(new C().message, "CBABC");
|
||||
assert.strictEqual(new D().message, "D");
|
||||
|
||||
});
|
||||
|
||||
|
||||
QUnit.test("inheritance", function (assert) {
|
||||
|
||||
var counter = 0;
|
||||
|
||||
var A = clazz(Object, {
|
||||
hello: function () {
|
||||
return "A";
|
||||
}
|
||||
});
|
||||
|
||||
var B = clazz(A, {
|
||||
|
||||
});
|
||||
|
||||
var a = new A(), b = new B();
|
||||
assert.ok(a.hello() === "A", "instance of A has hello()");
|
||||
assert.ok(b.hello() === "A", "instance of B has hello()");
|
||||
assert.ok(b instanceof B, "b is instance of B");
|
||||
assert.ok(b instanceof Object, "b is instance of Object");
|
||||
assert.ok(b instanceof A, "b is instance of A");
|
||||
});
|
||||
|
||||
|
||||
QUnit.test("initialization", function (assert) {
|
||||
|
||||
var counter = 0;
|
||||
|
||||
var A = clazz(Object, {
|
||||
construct: function () {
|
||||
counter++;
|
||||
}
|
||||
});
|
||||
|
||||
var B = clazz(A, {
|
||||
|
||||
});
|
||||
|
||||
assert.strictEqual(counter, 0);
|
||||
|
||||
var a = new A();
|
||||
|
||||
assert.strictEqual(counter, 1);
|
||||
|
||||
var b = new B();
|
||||
|
||||
assert.strictEqual(counter, 2);
|
||||
|
||||
});
|
99
playground/decorators/decorators.html
Normal file
99
playground/decorators/decorators.html
Normal file
@ -0,0 +1,99 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>QUnit Example</title>
|
||||
<link rel="stylesheet" href="qunit-1.14.0.css">
|
||||
<script src="jquery-2.1.0.js" type="text/javascript"></script>
|
||||
<script src="jquery.sortable.js" type="text/javascript"></script>
|
||||
<script src="decorators.js"></script>
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
|
||||
$(function () {
|
||||
|
||||
var Selection = clazz(Object, {
|
||||
construct: function () {
|
||||
this.data = ["value1", "value2"]
|
||||
},
|
||||
render: function () {
|
||||
|
||||
var markup = "<ul>";
|
||||
for (var i = 0; i < this.data.length; i++) {
|
||||
markup += "<li>" + this.renderItem(this.data[i]) + "</li>";
|
||||
}
|
||||
markup += "</ul>";
|
||||
return markup;
|
||||
},
|
||||
renderItem: function (item) {
|
||||
return item;
|
||||
},
|
||||
bind: function (container) {
|
||||
$(container).on("click", "li", function () {
|
||||
alert("clicked: " + $(this).text());
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
var ClearExtension = clazz(Object, {
|
||||
construct: function (opts) {
|
||||
this.opts = opts;
|
||||
},
|
||||
|
||||
renderItem: function (item) {
|
||||
var markup = this.decorated(item);
|
||||
|
||||
if (this.opts.position === "right") {
|
||||
markup += " <a href='#' class='clear'>x</a>";
|
||||
} else {
|
||||
markup = " <a href='#' class='clear'>x</a>" + markup;
|
||||
}
|
||||
return markup;
|
||||
},
|
||||
bind: function (container) {
|
||||
this.decorated(container);
|
||||
container.on("click", "a.clear", function (e) {
|
||||
alert("clear: " + $(this).parents("li").text());
|
||||
e.stopPropagation();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var SortableExtension = clazz(Object, {
|
||||
bind: function (container) {
|
||||
this.decorated(container);
|
||||
$(container).find("ul").sortable().bind('sortupdate', function () {
|
||||
alert("sorted");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
var selection = new Selection();
|
||||
|
||||
selection
|
||||
.decorateWith(new ClearExtension({position: "right"}))
|
||||
.decorateWith(new SortableExtension());
|
||||
|
||||
var markup = selection.render();
|
||||
$("#container").html(markup);
|
||||
selection.bind($("#container"));
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<div id="container"></div>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
95
playground/decorators/decorators.js
Normal file
95
playground/decorators/decorators.js
Normal file
@ -0,0 +1,95 @@
|
||||
var clazz = (function () {
|
||||
|
||||
var creatingPrototype = {};
|
||||
|
||||
var methods = function (object) {
|
||||
var methods = {};
|
||||
var proto = object.__proto__;
|
||||
while (proto !== Object.prototype) {
|
||||
for (var methodName in proto) {
|
||||
if (!methods[methodName] && typeof(proto[methodName]) === "function") {
|
||||
methods[methodName] = proto[methodName];
|
||||
}
|
||||
}
|
||||
proto = proto.__proto__;
|
||||
|
||||
}
|
||||
return methods;
|
||||
}
|
||||
|
||||
var decorateWith = function (decorator) {
|
||||
var decoratorMethods = methods(decorator);
|
||||
for (var methodName in decoratorMethods) {
|
||||
var decoratorMethod = decoratorMethods[methodName];
|
||||
if (typeof(decoratorMethod) === "function" && !(methodName === "construct" || methodName.name === "onDelegateAttached" || methodName === "decorateWith")) {
|
||||
this[methodName] = (function (prototype, methodName, method, decorator) {
|
||||
return function () {
|
||||
var delegate = this;
|
||||
try {
|
||||
decorator.decorated = function () {
|
||||
return prototype.apply(delegate, arguments);
|
||||
}
|
||||
return method.apply(decorator, arguments);
|
||||
} finally {
|
||||
delete decorator.decorated;
|
||||
}
|
||||
};
|
||||
})(this[methodName], methodName, decoratorMethod, decorator);
|
||||
this[methodName].displayName = methodName + "#decorator";
|
||||
}
|
||||
}
|
||||
|
||||
if (decorator.onDelegateAttached) {
|
||||
decorator.onDelegateAttached(this);
|
||||
}
|
||||
|
||||
decorator.delegate = this;
|
||||
|
||||
return this;
|
||||
|
||||
};
|
||||
|
||||
return function (SuperClass, methods) {
|
||||
|
||||
var constructor = function (arg) {
|
||||
if (arg === creatingPrototype) {
|
||||
return;
|
||||
}
|
||||
if (this.construct) {
|
||||
this.construct.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
constructor.prototype = SuperClass !== Object ? new SuperClass(creatingPrototype) : new SuperClass;
|
||||
|
||||
for (var methodName in methods) {
|
||||
|
||||
constructor.prototype[methodName] = (function (prototype, methodName, method) {
|
||||
return function () {
|
||||
var currentSuper = this.super;
|
||||
try {
|
||||
this.super = prototype;
|
||||
return method.apply(this, arguments);
|
||||
} finally {
|
||||
this.super = currentSuper;
|
||||
}
|
||||
};
|
||||
})(constructor.prototype[methodName], methodName, methods[methodName]);
|
||||
|
||||
constructor.prototype[methodName].displayName = methodName;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!constructor.prototype.decorateWith) {
|
||||
constructor.prototype.decorateWith = decorateWith;
|
||||
}
|
||||
|
||||
|
||||
return constructor;
|
||||
|
||||
}
|
||||
})();
|
||||
|
||||
|
9111
playground/decorators/jquery-2.1.0.js
vendored
Normal file
9111
playground/decorators/jquery-2.1.0.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
81
playground/decorators/jquery.sortable.js
Normal file
81
playground/decorators/jquery.sortable.js
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* HTML5 Sortable jQuery Plugin
|
||||
* http://farhadi.ir/projects/html5sortable
|
||||
*
|
||||
* Copyright 2012, Ali Farhadi
|
||||
* Released under the MIT license.
|
||||
*/
|
||||
(function($) {
|
||||
var dragging, placeholders = $();
|
||||
$.fn.sortable = function(options) {
|
||||
var method = String(options);
|
||||
options = $.extend({
|
||||
connectWith: false
|
||||
}, options);
|
||||
return this.each(function() {
|
||||
if (/^enable|disable|destroy$/.test(method)) {
|
||||
var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable');
|
||||
if (method == 'destroy') {
|
||||
items.add(this).removeData('connectWith items')
|
||||
.off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s');
|
||||
}
|
||||
return;
|
||||
}
|
||||
var isHandle, index, items = $(this).children(options.items);
|
||||
var placeholder = $('<' + (/^ul|ol$/i.test(this.tagName) ? 'li' : 'div') + ' class="sortable-placeholder">');
|
||||
items.find(options.handle).mousedown(function() {
|
||||
isHandle = true;
|
||||
}).mouseup(function() {
|
||||
isHandle = false;
|
||||
});
|
||||
$(this).data('items', options.items)
|
||||
placeholders = placeholders.add(placeholder);
|
||||
if (options.connectWith) {
|
||||
$(options.connectWith).add(this).data('connectWith', options.connectWith);
|
||||
}
|
||||
items.attr('draggable', 'true').on('dragstart.h5s', function(e) {
|
||||
if (options.handle && !isHandle) {
|
||||
return false;
|
||||
}
|
||||
isHandle = false;
|
||||
var dt = e.originalEvent.dataTransfer;
|
||||
dt.effectAllowed = 'move';
|
||||
dt.setData('Text', 'dummy');
|
||||
index = (dragging = $(this)).addClass('sortable-dragging').index();
|
||||
}).on('dragend.h5s', function() {
|
||||
dragging.removeClass('sortable-dragging').show();
|
||||
placeholders.detach();
|
||||
if (index != dragging.index()) {
|
||||
items.parent().trigger('sortupdate', {item: dragging});
|
||||
}
|
||||
dragging = null;
|
||||
}).not('a[href], img').on('selectstart.h5s', function() {
|
||||
this.dragDrop && this.dragDrop();
|
||||
return false;
|
||||
}).end().add([this, placeholder]).on('dragover.h5s dragenter.h5s drop.h5s', function(e) {
|
||||
if (!items.is(dragging) && options.connectWith !== $(dragging).parent().data('connectWith')) {
|
||||
return true;
|
||||
}
|
||||
if (e.type == 'drop') {
|
||||
e.stopPropagation();
|
||||
placeholders.filter(':visible').after(dragging);
|
||||
return false;
|
||||
}
|
||||
e.preventDefault();
|
||||
e.originalEvent.dataTransfer.dropEffect = 'move';
|
||||
if (items.is(this)) {
|
||||
if (options.forcePlaceholderSize) {
|
||||
placeholder.height(dragging.outerHeight());
|
||||
}
|
||||
dragging.hide();
|
||||
$(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder);
|
||||
placeholders.not(placeholder).detach();
|
||||
} else if (!placeholders.is(this) && !$(this).children(options.items).length) {
|
||||
placeholders.detach();
|
||||
$(this).append(placeholder);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
});
|
||||
};
|
||||
})(jQuery);
|
237
playground/decorators/qunit-1.14.0.css
Normal file
237
playground/decorators/qunit-1.14.0.css
Normal file
@ -0,0 +1,237 @@
|
||||
/*!
|
||||
* QUnit 1.14.0
|
||||
* http://qunitjs.com/
|
||||
*
|
||||
* Copyright 2013 jQuery Foundation and other contributors
|
||||
* Released under the MIT license
|
||||
* http://jquery.org/license
|
||||
*
|
||||
* Date: 2014-01-31T16:40Z
|
||||
*/
|
||||
|
||||
/** Font Family and Sizes */
|
||||
|
||||
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
|
||||
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
|
||||
#qunit-tests { font-size: smaller; }
|
||||
|
||||
|
||||
/** Resets */
|
||||
|
||||
#qunit-tests, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult, #qunit-modulefilter {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
|
||||
/** Header */
|
||||
|
||||
#qunit-header {
|
||||
padding: 0.5em 0 0.5em 1em;
|
||||
|
||||
color: #8699A4;
|
||||
background-color: #0D3349;
|
||||
|
||||
font-size: 1.5em;
|
||||
line-height: 1em;
|
||||
font-weight: 400;
|
||||
|
||||
border-radius: 5px 5px 0 0;
|
||||
}
|
||||
|
||||
#qunit-header a {
|
||||
text-decoration: none;
|
||||
color: #C2CCD1;
|
||||
}
|
||||
|
||||
#qunit-header a:hover,
|
||||
#qunit-header a:focus {
|
||||
color: #FFF;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar label {
|
||||
display: inline-block;
|
||||
padding: 0 0.5em 0 0.1em;
|
||||
}
|
||||
|
||||
#qunit-banner {
|
||||
height: 5px;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar {
|
||||
padding: 0.5em 0 0.5em 2em;
|
||||
color: #5E740B;
|
||||
background-color: #EEE;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#qunit-userAgent {
|
||||
padding: 0.5em 0 0.5em 2.5em;
|
||||
background-color: #2B81AF;
|
||||
color: #FFF;
|
||||
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
|
||||
}
|
||||
|
||||
#qunit-modulefilter-container {
|
||||
float: right;
|
||||
}
|
||||
|
||||
/** Tests: Pass/Fail */
|
||||
|
||||
#qunit-tests {
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
#qunit-tests li {
|
||||
padding: 0.4em 0.5em 0.4em 2.5em;
|
||||
border-bottom: 1px solid #FFF;
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#qunit-tests li strong {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#qunit-tests li a {
|
||||
padding: 0.5em;
|
||||
color: #C2CCD1;
|
||||
text-decoration: none;
|
||||
}
|
||||
#qunit-tests li a:hover,
|
||||
#qunit-tests li a:focus {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
#qunit-tests li .runtime {
|
||||
float: right;
|
||||
font-size: smaller;
|
||||
}
|
||||
|
||||
.qunit-assert-list {
|
||||
margin-top: 0.5em;
|
||||
padding: 0.5em;
|
||||
|
||||
background-color: #FFF;
|
||||
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.qunit-collapsed {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#qunit-tests table {
|
||||
border-collapse: collapse;
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
|
||||
#qunit-tests th {
|
||||
text-align: right;
|
||||
vertical-align: top;
|
||||
padding: 0 0.5em 0 0;
|
||||
}
|
||||
|
||||
#qunit-tests td {
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
#qunit-tests pre {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
#qunit-tests del {
|
||||
background-color: #E0F2BE;
|
||||
color: #374E0C;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#qunit-tests ins {
|
||||
background-color: #FFCACA;
|
||||
color: #500;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/*** Test Counts */
|
||||
|
||||
#qunit-tests b.counts { color: #000; }
|
||||
#qunit-tests b.passed { color: #5E740B; }
|
||||
#qunit-tests b.failed { color: #710909; }
|
||||
|
||||
#qunit-tests li li {
|
||||
padding: 5px;
|
||||
background-color: #FFF;
|
||||
border-bottom: none;
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
/*** Passing Styles */
|
||||
|
||||
#qunit-tests li li.pass {
|
||||
color: #3C510C;
|
||||
background-color: #FFF;
|
||||
border-left: 10px solid #C6E746;
|
||||
}
|
||||
|
||||
#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
|
||||
#qunit-tests .pass .test-name { color: #366097; }
|
||||
|
||||
#qunit-tests .pass .test-actual,
|
||||
#qunit-tests .pass .test-expected { color: #999; }
|
||||
|
||||
#qunit-banner.qunit-pass { background-color: #C6E746; }
|
||||
|
||||
/*** Failing Styles */
|
||||
|
||||
#qunit-tests li li.fail {
|
||||
color: #710909;
|
||||
background-color: #FFF;
|
||||
border-left: 10px solid #EE5757;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
#qunit-tests > li:last-child {
|
||||
border-radius: 0 0 5px 5px;
|
||||
}
|
||||
|
||||
#qunit-tests .fail { color: #000; background-color: #EE5757; }
|
||||
#qunit-tests .fail .test-name,
|
||||
#qunit-tests .fail .module-name { color: #000; }
|
||||
|
||||
#qunit-tests .fail .test-actual { color: #EE5757; }
|
||||
#qunit-tests .fail .test-expected { color: #008000; }
|
||||
|
||||
#qunit-banner.qunit-fail { background-color: #EE5757; }
|
||||
|
||||
|
||||
/** Result */
|
||||
|
||||
#qunit-testresult {
|
||||
padding: 0.5em 0.5em 0.5em 2.5em;
|
||||
|
||||
color: #2B81AF;
|
||||
background-color: #D2E0E6;
|
||||
|
||||
border-bottom: 1px solid #FFF;
|
||||
}
|
||||
#qunit-testresult .module-name {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/** Fixture */
|
||||
|
||||
#qunit-fixture {
|
||||
position: absolute;
|
||||
top: -10000px;
|
||||
left: -10000px;
|
||||
width: 1000px;
|
||||
height: 1000px;
|
||||
}
|
2288
playground/decorators/qunit-1.14.0.js
Normal file
2288
playground/decorators/qunit-1.14.0.js
Normal file
File diff suppressed because it is too large
Load Diff
25
playground/decorators/tests.html
Normal file
25
playground/decorators/tests.html
Normal file
@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link rel="stylesheet" href="qunit-1.14.0.css">
|
||||
<script src="jquery-2.1.0.js" type="text/javascript"></script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
<div id="qunit"></div>
|
||||
<div id="qunit-fixture"></div>
|
||||
<script src="qunit-1.14.0.js"></script>
|
||||
<script src="decorators.js"></script>
|
||||
<script src="decorator_tests.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user