Skip to content

Commit

Permalink
Refactor monolith
Browse files Browse the repository at this point in the history
Moved methods from Action and Gio services to Cursor, Opposite, Panel, Place, Selection and Tab, grouping by purpose and common data. Cancellable and Log helpers no longer in use, removed them. Improved #3, needs more work though. Fixed #4, resolving URIs with GioFile.
  • Loading branch information
nykula committed Dec 11, 2017
1 parent 65b14c2 commit f4eb0ee
Show file tree
Hide file tree
Showing 103 changed files with 4,766 additions and 3,995 deletions.
47 changes: 20 additions & 27 deletions src/app/Action/ActionBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ const { ReliefStyle } = imports.gi.Gtk;
const Component = require("inferno-component").default;
const h = require("inferno-hyperscript").default;
const { connect } = require("inferno-mobx");
const autoBind = require("../Gjs/autoBind").default;
const { autoBind } = require("../Gjs/autoBind");
const ActionBarRm = require("./ActionBarRm").default;
const { ActionService } = require("./ActionService");

const actions = [
{ type: "view", text: "View", shortcut: "F3" },
{ type: "editor", text: "Edit", shortcut: "F4" },
{ type: "cp", text: "Copy", shortcut: "F5" },
{ type: "mv", text: "Move", shortcut: "F6" },
{ type: "mkdir", text: "NewFolder", shortcut: "F7" },
{ type: "rm", text: "Delete", shortcut: "F8" },
{ type: "exit", text: "Exit", shortcut: "Alt+F4" },
{ id: "cursorService.view", label: "View", shortcut: "F3" },
{ id: "cursorService.edit", label: "Edit", shortcut: "F4" },
{ id: "oppositeService.cp", label: "Copy", shortcut: "F5" },
{ id: "oppositeService.mv", label: "Move", shortcut: "F6" },
{ id: "directoryService.mkdir", label: "NewFolder", shortcut: "F7" },
{ id: "selectionService.rm", label: "Delete", shortcut: "F8" },
{ id: "windowService.exit", label: "Exit", shortcut: "Alt+F4" },
];

/**
Expand All @@ -34,28 +34,21 @@ ActionBar.prototype = Object.create(Component.prototype);
*/
ActionBar.prototype.props = undefined;

/**
* @param {string} type
*/
ActionBar.prototype.handlePressed = function(type) {
return () => this.props.actionService[type]();
};

ActionBar.prototype.render = function() {
return (
h("box", { expand: false }, actions.reduce((prev, action) => prev.concat([
action.type === "rm" ? h(ActionBarRm, {
key: action.type,
label: action.shortcut + " " + action.text,
}) : h("button", {
can_focus: false,
expand: true,
key: action.type,
label: action.shortcut + " " + action.text,
on_pressed: this.handlePressed(action.type),
relief: ReliefStyle.NONE,
}),
h("v-separator", { key: action.type + "+" }),
action.id === "rm" ? h(ActionBarRm, {
key: action.id,
label: action.shortcut + " " + action.label,
}) : h("button", {
can_focus: false,
expand: true,
key: action.id,
label: action.shortcut + " " + action.label,
on_pressed: this.props.actionService.get(action.id).handler,
relief: ReliefStyle.NONE,
}),
h("v-separator", { key: action.id + "+" }),
]), []))
);
};
Expand Down
12 changes: 6 additions & 6 deletions src/app/Action/ActionBar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const { ActionBar } = require("./ActionBar");

describe("ActionBar", () => {
it("dispatches action without payload", () => {
const views = [];
const handler = expect.createSpy();

/** @type {any} */
const actionService = {
view: function() {
views.push(arguments.length);
},
get: () => ({ handler }),
};

const tree = shallow(h(ActionBar, { actionService: actionService }));
const tree = shallow(h(ActionBar, { actionService }));
const button = find(tree, x => x.type === "button");

button.props.on_pressed();
expect(views).toEqual([0]);
expect(handler).toHaveBeenCalledWith();
});
});
39 changes: 30 additions & 9 deletions src/app/Action/ActionBarRm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ const { DestDefaults, ReliefStyle } = imports.gi.Gtk;
const Component = require("inferno-component").default;
const h = require("inferno-hyperscript").default;
const { connect } = require("inferno-mobx");
const autoBind = require("../Gjs/autoBind").default;
const { ActionService } = require("./ActionService");
const { autoBind } = require("../Gjs/autoBind");
const { JobService } = require("../Job/JobService");
const { PanelService } = require("../Panel/PanelService");
const { SelectionService } = require("../Selection/SelectionService");

