Cat/Cat.js

235 lines
6.9 KiB
JavaScript

const {remote} = require('electron');
const {dialog,Menu,MenuItem} = remote;
const ipcRenderer = require('electron').ipcRenderer;
const settings = require('electron').remote.require('electron-settings');
const CatProject = require('./classes/CatProject');
const CatEmitter = require('./classes/CatEmitter');
const {inherits} = require('util');
function TrueCat() {
CatEmitter.call(this);
}; inherits(TrueCat, CatEmitter);
let Cat = new TrueCat();
Cat.loadedProjects = [];
Cat.focusedProject = null;
Cat.addProject = function() {
let treeView = document.querySelector('cat-tree');
let tabview = document.querySelector('cat-tabview');
let tab = document.createElement('cat-tab');
tab.setAttribute('label', '');
let view = document.createElement('cat-projectview');
view.className = 'project-view';
view.addEventListener('click', function(e) {
//console.log(e);
});
tab.appendChild(view);
let proj = new CatProject(view);
proj.tab = tab;
tab.project = proj;
tabview.appendChild(tab);
proj.on('loaded', e => {
proj.tab.setAttribute('label', proj.getTitle());
Cat.emit('project-loaded', proj);
this.focusProject(proj);
});
Cat.loadedProjects.push(proj);
tabview.selectTab(tab);
return proj;
}
Cat.openProject = function() {
dialog.showOpenDialog({
title: "Open Project",
properties: ["openFile", "promptToCreate", "createDirectory"],
filters: [
{name: 'HTML', extensions: ['html', 'htm']}
]
}, function(filenames) {
if (!filenames || filenames.length == 0) return;
Cat.loadProject(filenames[0]);
ipcRenderer.send('open-file', filenames[0]);
});
}
Cat.loadProject = function(filename) {
let proj = this.addProject();
try {
proj.load(filename, () => {
//let tabView = document.querySelector('cat-tabview');
//tabView.selectTab(proj.tab.control);
//console.log('setting our title to: ' + proj.getTitle());
//proj.tab.setAttribute('label', proj.getTitle());
});
} catch (e) {
console.log(e);
alert('Failed to load ' + filename + ' as a project');
}
}
Cat.saveProject = function(proj=Cat.focusedProject, cb) {
if (!proj) return;
console.dir(proj);
proj.save(proj.path, err => {
if (err) {
alert("Failed to save file");
}
if (cb) cb(err);
});
}
Cat.saveProjectAs = function(proj=Cat.focusedProject, cb) {
if (!proj) return;
dialog.showSaveDialog({}, filename => {
if (!filename) return;
console.dir(proj);
proj.save(filename, err => {
if (err) {
alert("Failed to save file :S");
}
if (cb) cb(err);
});
});
}
Cat.closeProject = function(proj=Cat.focusedProject, force=false) {
if (!proj) return;
if (proj.hasChanges && !force) {
dialog.showMessageBox({
type: "question",
title: "Unsaved changes for Project",
message: "Project has unsaved changes!",
buttons: ["Save", "Save As", "Cancel", "OK"]
}, index => {
const SAVE = 0, SAVE_AS = 1, CANCEL = 2, OK = 3;
if (index == CANCEL) { // cancel
} else if (index == OK) { // ok
this.closeProject(proj, true);
return true;
} else if (index == SAVE && proj.path != '') { // save
this.saveProject(proj, err => {
if (err) return;
this.closeProject(proj);
});
return false;
} else if (index == SAVE_AS || proj.path == '') { // save as
this.saveProjectAs(proj, err => {
if (err) return;
this.closeProject(proj);
});
return false;
}
});
} else {
// TODO: augh, rework this stuff
this.loadedProjects.splice(this.loadedProjects.indexOf(proj), 1);
document.querySelector('cat-tabview').removeTab(proj.tab.control, true);
this.emit('project-closed', proj);
return true;
}
}
Cat.focusProject = function(proj, cb) {
if (!proj) {
return;
}
Cat.focusedProject = proj;
Cat.emit('project-focused', proj);
}
Cat.getProjectFromTab = function(tab) {
for (var i = 0; i < this.loadedProjects.length; i++) {
if (this.loadedProjects[i].tab === tab) {
return this.loadedProjects[i];
}
}
return null;
}
Cat.init = function() {
let tabView = document.querySelector('cat-tabview');
Cat.loadModule('elements-view');
Cat.loadModule('animations-view');
tabView.addEventListener('close-tab', evt => {
let proj = this.getProjectFromTab(evt.detail.content);
if (!proj) {
alert('Target tab does not have an associated project! Closing.');
return;
}
if (this.closeProject(proj)) {
evt.preventDefault();
return false;
}
return true;
});
tabView.addEventListener('select-tab', evt => {
this.focusProject(this.getProjectFromTab(evt.detail));
});
tabView.addEventListener('remove-tab', evt => {
var proj = this.getProjectFromTab(evt.detail);
if (!proj) return;
//this.loadedProjects
});
tabView.addEventListener('loaded', evt => {
var proj = this.getProjectFromTab(evt.detail);
if (!proj) return;
console.log('fuuu');
});
Cat.setupControlHooks();
/* set up ipc */
ipcRenderer.on('menu', function(event, item) {
if (item.type == 'new') {
console.log('new');
Cat.addProject();
} else if (item.type == 'open') {
console.log('open');
Cat.openProject();
} else if (item.type == 'save') {
console.log('save');
Cat.saveProject();
} else if (item.type == 'save-as') {
console.log('save-as');
Cat.saveProjectAs();
} else if (item.type == 'close') {
console.log('close');
Cat.closeProject();
}
});
/* load our settings */
settings.has('openProjects') ? settings.get('openProjects').forEach(item => {
}):'';
}
Cat.setupControlHooks = function() {
document.querySelector('cat-ctl-2dtransform').addEventListener('value-change', e => {
console.log(e.detail.type + ': ' + e.detail.value);
});
}
Cat.setSelectorAllAttribute = function(which, attr, value) {
var items = document.querySelectorAll(which);
for (var i = 0; i < items.length; i++) {
items[i].setAttribute(attr, value);
}
}
Cat.modules = [];
Cat.loadModule = function(name) {
if (Cat.modules[name]) return; // already loaded, return
console.log('loading Module: ' + name);
try {
if (Cat.emit('loading-module', {name: name}) === false) return;
Cat.modules[name] = require('./modules/'+name)(Cat);
for (var i in Cat.modules[name].on) {
console.log('adding hook: ' + i);
Cat.on(i, Cat.modules[name].on[i]);
}
if (Cat.modules[name].init) Cat.modules[name].init(document);
Cat.emit('loaded-module', {name: name, module: Cat.modules[name]});
} catch (e) {
alert('Unhandled module loading error: ' + e);
}
}
Cat.unloadModule = function(name) {
if (!Cat.modules[name]) return; // not loaded, return
for (var i in Cat.modules[name].on) {
Cat.removeListener(i, Cat.modules[name].on[i]);
}
// TODO: remove module from require cache
}
module.exports = Cat;