handsontable的一种js继承方式
handsontable的一种js继承方式
注意: 部分核心代码如下:
管理类editors.js
(function (Handsontable) {
'use strict';
function RegisteredEditor(editorClass) {
var clazz, instances;
instances = {};
clazz = editorClass;
this.getInstance = function (hotInstance) {
if (!(hotInstance.guid in instances)) {
instances[hotInstance.guid] = new clazz(hotInstance);
}
return instances[hotInstance.guid];
}
}
var registeredEditors = {};
Handsontable.editors = {
/**
* Registers editor under given name
* @param {String} editorName
* @param {Function} editorClass
*/
registerEditor: function (editorName, editorClass) {
registeredEditors[editorName] = new RegisteredEditor(editorClass);
},
/**
* Returns instance (singleton) of editor class
* @param {String|Function} editorName/editorClass
* @returns {Function} editorClass
*/
getEditor: function (editorName, hotInstance) {
if (typeof editorName == 'function'){
var editorClass = editorName;
editorName = editorClass.toString();
this.registerEditor(editorName, editorClass);
}
if (typeof editorName != 'string'){
throw Error('Only strings and functions can be passed as "editor" parameter ');
}
if (!(editorName in registeredEditors)) {
throw Error('No editor registered under name "' + editorName + '"');
}
return registeredEditors[editorName].getInstance(hotInstance);
}
};
})(Handsontable);
基类baseEditor.js
(function (Handsontable) {
'use strict';
function BaseEditor(instance) {
//this.prop = value
this.init();
}
BaseEditor.prototype.init = function(){};
BaseEditor.prototype.extend = function(){
var baseClass = this.constructor;
function Editor(){
baseClass.apply(this, arguments);
}
function inherit(Child, Parent){
function Bridge() {
}
Bridge.prototype = Parent.prototype;
Child.prototype = new Bridge();
Child.prototype.constructor = Child;
return Child;
}
return inherit(Editor, baseClass);
};
//BaseEditor.prototype.xxx = function(){}
Handsontable.editors.BaseEditor = BaseEditor;
})(Handsontable);
扩展类1checkboxEditor.js
(function(Handsontable){
//Blank editor, because all the work is done by renderer
var CheckboxEditor = Handsontable.editors.BaseEditor.prototype.extend();
CheckboxEditor.prototype.beginEditing = function () {
this.saveValue([
[!this.originalValue]
]);
};
CheckboxEditor.prototype.finishEditing = function () {};
CheckboxEditor.prototype.init = function () {};
CheckboxEditor.prototype.open = function () {};
CheckboxEditor.prototype.close = function () {};
CheckboxEditor.prototype.getValue = function () {};
CheckboxEditor.prototype.setValue = function () {};
CheckboxEditor.prototype.focus = function () {};
Handsontable.editors.CheckboxEditor = CheckboxEditor;
Handsontable.editors.registerEditor('checkbox', CheckboxEditor);
})(Handsontable);
扩展类2 dropdownEditor.js
(function (Handsontable) {
var DropdownEditor = Handsontable.editors.AutocompleteEditor.prototype.extend();
DropdownEditor.prototype.prepare = function () {
Handsontable.editors.AutocompleteEditor.prototype.prepare.apply(this, arguments);
this.cellProperties.filter = false;
this.cellProperties.strict = true;
};
Handsontable.editors.DropdownEditod = DropdownEditor;
Handsontable.editors.registerEditor('dropdown', DropdownEditor);
})(Handsontable);
扩展类2,3,4,5……
使用方法
var editorClass = instance.getCellEditor(cellProperties);
activeEditor = Handsontable.editors.getEditor(editorClass, instance);