/**
* @typedef IProps
* @property {ActionService} actionService
* @property {JobService} jobService
* @property {PanelService} panelService
* @property {SelectionService} selectionService
* @property {string} label
*
* @param {IProps} props
Expand All @@ -26,23 +30,38 @@ ActionBarRm.prototype = Object.create(Component.prototype);
ActionBarRm.prototype.props = undefined;

/**
* @param {*} _node
* @param {any} _node
* @param {{ get_selected_action(): number }} _dragContext
* @param {number} _x
* @param {number} _y
* @param {{ get_uris(): string[] }} selectionData
*/
ActionBarRm.prototype.handleDrop = function(_node, _dragContext, _x, _y, selectionData) {
ActionBarRm.prototype.handleDrop = function(
_node,
_dragContext,
_x,
_y,
selectionData,
) {
const { jobService, panelService } = this.props;
const uris = selectionData.get_uris();
this.props.actionService.rm(uris);

jobService.run(
{
destUri: "",
type: "rm",
uris,
},
panelService.refresh,
);
};

ActionBarRm.prototype.handlePressed = function() {
this.props.actionService.rm();
this.props.selectionService.rm();
};

/**
* @param {*} node
* @param {any} node
*/
ActionBarRm.prototype.ref = function(node) {
node.drag_dest_set(DestDefaults.ALL, [], DragAction.MOVE);
Expand All @@ -64,4 +83,6 @@ ActionBarRm.prototype.render = function() {
};

exports.ActionBarRm = ActionBarRm;
exports.default = connect(["actionService"])(ActionBarRm);
exports.default = connect(["jobService", "panelService", "selectionService"])(
ActionBarRm,
);
45 changes: 30 additions & 15 deletions src/app/Action/ActionBarRm.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const { DragAction } = imports.gi.Gdk;
const expect = require("expect");
const h = require("inferno-hyperscript").default;
const { h } = require("../Gjs/GtkInferno");
const { find, shallow } = require("../Test/Test");
const { ActionBarRm } = require("./ActionBarRm");

describe("ActionBarRm", () => {
it("renders", () => {
new ActionBarRm({
actionService: undefined,
jobService: undefined,
label: "",
panelService: undefined,
selectionService: undefined,
}).render();
});

Expand All @@ -19,42 +21,55 @@ describe("ActionBarRm", () => {
};

new ActionBarRm({
actionService: undefined,
jobService: undefined,
label: "",
panelService: undefined,
selectionService: undefined,
}).ref(node);

expect(node.drag_dest_set).toHaveBeenCalled();
expect(node.drag_dest_add_uri_targets).toHaveBeenCalled();
});

it("removes files on drop", () => {
/** @type {*} */
const actionService = {
rm: expect.createSpy(),
it("removes dropped files", () => {
/** @type {any} */
const jobService = {
run: expect.createSpy(),
};

/** @type {*} */
/** @type {any} */
const selectionData = {
get_uris: () => ["file:///foo.bar"],
};

/** @type {any} */
const panelService = {
refresh: expect.createSpy(),
};

new ActionBarRm({
actionService,
jobService,
label: "",
panelService,
selectionService: undefined,
}).handleDrop(undefined, undefined, 0, 0, selectionData);

expect(actionService.rm).toHaveBeenCalledWith(["file:///foo.bar"]);
expect(jobService.run).toHaveBeenCalledWith({
destUri: "",
type: "rm",
uris: ["file:///foo.bar"],
}, panelService.refresh);
});

it("shows confirm on click", () => {
/** @type {*} */
const actionService = {
it("removes selected files on click", () => {
/** @type {any} */
const selectionService = {
rm: expect.createSpy(),
};

const button = shallow(h(ActionBarRm, { actionService }));
const button = shallow(h(ActionBarRm, { selectionService }));
button.props.on_pressed();

expect(actionService.rm).toHaveBeenCalledWith();
expect(selectionService.rm).toHaveBeenCalled();
});
});
Loading

0 comments on commit f4eb0ee

Please sign in to comment.