Passed
Pull Request — master (#66)
by Michael
02:50
created

PageMacroMenuItemDispatcher.js ➔ getMenuItem   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
nc 1
nop 1
dl 0
loc 36
rs 9.184
c 1
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A PageMacroMenuItemDispatcher.js ➔ ... ➔ ??? 0 9 2
1
import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher';
2
import MenuItem from '../MenuItem';
3
import SetDocAttrStep from '../../../custom/SetDocAttrStep';
4
import { svgIcon } from '../MDI';
5
6
export default class PageMacroMenuItemDispatcher extends AbstractMenuItemDispatcher {
7
    /**
8
     * Create an MenuItemDispatcher to toggle the state of a page macro
9
     *
10
     * @param {string} macro The page macro, should be either NOCACHE or NOTOC
11
     */
12
    constructor(macro) {
13
        super();
14
        this.macro = macro;
15
    }
16
17
    isAvailable({ nodes }) {
18
        return !!nodes.doc.attrs[this.macro.toLowerCase()];
19
    }
20
21
    getMenuItem({ nodes }) {
22
        if (!this.isAvailable({ nodes })) {
23
            throw new Error('Plugin block not available in schema!');
24
        }
25
        const macroAttrName = this.macro.toLowerCase();
26
        return new MenuItem({
27
            command: (state, dispatch) => {
28
                if (dispatch) {
29
                    const newState = !state.doc.attrs[macroAttrName];
30
                    const tr = state.tr;
31
                    tr.step(new SetDocAttrStep(macroAttrName, newState));
32
                    dispatch(tr);
33
                }
34
                return true;
35
            },
36
            render: (editorView) => {
0 ignored issues
show
Unused Code introduced by
The parameter editorView is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
                const label = LANG.plugins.prosemirror[`label:${macroAttrName}`];
0 ignored issues
show
Bug introduced by
The variable LANG seems to be never declared. If this is a global, consider adding a /** global: LANG */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
38
                const blankIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-blank-outline'), label).addClass('is-false');
39
                const checkedIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-marked-outline'), label).addClass('is-true');
40
                const dom = jQuery('<span>').addClass('menuitem');
41
                dom.append(blankIcon);
42
                dom.append(checkedIcon);
43
                dom.append(jQuery('<span>')
44
                    .addClass('menulabel')
45
                    .text(label));
46
                return dom.get(0);
47
            },
48
            update: (editorView, dom) => {
49
                const $dom = jQuery(dom);
50
                const attrState = editorView.state.doc.attrs[macroAttrName];
51
                $dom.find('.is-true').toggle(attrState);
52
                $dom.find('.is-false').toggle(!attrState);
53
            },
54
            isActive: state => state.doc.attrs[macroAttrName],
55
        });
56
    }
57
}
58