📄 list.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.
*/
/**
* A list if items representing resources with an uri and a caption.
*/
function List() {
if(!Cfx.Class.IsDefined(List)) {
Cfx.Class.New(List);
if(Cfx.Class.IsInitializing(List)) {
List.Method(init);
List.Method(clear);
List.Method(addItem);
List.Method(getItem);
List.Method(removeItem);
List.Method(moveItemUp);
List.Method(moveItemDown);
List.Method(getNumberOfItems);
List.Method(setMaxNumberOfItems);
List.Method(getMaxNumberOfItems);
List.Method(setTopLevelElementName);
List.Method(setItemElementName);
List.Method(setUriAttributeName);
List.Method(getTopLevelElementName);
List.Method(getItemElementName);
List.Method(getUriAttributeName);
List.Method(setCaptionAttributeName);
List.Method(setUriPrefix);
List.Method(getUriPrefix);
List.Method(setUriPostfix);
List.Method(getUriPostfix);
List.Method(serializeAsXml);
List.Method(sortOnCaption);
List.Method(sortOnCaptionHelper);
return;
}
}
this.InitInstance();
this.items = null;
this.maxNumberOfItems = 0;
this.topLevelElementName = null;
this.itemElementName = null;
this.uriAttributeName = null;
this.captionAttributeName = null;
this.uriPrefix = null;
this.uriPostfix = null;
return this;
function init()
{
this.items = new Array();
this.topLevelElementName = "list";
this.itemElementName = "item";
this.uriAttributeName = "uri";
this.captionAttributeName = "caption";
this.uriPrefix = "";
this.uriPostfix = "";
}
/**
* Empties the list
*/
function clear()
{
for (var i = 0; i < this.items.length; i++)
{
this.items[i] = null;
}
this.items = new Array();
}
/**
* @returns the added item object, or null if an item with the same uri is already in the list
*/
function addItem(uri, caption)
{
if (this.maxNumberOfItems > 0 && this.getNumberOfItems() >= this.maxNumberOfItems)
{
success = false;
return null;
}
var newItem = null;
if (this.getItem(uri) == null)
{
newItem = new Item();
newItem.setUri(uri);
newItem.setCaption(caption);
this.items[this.items.length] = newItem;
success = true;
}
return newItem;
}
/**
* @returns the item with the requested uri, or null if it is not in the list
*/
function getItem(uri)
{
var result = null;
if (this.items[uri] != null)
{
result = this.items[uri];
}
else
{
for (var i = 0; i < this.items.length; i++)
{
if (this.items[i] != null && this.items[i].getUri() == uri)
{
result = this.items[i];
}
}
}
return result;
}
/**
* Removes an item from the list.
*
* @returns the removed item, or null if it is not in the list
*/
function removeItem(uri)
{
var result = null;
var found = false;
if (this.items[uri] != null) // uri is really an index
{
result = this.items[uri];
}
for (var i = 0; i < this.items.length; i++)
{
if (result == null && this.items[i] != null && this.items[i].getUri() == uri)
{
result = this.items[i];
found = true;
if (i == this.items.length - 1)
{
this.items.pop();
}
}
else if (result != null && result == this.items[i])
{
found = true;
if (i == this.items.length - 1)
{
this.items.pop();
}
}
else if (found) // item has been found in a previous iteration
{
this.items[i-1] = this.items[i];
if (i == this.items.length - 1)
{
this.items.pop();
}
}
}
return result;
}
function moveItemDown(index)
{
if (index >= 0 && index < this.getNumberOfItems() - 1)
{
var temp = this.items[index + 1];
this.items[index + 1] = this.items[index];
this.items[index] = temp;
temp = null;
}
}
function moveItemUp(index)
{
if (index > 0 && index < this.getNumberOfItems())
{
var temp = this.items[index - 1];
this.items[index - 1] = this.items[index];
this.items[index] = temp;
temp = null;
}
}
/**
* @return the number of items in the list
*/
function getNumberOfItems()
{
return this.items.length;
}
function setMaxNumberOfItems(n)
{
this.maxNumberOfItems = n;
}
function getMaxNumberOfItems()
{
return this.maxNumberOfItems;
}
function setTopLevelElementName(name)
{
this.topLevelElementName = name;
}
function setItemElementName(name)
{
this.itemElementName = name;
}
function setUriAttributeName(name)
{
this.uriAttributeName = name;
}
function getTopLevelElementName(name)
{
return this.topLevelElementName;
}
function getItemElementName(name)
{
return this.itemElementName;
}
function getUriAttributeName(name)
{
return this.uriAttributeName;
}
function setCaptionAttributeName(name)
{
this.captionAttributeName = name;
}
function setUriPrefix(prefix)
{
this.uriPrefix = prefix;
}
function getUriPrefix(prefix)
{
return this.uriPrefix;
}
function setUriPostfix(postfix)
{
this.uriPostfix = postfix;
}
function getUriPostfix(postfix)
{
return this.uriPostfix;
}
/**
* @returns the full list represented in XML code
*/
function serializeAsXml()
{
var xml = "<" + this.topLevelElementName + ">";
for (var i = 0; i < this.items.length; i++)
{
xml += "<" + this.itemElementName;
//xml += " " + this.uriAttributeName + '="' + this.items[i].getUri() + '"';
var strURI = this.items[i].getUri().substr(this.uriPrefix.length);
strURI = strURI.substring(0, strURI.length - this.uriPostfix.length)
xml += " " + this.uriAttributeName + '="' + strURI + '"';
xml += "/>";
}
xml += "</" + this.topLevelElementName + ">";
return xml;
}
/**
* Sorts the list alphabetically on the items' captions.
*/
function sortOnCaption()
{
this.items.sort(this.sortOnCaptionHelper);
}
/**
* Custom sort function used by sortOnCaption.
*/
function sortOnCaptionHelper(a, b)
{
var x = a.getCaption().toLowerCase();
var y = b.getCaption().toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -