📄 components.js
字号:
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* 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.
*/
/**
* Component interface
example interface for classes used in the framework
interface component() {
public enableLogging();
public contextualize();
public service();
public configure();
public getId();
}
*/
/**
* BaseComponent
* Provides an abstract implementation of the component interface
*/
function baseComponent() {
if(!Cfx.Class.IsDefined(baseComponent)) {
Cfx.Class.New(baseComponent);
if(Cfx.Class.IsInitializing(baseComponent)) {
baseComponent.Method(enableLogging);
baseComponent.Method(contextualize);
baseComponent.Method(service);
baseComponent.Method(initServices);
baseComponent.Method(configure);
baseComponent.Method(getId);
return;
}
}
this.InitInstance();
this.config = null;
this.log = null;
this.sm = null;
this.context = null;
return this;
function enableLogging(logger) {
this.log = logger;
}
function contextualize(context) {
this.context = context;
}
function service(service) {
this.sm = service;
}
function configure(config) {
this.config = config;
}
function getId() {
return this.config.id;
}
function initServices(){
for(var i=0;i<this.config.list.length;i++){
//add framework services
//will be exposed to all components
var compFactory = this.sm.lookup("framework.componentFactory");
var comp = compFactory.createComponent(this.config.list[i], this.log, this.context, this.sm);
comp.init();
this.sm.put(this.getId() + "." + this.config.list[i].id, comp);
}
}
}
function AbstractContainer(){
if(!Cfx.Class.IsDefined(AbstractContainer)) {
Cfx.Class.New(AbstractContainer);
if(Cfx.Class.IsInitializing(AbstractContainer)) {
AbstractContainer.Method(getCount);
AbstractContainer.Method(isEmpty);
AbstractContainer.Method(isFull);
//AbstractContainer.Method(accept);
//AbstractContainer.Method(getEnumeration);
return;
}
}
this.InitInstance();
this.count = 0;
return this;
function getCount(){
return this.count;
}
function isEmpty(){
return this.getCount() == 0;
}
function isFull(){
//hmm
return false;
}
}
function QueueAsArray(){
if(!Cfx.Class.IsDefined(QueueAsArray)) {
Cfx.Class.New(QueueAsArray, AbstractContainer);
if(Cfx.Class.IsInitializing(QueueAsArray)) {
QueueAsArray.Method(getHead);
QueueAsArray.Method(enqueue);
QueueAsArray.Method(dequeue);
QueueAsArray.Method(purge);
QueueAsArray.Method(empty);
return;
}
}
this.InitInstance();
var size = (arguments[0]!=undefined) ? arguments[0] : 10;
this.array = new Array(size);
this.head = 0;
this.tail = size-1;
return this;
function purge(){
while(this.count=0){
this.array[this.head] = null;
if(++this.head == this.array.length)
this.head=0;
--this.count;
}
}
function getHead(){
if(this.count == 0)
throw new Error("getHead() : Queue is empty!");
return this.array[head];
}
function enqueue(obj){
if(this.count == this.array.length)
throw new Error("enqueue() : Queue is full");
if(++this.tail == this.array.length)
this.tail = 0;
this.array[this.tail] = obj;
++this.count;
}
function dequeue(){
if(this.count == 0)
throw new Error("dequeue() : Queue is empty");
var res = this.array[this.head];
this.array[this.head] = null;
if(++this.head == this.array.length)
this.head = 0;
--this.count;
return res;
}
function empty() {
for(x in this.array) {
x = null;
}
this.head = 0;
this.tail = size-1;
}
}
/**
* default component factory
* Create, setup and initialize a new component
*/
function DefaultComponentFactory() {
if(!Cfx.Class.IsDefined(DefaultComponentFactory)) {
Cfx.Class.New(DefaultComponentFactory);
if(Cfx.Class.IsInitializing(DefaultComponentFactory)) {
DefaultComponentFactory.Method(createComponent);
DefaultComponentFactory.Method(disposeComponent);
DefaultComponentFactory.Method(setup);
return;
}
}
this.InitInstance();
/**
* @param ServiceManager
* @param Logger
*/
if(arguments.length) {
this.log = arguments[0];
}
return this;
function createComponent(_config, _logger, _context, _serviceManager) {
var serviceManager = _serviceManager;
var context = _context;
var config = _config;
var logger = _logger;
var component; //reference to the new instance
if(config == undefined)
throw new Error("No config found while trying to instanciate component. Please check your Application configuration.");
if(config.id == null)
throw new Error("No id attribute found for component " + config);
//classname of the component
var className = config.className;
if (className == null)
throw new Error("No className configured for component " + config.id);
try {
var construct = "new " + className + "()";
if (this.log.debugging()) {
this.log.debug("Creating: " + construct);
}
component = eval(construct);
} catch (ex) {
this.log.error("Class " + className + " was not found", "init:");
}
if(FrameworkConfiguration.framework.logEnabled){
var logLevel = INFO_LEVEL;
if(config.logLevel)
logLevel = config.logLevel;
else if(logger != null)
logLevel = logger.level;
if(config.logger && config.id){
//this component has a logger configured so create a new logger obj.
if (this.log.debugging()) {
this.log.debug("Creating a new DefaultLogger for " + config.id);
}
logger = new DefaultLogger(config.id, top.bottomframe, config.logger, logLevel);
}
else if(logger && config.id) {
if (this.log.debugging()) {
this.log.debug("Child Logger for " + config.id);
}
logger = logger.getChildLogger(config.id, logLevel);
}else logger = new EmptyLogger(config.id);
}else {
if(logger)
logger = logger.getChildLogger(config.id);
else
logger = new EmptyLogger(config.id);
}
return this.setup(component, config, logger, context, serviceManager);
}
/**
* Not in use
*/
function disposeComponent(component) {
if(Cfx.Js.IsDefined(component) && Cfx.Js.IsFunction(component.dispose)) {
component.dispose();
}
}
function setup(component, config, logger, context, serviceManager) {
if (logger == null) {
logger = this.log;
}
if (component.enableLogging != null) {
component.enableLogging(logger);
}
if (component.contextualize != null) {
component.contextualize(context);
}
if (component.service != null) {
component.service(serviceManager);
}
if (component.configure != null) {
component.configure(config);
}
return component;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -