diff --git a/lib/Widget.js b/lib/Widget.js index 6621dfe2e..c664ac328 100644 --- a/lib/Widget.js +++ b/lib/Widget.js @@ -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; \ No newline at end of file diff --git a/lib/Widget_jquery.js b/lib/Widget_jquery.js deleted file mode 100644 index b39216a2d..000000000 --- a/lib/Widget_jquery.js +++ /dev/null @@ -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); - }; -}; \ No newline at end of file diff --git a/lib/WidgetsContext.js b/lib/WidgetsContext.js index 994b47297..5f3298a2f 100644 --- a/lib/WidgetsContext.js +++ b/lib/WidgetsContext.js @@ -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; \ No newline at end of file diff --git a/lib/WidgetsContext_browser.js b/lib/WidgetsContext_browser.js deleted file mode 100644 index 02a213533..000000000 --- a/lib/WidgetsContext_browser.js +++ /dev/null @@ -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(); - } -}; \ No newline at end of file diff --git a/lib/optimizer.json b/lib/optimizer.json index cc45eebcb..695653972 100644 --- a/lib/optimizer.json +++ b/lib/optimizer.json @@ -1,7 +1,4 @@ { "dependencies": [ - { "require": "raptor-pubsub" }, - { "require": "./raptor-widgets_browser.js" }, - { "require": "./Widget_jquery.js", "if-extension": "jquery" } ] } \ No newline at end of file diff --git a/lib/package.json b/lib/package.json index 069cb52c2..9f8b4ad9c 100644 --- a/lib/package.json +++ b/lib/package.json @@ -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" } } \ No newline at end of file diff --git a/lib/raptor-widgets_browser.js b/lib/raptor-widgets-browser.js similarity index 97% rename from lib/raptor-widgets_browser.js rename to lib/raptor-widgets-browser.js index 66a0987cb..192804017 100644 --- a/lib/raptor-widgets_browser.js +++ b/lib/raptor-widgets-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]; } diff --git a/lib/raptor-widgets.js b/lib/raptor-widgets.js index e283b806b..4fe563f1d 100644 --- a/lib/raptor-widgets.js +++ b/lib/raptor-widgets.js @@ -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(); + } + } +}; \ No newline at end of file diff --git a/lib/raptor-widgets_server.js b/lib/raptor-widgets_server.js deleted file mode 100644 index 2ac9a563b..000000000 --- a/lib/raptor-widgets_server.js +++ /dev/null @@ -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++; - } -}; \ No newline at end of file diff --git a/lib/uniqueId-browser.js b/lib/uniqueId-browser.js new file mode 100644 index 000000000..6f7d832a5 --- /dev/null +++ b/lib/uniqueId-browser.js @@ -0,0 +1,5 @@ +var nextUniqueId = 0; + +module.exports = function() { + return 'c' + nextUniqueId++; +} \ No newline at end of file diff --git a/lib/uniqueId.js b/lib/uniqueId.js new file mode 100644 index 000000000..c5fd647f9 --- /dev/null +++ b/lib/uniqueId.js @@ -0,0 +1,7 @@ +module.exports = function (context) { + var attrs = context.attributes; + if (!attrs._nextId) { + attrs._nextId = 0; + } + return attrs._nextId++; +}; \ No newline at end of file