Browse Source

Convert Panel model into ts

pull/4431/head
Alex Ritter 4 years ago
parent
commit
5579dc1edf
  1. 42
      src/panels/index.ts
  2. 5
      src/panels/model/Button.ts
  3. 13
      src/panels/model/Buttons.ts
  4. 15
      src/panels/model/Panel.ts
  5. 2
      src/panels/model/Panels.ts
  6. 2
      src/panels/view/ButtonView.js
  7. 5
      test/specs/panels/index.js

42
src/panels/index.js → src/panels/index.ts

@ -25,36 +25,30 @@
* *
* @module Panels * @module Panels
*/ */
import { Module } from '../abstract';
import EditorModel from '../editor/model/Editor';
import defaults from './config/config'; import defaults from './config/config';
import Panel from './model/Panel'; import Panel from './model/Panel';
import Panels from './model/Panels'; import Panels from './model/Panels';
import PanelsView from './view/PanelsView'; import PanelsView from './view/PanelsView';
export default class PanelsManager { export default class PanelsManager extends Module<typeof defaults> {
/** //config = {};
* Name of the module panels: Panels;
* @type {String} PanelsViewObj?: PanelsView;
* @private
*/
name = 'Panels';
config = {};
/** /**
* Initialize module. Automatically called with a new instance of the editor * Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations * @param {Object} config Configurations
* @private * @private
*/ */
init(config) { constructor(em: EditorModel) {
this.config = config || {}; super(em, 'Panels', defaults);
this.panels = new Panels(this.config.defaults);
for (var name in defaults) { for (var name in defaults) {
//@ts-ignore
if (!(name in this.config)) this.config[name] = defaults[name]; if (!(name in this.config)) this.config[name] = defaults[name];
} }
var ppfx = this.config.pStylePrefix;
if (ppfx) this.config.stylePrefix = ppfx + this.config.stylePrefix;
this.panels = new Panels(this.config.defaults);
return this; return this;
} }
@ -85,7 +79,7 @@ export default class PanelsManager {
* buttons : [...], * buttons : [...],
* }); * });
*/ */
addPanel(panel) { addPanel(panel: Panel) {
return this.panels.add(panel); return this.panels.add(panel);
} }
@ -103,7 +97,7 @@ export default class PanelsManager {
* const newPanel = panelManager.removePanel('myNewPanel'); * const newPanel = panelManager.removePanel('myNewPanel');
* *
*/ */
removePanel(panel) { removePanel(panel: Panel) {
return this.panels.remove(panel); return this.panels.remove(panel);
} }
@ -114,7 +108,7 @@ export default class PanelsManager {
* @example * @example
* var myPanel = panelManager.getPanel('myNewPanel'); * var myPanel = panelManager.getPanel('myNewPanel');
*/ */
getPanel(id) { getPanel(id: string) {
var res = this.panels.where({ id }); var res = this.panels.where({ id });
return res.length ? res[0] : null; return res.length ? res[0] : null;
} }
@ -149,7 +143,7 @@ export default class PanelsManager {
* ... * ...
* } * }
*/ */
addButton(panelId, button) { addButton(panelId: string, button: any) {
var pn = this.getPanel(panelId); var pn = this.getPanel(panelId);
return pn ? pn.get('buttons').add(button) : null; return pn ? pn.get('buttons').add(button) : null;
} }
@ -171,7 +165,7 @@ export default class PanelsManager {
* const removedButton = panelManager.removeButton('myNewPanel', 'myNewButton'); * const removedButton = panelManager.removeButton('myNewPanel', 'myNewButton');
* *
*/ */
removeButton(panelId, button) { removeButton(panelId: string, button: any) {
var pn = this.getPanel(panelId); var pn = this.getPanel(panelId);
return pn && pn.get('buttons').remove(button); return pn && pn.get('buttons').remove(button);
} }
@ -184,7 +178,7 @@ export default class PanelsManager {
* @example * @example
* var button = panelManager.getButton('myPanel','myButton'); * var button = panelManager.getButton('myPanel','myButton');
*/ */
getButton(panelId, id) { getButton(panelId: string, id: string) {
var pn = this.getPanel(panelId); var pn = this.getPanel(panelId);
if (pn) { if (pn) {
var res = pn.get('buttons').where({ id }); var res = pn.get('buttons').where({ id });
@ -202,6 +196,7 @@ export default class PanelsManager {
this.PanelsViewObj && this.PanelsViewObj.remove(); this.PanelsViewObj && this.PanelsViewObj.remove();
this.PanelsViewObj = new PanelsView({ this.PanelsViewObj = new PanelsView({
collection: this.panels, collection: this.panels,
//@ts-ignore
config: this.config, config: this.config,
}); });
return this.PanelsViewObj.render().el; return this.PanelsViewObj.render().el;
@ -213,6 +208,7 @@ export default class PanelsManager {
*/ */
active() { active() {
this.getPanels().each(p => { this.getPanels().each(p => {
//@ts-ignore
p.get('buttons').each(btn => { p.get('buttons').each(btn => {
btn.get('active') && btn.trigger('updateActive'); btn.get('active') && btn.trigger('updateActive');
}); });
@ -225,6 +221,7 @@ export default class PanelsManager {
*/ */
disableButtons() { disableButtons() {
this.getPanels().each(p => { this.getPanels().each(p => {
//@ts-ignore
p.get('buttons').each(btn => { p.get('buttons').each(btn => {
if (btn.get('disable')) btn.trigger('change:disable'); if (btn.get('disable')) btn.trigger('change:disable');
}); });
@ -235,6 +232,5 @@ export default class PanelsManager {
this.panels.reset(); this.panels.reset();
this.panels.stopListening(); this.panels.stopListening();
this.PanelsViewObj && this.PanelsViewObj.remove(); this.PanelsViewObj && this.PanelsViewObj.remove();
[this.config, this.panels, this.PanelsViewObj].forEach(i => (i = {}));
} }
} }

5
src/panels/model/Button.js → src/panels/model/Button.ts

@ -1,4 +1,5 @@
import { Model } from '../../common'; import { Model } from '../../common';
import Buttons from './Buttons';
export default class Button extends Model { export default class Button extends Model {
defaults() { defaults() {
@ -21,9 +22,9 @@ export default class Button extends Model {
}; };
} }
initialize(options) { constructor(options: any) {
super(options);
if (this.get('buttons').length) { if (this.get('buttons').length) {
var Buttons = require('./Buttons').default;
this.set('buttons', new Buttons(this.get('buttons'))); this.set('buttons', new Buttons(this.get('buttons')));
} }
} }

13
src/panels/model/Buttons.js → src/panels/model/Buttons.ts

@ -1,7 +1,7 @@
import { Collection } from '../../common'; import { Collection } from '../../common';
import Button from './Button'; import Button from './Button';
export default class Buttons extends Collection { export default class Buttons extends Collection<Button> {
/** /**
* Deactivate all buttons, except one passed * Deactivate all buttons, except one passed
* @param {Object} except Model to ignore * @param {Object} except Model to ignore
@ -9,7 +9,7 @@ export default class Buttons extends Collection {
* *
* @return void * @return void
* */ * */
deactivateAllExceptOne(except, r) { deactivateAllExceptOne(except: Button, r: boolean) {
this.forEach((model, index) => { this.forEach((model, index) => {
if (model !== except) { if (model !== except) {
model.set('active', false); model.set('active', false);
@ -24,11 +24,12 @@ export default class Buttons extends Collection {
* *
* @return void * @return void
* */ * */
deactivateAll(ctx, sender) { deactivateAll(ctx?: string, sender?: any) {
const context = ctx || ''; const context = ctx || '';
this.forEach(model => { this.forEach(model => {
if (model.get('context') == context && model !== sender) { if (model.get('context') == context && model !== sender) {
model.set('active', false, { fromCollection: 1 }); //@ts-ignore
model.set('active', false, { fromCollection: true });
} }
}); });
} }
@ -39,7 +40,7 @@ export default class Buttons extends Collection {
* *
* @return void * @return void
* */ * */
disableAllButtons(ctx) { disableAllButtons(ctx?: string) {
var context = ctx || ''; var context = ctx || '';
this.forEach((model, index) => { this.forEach((model, index) => {
if (model.get('context') == context) { if (model.get('context') == context) {
@ -55,7 +56,7 @@ export default class Buttons extends Collection {
* *
* @return void * @return void
* */ * */
disableAllButtonsExceptOne(except, r) { disableAllButtonsExceptOne(except: Button, r: boolean) {
this.forEach((model, index) => { this.forEach((model, index) => {
if (model !== except) { if (model !== except) {
model.set('disable', true); model.set('disable', true);

15
src/panels/model/Panel.js → src/panels/model/Panel.ts

@ -12,9 +12,18 @@ export default class Panel extends Model {
}; };
} }
initialize(options) { get buttons() {
this.btn = this.get('buttons') || []; return this.get('buttons');
this.buttons = new Buttons(this.btn); }
private set buttons(buttons: Buttons) {
this.set('buttons', buttons);
}
constructor(options: any) {
super(options);
var btn = this.get('buttons') || [];
this.buttons = new Buttons(btn);
this.set('buttons', this.buttons); this.set('buttons', this.buttons);
} }
} }

2
src/panels/model/Panels.js → src/panels/model/Panels.ts

@ -1,6 +1,6 @@
import { Collection } from '../../common'; import { Collection } from '../../common';
import Panel from './Panel'; import Panel from './Panel';
export default class Panels extends Collection {} export default class Panels extends Collection<Panel> {}
Panels.prototype.model = Panel; Panels.prototype.model = Panel;

2
src/panels/view/ButtonView.js

@ -110,7 +110,7 @@ export default class ButtonView extends View {
} }
if (model.get('active')) { if (model.get('active')) {
!fromCollection && model.collection.deactivateAll(context, model); !fromCollection && model.collection?.deactivateAll(context, model);
model.set('active', true, { silent: true }).trigger('checkActive'); model.set('active', true, { silent: true }).trigger('checkActive');
!fromListen && commands.runCommand(command, { ...options, sender: model }); !fromListen && commands.runCommand(command, { ...options, sender: model });

5
test/specs/panels/index.js

@ -1,12 +1,15 @@
import Panels from 'panels'; import Panels from 'panels';
import Panel from 'panels/model/Panel'; import Panel from 'panels/model/Panel';
import Editor from 'editor/model/Editor';
describe('Panels', () => { describe('Panels', () => {
describe('Main', () => { describe('Main', () => {
var em;
var obj; var obj;
beforeEach(() => { beforeEach(() => {
obj = new Panels().init(); em = new Editor({});
obj = new Panels(em);
}); });
afterEach(() => { afterEach(() => {

Loading…
Cancel
Save