📄 application.menu.js
字号:
this.parent = null;
this.text = text;
this.icon = icon;
if (items) {
for (var i = 0; i < items.length; ++i) {
items[i].parent = this;
}
}
this.items = items ? items : [];
},
/**
* Adds an item to the MenuModel.
*
* @param {Extras.ItemModel} item the item (must be a MenuModel, OptionModel, or SeparatorModel.
*/
addItem: function(item) {
this.items.push(item);
item.parent = this;
},
/**
* Finds an item by id in the <code>MenuModel</code>, searching descendant <code>MenuModel</code>s as necessary.
*
* @param id the id of the menu item to find
* @return the item model, or null if it cannot be found
* @type Extras.ItemModel
*/
findItem: function(id) {
var i;
for (i = 0; i < this.items.length; ++i) {
if (this.items[i].id == id) {
return this.items[i];
}
}
for (i = 0; i < this.items.length; ++i) {
if (this.items[i] instanceof Extras.MenuModel) {
var itemModel = this.items[i].findItem(id);
if (itemModel) {
return itemModel;
}
}
}
return null;
},
/**
* Returns the <code>ItemModel</code> at a specific path within this menu model.
*
* @param {Array} itemPositions array of integers describing path, e.g., [0,1,2] would
* indicate the third item in the second item in the first item in this menu model.
* @return the found item
* @type Extras.ItemModel
*/
getItemModelFromPositions: function(itemPositions) {
var menuModel = this;
for (var i = 0; i < itemPositions.length; ++i) {
menuModel = menuModel.items[parseInt(itemPositions[i], 10)];
}
return menuModel;
},
/**
* Determines the index of the specified menu item.
*
* @param {Extras.ItemModel} item the item to find
* @return the index of the item, or -1 if it cannot be found
* @type Number
*/
indexOfItem: function(item) {
for (var i = 0; i < this.items.length; ++i) {
if (this.items[i] == item) {
return i;
}
}
return -1;
},
/** @see Object#toString */
toString: function() {
return "MenuModel \"" + this.text + "\" Items:" + this.items.length;
}
});
/**
* Representation of a menu option.
*/
Extras.OptionModel = Core.extend(Extras.ItemModel, {
/**
* The menu title.
* @type String
*/
text: null,
/**
* The menu icon.
* @type #ImageReference
*/
icon: null,
/**
* Creates a new menu option.
*
* @param {String} modelId the id of the menu model
* @param {String} text the menu item title
* @param {#ImageReference} icon the menu item icon
*/
$construct: function(modelId, text, icon) {
this.modelId = modelId;
this.id = Extras.uniqueId++;
this.parent = null;
this.text = text;
this.icon = icon;
},
/**
* Returns an array containing the path of this model to its most distant ancestor, consisting of
* positions.
*
* @return the array of positions
* @type Array
*/
getItemPositionPath: function() {
var path = [];
var itemModel = this;
while (itemModel.parent != null) {
path.unshift(itemModel.parent.indexOfItem(itemModel));
itemModel = itemModel.parent;
}
return path;
},
/** @see Object#toString */
toString: function() {
return "OptionModel \"" + this.text + "\"";
}
});
/**
* Representation of a toggle button (checkbox) menu option.
*/
Extras.ToggleOptionModel = Core.extend(Extras.OptionModel, {
/**
* Creates a new toggle option.
*
* @param {String} modelId the id of the menu model
* @param {String} text the menu item title
* @param {Boolean} initial selection state
*/
$construct: function(modelId, text, selected) {
Extras.OptionModel.call(this, modelId, text, null);
this.selected = selected;
}
});
/**
* Representation of a radio button menu option.
*/
Extras.RadioOptionModel = Core.extend(Extras.ToggleOptionModel, {
/**
* Creates a radio option.
*
* @param {String} modelId the id of the menu model
* @param {String} text the menu item title
* @param {Boolean} initial selection state
*/
$construct: function(modelId, text, selected) {
Extras.ToggleOptionModel.call(this, modelId, text, selected);
}
});
/**
* A representation of a menu separator.
*/
Extras.SeparatorModel = Core.extend(Extras.ItemModel, { });
/**
* Representation of menu model state, describing which items are selected and/or disabled.
*/
Extras.MenuStateModel = Core.extend({
/**
* Disabled menu item ids.
* @type Array
*/
_disabledItems: null,
/**
* Selected menu item ids.
* @type Array
*/
_selectedItems: null,
/**
* Creates a new <code>MenuStateModel</code>.
*/
$construct: function() {
this._disabledItems = [];
this._selectedItems = [];
},
/**
* Determines if the specified menu item is enabled.
*
* @param {String} modelId the item model id
* @return true if the item is enabled
* @type Boolean
*/
isEnabled: function(modelId) {
if (modelId) {
for (var i = 0; i < this._disabledItems.length; i++) {
if (this._disabledItems[i] == modelId) {
return false;
}
}
}
return true;
},
/**
* Determines if the specified menu item is selected.
*
* @param {String} modelId the item model id
* @return true if the item is selected
* @type Boolean
*/
isSelected: function(modelId) {
if (modelId) {
for (var i = 0; i < this._selectedItems.length; i++) {
if (this._selectedItems[i] == modelId) {
return true;
}
}
}
return false;
},
/**
* Sets the enabled state of a menu item.
*
* @param {String} modelId the item model id
* @param {Boolean} enabled the enabled state
*/
setEnabled: function(modelId, enabled) {
if (enabled) {
Core.Arrays.remove(this._disabledItems, modelId);
} else {
if (Core.Arrays.indexOf(this._disabledItems, modelId) == -1) {
this._disabledItems.push(modelId);
}
}
},
/**
* Sets the selection state of a menu item.
*
* @param {String} modelId the item model id
* @param {Boolean} selected the selection state
*/
setSelected: function(modelId, selected) {
if (selected) {
if (Core.Arrays.indexOf(this._selectedItems, modelId) == -1) {
this._selectedItems.push(modelId);
}
} else {
Core.Arrays.remove(this._selectedItems, modelId);
}
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -