mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Cleanup
This commit is contained in:
parent
0806fcb97c
commit
cf39d8a06d
@ -13,18 +13,21 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* Mixins applied to the prototypes of all widget instances
|
||||
* @mixin
|
||||
*
|
||||
* @borrows raptor/listeners/Observable#publish as #publish
|
||||
* @borrows raptor/listeners/Observable#subscribe as #subscribe
|
||||
*/
|
||||
'use strict';
|
||||
var JQUERY = 'jquery';
|
||||
var extend = require('raptor-util').extend;
|
||||
var raptorListeners = require('raptor-listeners');
|
||||
var raptorDom = require('raptor-dom');
|
||||
var raptorWidgets = require('raptor-widgets');
|
||||
var idRegExp = /\#(\w+)( .*)?/g;
|
||||
|
||||
var jquery = window.$;
|
||||
if (!jquery) {
|
||||
try {
|
||||
jquery = require(JQUERY);
|
||||
}
|
||||
catch(e) {}
|
||||
}
|
||||
|
||||
function _destroy(widget, removeNode, recursive) {
|
||||
function walkDOM(el) {
|
||||
@ -166,10 +169,52 @@ Widget.prototype = widgetProto = {
|
||||
},
|
||||
ready: function (callback) {
|
||||
raptorWidgets.ready(callback, this);
|
||||
},
|
||||
$: function (arg) {
|
||||
var args = arguments;
|
||||
if (args.length === 1) {
|
||||
//Handle an "ondomready" callback function
|
||||
if (typeof arg === 'function') {
|
||||
var _this = this;
|
||||
jquery(function () {
|
||||
arg.apply(_this, args);
|
||||
});
|
||||
} else if (typeof arg === 'string') {
|
||||
var match = idRegExp.exec(arg);
|
||||
idRegExp.lastIndex = 0;
|
||||
//Reset the search to 0 so the next call to exec will start from the beginning for the new string
|
||||
if (match != null) {
|
||||
var widgetElId = match[1];
|
||||
if (match[2] == null) {
|
||||
return jquery(this.getEl(widgetElId));
|
||||
} else {
|
||||
return jquery('#' + this.getElId(widgetElId) + match[2]);
|
||||
}
|
||||
} else {
|
||||
var rootEl = this.getEl();
|
||||
if (!rootEl) {
|
||||
throw new Error('Root element is not defined for widget');
|
||||
}
|
||||
if (rootEl) {
|
||||
return jquery(arg, rootEl);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (args.length === 2) {
|
||||
if (typeof args[1] === 'string') {
|
||||
return jquery(arg, this.getEl(args[1]));
|
||||
}
|
||||
} else if (args.length === 0) {
|
||||
return jquery(this.el);
|
||||
}
|
||||
return jquery.apply(window, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
widgetProto.on = widgetProto.subscribe;
|
||||
widgetProto.elId = widgetProto.getElId;
|
||||
|
||||
|
||||
|
||||
module.exports = Widget;
|
||||
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 eBay Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* jQuery extensions applied to all widgets
|
||||
*
|
||||
* @extension jQuery
|
||||
*/
|
||||
'use strict';
|
||||
var idRegExp = /\#(\w+)( .*)?/g;
|
||||
|
||||
module.exports = function($) {
|
||||
return function (arg) {
|
||||
var args = arguments;
|
||||
if (args.length === 1) {
|
||||
//Handle an "ondomready" callback function
|
||||
if (typeof arg === 'function') {
|
||||
var _this = this;
|
||||
$(function () {
|
||||
arg.apply(_this, args);
|
||||
});
|
||||
} else if (typeof arg === 'string') {
|
||||
var match = idRegExp.exec(arg);
|
||||
idRegExp.lastIndex = 0;
|
||||
//Reset the search to 0 so the next call to exec will start from the beginning for the new string
|
||||
if (match != null) {
|
||||
var widgetElId = match[1];
|
||||
if (match[2] == null) {
|
||||
return $(this.getEl(widgetElId));
|
||||
} else {
|
||||
return $('#' + this.getElId(widgetElId) + match[2]);
|
||||
}
|
||||
} else {
|
||||
var rootEl = this.getEl();
|
||||
if (!rootEl) {
|
||||
throw new Error('Root element is not defined for widget');
|
||||
}
|
||||
if (rootEl) {
|
||||
return $(arg, rootEl);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (args.length === 2) {
|
||||
if (typeof args[1] === 'string') {
|
||||
return $(arg, this.getEl(args[1]));
|
||||
}
|
||||
} else if (args.length === 0) {
|
||||
return $(this.el);
|
||||
}
|
||||
return $.apply(window, arguments);
|
||||
};
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var WidgetDef = require('./WidgetDef');
|
||||
var uniqueId = require('./uniqueId');
|
||||
var WIDGET_CONTEXT_KEY = 'widgets';
|
||||
|
||||
function WidgetsContext(context) {
|
||||
this.context = context;
|
||||
@ -46,8 +47,24 @@ WidgetsContext.prototype = {
|
||||
this.widgetStack = [];
|
||||
},
|
||||
_nextWidgetId: function () {
|
||||
return 'w' + this.context.uniqueId();
|
||||
return 'w' + uniqueId(this.context);
|
||||
},
|
||||
initWidgets: function () {
|
||||
var widgetDefs = this.widgets;
|
||||
var widgets = require('./');
|
||||
widgetDefs.forEach(function (widgetDef) {
|
||||
widgets.initWidget(widgetDef);
|
||||
});
|
||||
this.clearWidgets();
|
||||
}
|
||||
};
|
||||
|
||||
WidgetsContext.getWidgetsContext = function (context) {
|
||||
var attributes = context.attributes;
|
||||
|
||||
return attributes[WIDGET_CONTEXT_KEY] ||
|
||||
(attributes[WIDGET_CONTEXT_KEY] = new WidgetsContext(context));
|
||||
};
|
||||
|
||||
|
||||
module.exports = WidgetsContext;
|
||||
@ -1,11 +0,0 @@
|
||||
'use strict';
|
||||
module.exports = {
|
||||
initWidgets: function () {
|
||||
var widgetDefs = this.widgets;
|
||||
var widgets = require('./');
|
||||
widgetDefs.forEach(function (widgetDef) {
|
||||
widgets.initWidget(widgetDef);
|
||||
});
|
||||
this.clearWidgets();
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,4 @@
|
||||
{
|
||||
"dependencies": [
|
||||
{ "require": "raptor-pubsub" },
|
||||
{ "require": "./raptor-widgets_browser.js" },
|
||||
{ "require": "./Widget_jquery.js", "if-extension": "jquery" }
|
||||
]
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
{
|
||||
"main": "raptor-widgets.js",
|
||||
"browser": {
|
||||
"./raptor-widgets_server.js": null
|
||||
"./raptor-widgets.js": "./raptor-widgets-browser.js",
|
||||
"./uniqueId.js": "./uniqueId-browser.js"
|
||||
}
|
||||
}
|
||||
@ -19,11 +19,11 @@
|
||||
*/
|
||||
'use strict';
|
||||
var forEach = require('raptor-util').forEach;
|
||||
var raptorWidgets = require('./');
|
||||
var logger = require('raptor-logging').logger(module);
|
||||
var scopes = window.$rwidgetScopes || (window.$rwidgetScopes = {});
|
||||
var isArray = Array.isArray;
|
||||
var Widget = require('./Widget');
|
||||
var ready = require('raptor-dom').ready;
|
||||
|
||||
function _convertEvents(events) {
|
||||
var convertedEvents = {};
|
||||
@ -169,13 +169,15 @@ function _registerWidget(path, id, assignedId, config, scope, events, bubbleErro
|
||||
if (widget.initBeforeOnDomReady === true) {
|
||||
_doInitWidget();
|
||||
} else {
|
||||
raptorWidgets.ready(_doInitWidget, widget);
|
||||
ready(_doInitWidget, widget);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getWidgetsContext: require('./WidgetsContext').getWidgetsContext,
|
||||
uniqueId: require('./uniqueId'),
|
||||
initWidget: function (widgetDef) {
|
||||
var result = _registerWidget(
|
||||
widgetDef.path,
|
||||
@ -207,7 +209,7 @@ module.exports = {
|
||||
var node = document.getElementById(id);
|
||||
return node.__widget || null;
|
||||
},
|
||||
ready: require('raptor-dom').ready,
|
||||
ready: ready,
|
||||
_remove: function (id) {
|
||||
delete scopes[id];
|
||||
}
|
||||
@ -18,36 +18,117 @@
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
var WidgetsContext = require('./WidgetsContext');
|
||||
var extend = require('raptor-util').extend;
|
||||
var WIDGET_CONTEXT_KEY = 'widgets';
|
||||
|
||||
exports.getWidgetsContext = function (context) {
|
||||
var attributes = context.attributes;
|
||||
|
||||
return attributes[WIDGET_CONTEXT_KEY] ||
|
||||
(attributes[WIDGET_CONTEXT_KEY] = new WidgetsContext(context));
|
||||
};
|
||||
/*
|
||||
* Copyright 2011 eBay Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* @extension Server
|
||||
*
|
||||
*/
|
||||
var stringify = require('raptor-json/stringify');
|
||||
var raptorModulesResolver = require('raptor-modules/resolver');
|
||||
var raptorModulesUtil = require('raptor-modules/util');
|
||||
var raptorModulesTransport = require('raptor-modules/transport');
|
||||
var nodePath = require('path');
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
extend(exports, require('./raptor-widgets_' + 'server'));
|
||||
} else {
|
||||
extend(exports, require('./raptor-widgets_browser'));
|
||||
extend(WidgetsContext.prototype, require('./WidgetsContext_browser'));
|
||||
var specialRegExp = /([^ -~]|(["'\\<]))/g;
|
||||
var requireInfoCache = {};
|
||||
var aCharCode = 'a'.charCodeAt(0);
|
||||
|
||||
/*
|
||||
var jquery = window.$;
|
||||
if (!jquery) {
|
||||
try {
|
||||
jquery = require('jquery');
|
||||
}
|
||||
catch(e) {}
|
||||
function getWidgetRequireInfo(path) {
|
||||
var requireInfo = requireInfoCache[path];
|
||||
if (requireInfo === undefined) {
|
||||
var logicalPath = raptorModulesUtil.getPathInfo(path).logicalPath;
|
||||
|
||||
|
||||
var dirname = nodePath.dirname(path);
|
||||
var raptorWidgetsPath = raptorModulesResolver.resolveRequire('raptor-widgets', dirname);
|
||||
|
||||
requireInfo = requireInfoCache[path] = {
|
||||
path: logicalPath,
|
||||
init: 'require("' +raptorWidgetsPath.logicalPath + '")._init'
|
||||
};
|
||||
}
|
||||
|
||||
if (jquery) {
|
||||
try {
|
||||
require('./Widget').prototype.$ = require('./Widget_' + 'jquery')(jquery);
|
||||
} catch(e) {}
|
||||
}
|
||||
*/
|
||||
return requireInfo;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getWidgetsContext: require('./WidgetsContext').getWidgetsContext,
|
||||
uniqueId: require('./uniqueId'),
|
||||
|
||||
writeInitWidgetsCode: function (widgetsContext, context, clearWidgets) {
|
||||
var widgets = widgetsContext.getWidgets();
|
||||
var varsLookup = {};
|
||||
var vars = [];
|
||||
|
||||
var buffer = [];
|
||||
|
||||
if (!widgets) {
|
||||
return;
|
||||
}
|
||||
function write(str) {
|
||||
buffer.push(str);
|
||||
}
|
||||
function writeWidgets(widgets, isChildren) {
|
||||
for (var i = 0, len = widgets.length; i < len; i++) {
|
||||
if (isChildren && i) {
|
||||
write(',');
|
||||
}
|
||||
writeWidget(widgets[i]);
|
||||
}
|
||||
}
|
||||
function writeWidget(widget) {
|
||||
var requireInfo = getWidgetRequireInfo(widget.path);
|
||||
var varName = varsLookup[requireInfo.init];
|
||||
if (!varName) {
|
||||
varName = varsLookup[requireInfo.init] = String.fromCharCode(aCharCode + vars.length);
|
||||
vars.push(varName + '=' + requireInfo.init);
|
||||
}
|
||||
var widgetConfig = widget.config;
|
||||
write('\n' + varName + '("');
|
||||
write(requireInfo.path);
|
||||
write('","');
|
||||
write(widget.id);
|
||||
write('",');
|
||||
write(widgetConfig ? stringify(widgetConfig, { special: specialRegExp }) : '0');
|
||||
write(widget.scope ? ',"' + widget.scope.id + '"' : ',0');
|
||||
write(widget.assignedId ? ',"' + widget.assignedId + '"' : ',0');
|
||||
if (widget.events) {
|
||||
write(',[');
|
||||
widget.events.forEach(function (event) {
|
||||
write('["' + event[0] + '","' + event[1] + (event[2] != null ? '",' + stringify(event[2]) + ']' : '"]'));
|
||||
});
|
||||
write(']');
|
||||
} else {
|
||||
write(',0');
|
||||
}
|
||||
if (widget.children.length) {
|
||||
write(',');
|
||||
writeWidgets(widget.children, true);
|
||||
}
|
||||
write(')');
|
||||
}
|
||||
|
||||
writeWidgets(widgets);
|
||||
|
||||
context.write(raptorModulesTransport.runCode.sync('/', 'var ' + vars.join(',') + buffer.join('')));
|
||||
|
||||
if (clearWidgets !== false) {
|
||||
widgetsContext.clearWidgets();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1,117 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 eBay Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* @extension Server
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
var stringify = require('raptor-json/stringify');
|
||||
var raptorModulesResolver = require('raptor-modules/resolver');
|
||||
var raptorModulesUtil = require('raptor-modules/util');
|
||||
var raptorModulesTransport = require('raptor-modules/transport');
|
||||
var nodePath = require('path');
|
||||
|
||||
var specialRegExp = /([^ -~]|(["'\\<]))/g;
|
||||
var requireInfoCache = {};
|
||||
var aCharCode = 'a'.charCodeAt(0);
|
||||
|
||||
function getWidgetRequireInfo(path) {
|
||||
var requireInfo = requireInfoCache[path];
|
||||
if (requireInfo === undefined) {
|
||||
var logicalPath = raptorModulesUtil.getPathInfo(path).logicalPath;
|
||||
|
||||
|
||||
var dirname = nodePath.dirname(path);
|
||||
var raptorWidgetsPath = raptorModulesResolver.resolveRequire('raptor-widgets', dirname);
|
||||
|
||||
requireInfo = requireInfoCache[path] = {
|
||||
path: logicalPath,
|
||||
init: 'require("' +raptorWidgetsPath.logicalPath + '")._init'
|
||||
};
|
||||
}
|
||||
return requireInfo;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
writeInitWidgetsCode: function (widgetsContext, context, clearWidgets) {
|
||||
var widgets = widgetsContext.getWidgets();
|
||||
var varsLookup = {};
|
||||
var vars = [];
|
||||
|
||||
var buffer = [];
|
||||
|
||||
if (!widgets) {
|
||||
return;
|
||||
}
|
||||
function write(str) {
|
||||
buffer.push(str);
|
||||
}
|
||||
function writeWidgets(widgets, isChildren) {
|
||||
for (var i = 0, len = widgets.length; i < len; i++) {
|
||||
if (isChildren && i) {
|
||||
write(',');
|
||||
}
|
||||
writeWidget(widgets[i]);
|
||||
}
|
||||
}
|
||||
function writeWidget(widget) {
|
||||
var requireInfo = getWidgetRequireInfo(widget.path);
|
||||
var varName = varsLookup[requireInfo.init];
|
||||
if (!varName) {
|
||||
varName = varsLookup[requireInfo.init] = String.fromCharCode(aCharCode + vars.length);
|
||||
vars.push(varName + '=' + requireInfo.init);
|
||||
}
|
||||
var widgetConfig = widget.config;
|
||||
write('\n' + varName + '("');
|
||||
write(requireInfo.path);
|
||||
write('","');
|
||||
write(widget.id);
|
||||
write('",');
|
||||
write(widgetConfig ? stringify(widgetConfig, { special: specialRegExp }) : '0');
|
||||
write(widget.scope ? ',"' + widget.scope.id + '"' : ',0');
|
||||
write(widget.assignedId ? ',"' + widget.assignedId + '"' : ',0');
|
||||
if (widget.events) {
|
||||
write(',[');
|
||||
widget.events.forEach(function (event) {
|
||||
write('["' + event[0] + '","' + event[1] + (event[2] != null ? '",' + stringify(event[2]) + ']' : '"]'));
|
||||
});
|
||||
write(']');
|
||||
} else {
|
||||
write(',0');
|
||||
}
|
||||
if (widget.children.length) {
|
||||
write(',');
|
||||
writeWidgets(widget.children, true);
|
||||
}
|
||||
write(')');
|
||||
}
|
||||
|
||||
writeWidgets(widgets);
|
||||
|
||||
context.write(raptorModulesTransport.runCode.sync('/', 'var ' + vars.join(',') + buffer.join('')));
|
||||
|
||||
if (clearWidgets !== false) {
|
||||
widgetsContext.clearWidgets();
|
||||
}
|
||||
},
|
||||
_nextWidgetId: function (context) {
|
||||
var attributes = context.getAttributes();
|
||||
if (!attributes.nextWidgetId) {
|
||||
attributes.nextWidgetId = 0;
|
||||
}
|
||||
return 's' + attributes.nextWidgetId++;
|
||||
}
|
||||
};
|
||||
5
lib/uniqueId-browser.js
Normal file
5
lib/uniqueId-browser.js
Normal file
@ -0,0 +1,5 @@
|
||||
var nextUniqueId = 0;
|
||||
|
||||
module.exports = function() {
|
||||
return 'c' + nextUniqueId++;
|
||||
}
|
||||
7
lib/uniqueId.js
Normal file
7
lib/uniqueId.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = function (context) {
|
||||
var attrs = context.attributes;
|
||||
if (!attrs._nextId) {
|
||||
attrs._nextId = 0;
|
||||
}
|
||||
return attrs._nextId++;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user