mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-15 23:55:50 +00:00
package
This commit is contained in:
@@ -1,254 +0,0 @@
|
||||
var Common = require('common');
|
||||
|
||||
var Evt = require('evt');
|
||||
var basePath = require('nw.gui').App.dataPath;
|
||||
Evt.setDataBasePath(basePath);
|
||||
|
||||
// 启动服务器, 图片
|
||||
var Server = require('server');
|
||||
Server.start();
|
||||
|
||||
|
||||
// 所有service, 与数据库打交道
|
||||
var Service = {
|
||||
notebookService: require('notebook'),
|
||||
noteService: require('note'),
|
||||
tagService: require('tag'),
|
||||
userService: require('user'),
|
||||
tagService: require('tag'),
|
||||
apiService: require('api'),
|
||||
syncServie: require('sync')
|
||||
};
|
||||
|
||||
// 全局变量
|
||||
var ApiService = Service.apiService;
|
||||
var UserService = Service.userService;
|
||||
var SyncService = Service.syncServie;
|
||||
var NoteService = Service.noteService;
|
||||
var NotebookService = Service.notebookService;
|
||||
var TagService = Service.tagService;
|
||||
var WebService = require('web');
|
||||
var ServerService = require('server');
|
||||
var FileService = require('file');
|
||||
var EvtService = require('evt');
|
||||
|
||||
// 分发服务
|
||||
// route = /note/notebook
|
||||
// 过时
|
||||
Service.dispatch = function() {};
|
||||
/*
|
||||
Service.dispatch = function(router, param, callback) {
|
||||
var me = this;
|
||||
router = $.trim(router);
|
||||
if(router.indexOf('/') >= 0) {
|
||||
router = router.substr(router.indexOf('/') + 1);
|
||||
}
|
||||
var routers = router.split('/');
|
||||
var controller = routers[0] + 'Service';
|
||||
var action = routers[1];
|
||||
if(Service[controller] && Service[controller][action]) {
|
||||
Service[controller][action].call(Service[controller], param, callback);
|
||||
} else {
|
||||
log('no such router: ' + router);
|
||||
callback && callback(false);
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
var gui = require('nw.gui');
|
||||
|
||||
function isURL(str_url) {
|
||||
var re = new RegExp("^((https|http|ftp|rtsp|mms|emailto)://).+");
|
||||
return re.test(str_url);
|
||||
}
|
||||
|
||||
// 浏览器打开
|
||||
function openExternal(url) {
|
||||
gui.Shell.openExternal(url);
|
||||
}
|
||||
function isMac() {
|
||||
return process.platform === 'darwin';
|
||||
}
|
||||
|
||||
// 窗口大小设置
|
||||
var win = gui.Window.get();
|
||||
|
||||
var downloadImgPath;
|
||||
$(function() {
|
||||
var isMacP = isMac();
|
||||
$('.tool-close, .tool-close-blur').click(function() {
|
||||
// mac下关闭才是隐藏
|
||||
if(isMacP) {
|
||||
win.hide();
|
||||
} else {
|
||||
win.close();
|
||||
}
|
||||
});
|
||||
gui.App.on('reopen', function() {
|
||||
win.show();
|
||||
win.focus();
|
||||
});
|
||||
|
||||
$('.tool-min, .tool-min-blur').click(function() {
|
||||
win.minimize();
|
||||
});
|
||||
$('.tool-max, .tool-max-blur').click(function() {
|
||||
win.maximize();
|
||||
// win.toggleFullscreen(); // mac下是新屏幕
|
||||
// 全屏模式
|
||||
// win.toggleKioskMode();
|
||||
});
|
||||
|
||||
// 下载图片输入框
|
||||
$('#downloadImgInput').change(function() {
|
||||
var name = $(this).val();
|
||||
$(this).val(''); // 为防止重名不触发
|
||||
if(downloadImgPath) {
|
||||
FileService.download(downloadImgPath, name, function(ok, msg) {
|
||||
// console.log(ok + ' -=-');
|
||||
if(ok) {
|
||||
new window.Notification('Info', {
|
||||
body: 'Image saved successful!',
|
||||
});
|
||||
} else {
|
||||
new window.Notification('Warning', {
|
||||
body: msg || 'Image saved failure!',
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// bind close event
|
||||
// 保存当前打开的笔记
|
||||
|
||||
// 菜单
|
||||
// 更多menu用法: http://www.cnblogs.com/xuanhun/p/3669216.html
|
||||
function Menu() {
|
||||
this.menu = new gui.Menu();
|
||||
this.cut = new gui.MenuItem({
|
||||
label: 'Cut',
|
||||
click: function() {
|
||||
document.execCommand('cut');
|
||||
}
|
||||
});
|
||||
this.copy = new gui.MenuItem({
|
||||
label: 'Copy',
|
||||
click: function() {
|
||||
document.execCommand('copy');
|
||||
}
|
||||
});
|
||||
this.paste = new gui.MenuItem({
|
||||
label: 'Paste',
|
||||
click: function() {
|
||||
// document.execCommand("selectAll");
|
||||
document.execCommand('paste');
|
||||
}
|
||||
});
|
||||
|
||||
this.saveAs = new gui.MenuItem({
|
||||
label: 'Save as',
|
||||
click: function() {
|
||||
// document.execCommand("selectAll");
|
||||
// document.execCommand('paste');
|
||||
var src = $curTarget.attr('src');
|
||||
if(!src) {
|
||||
alert('error');
|
||||
}
|
||||
// 得到图片, 打开dialog
|
||||
FileService.downloadImg(src, function(curPath) {
|
||||
if(curPath) {
|
||||
var paths = curPath.split(/\/|\\/);
|
||||
var name = paths[paths.length-1];
|
||||
downloadImgPath = curPath;
|
||||
$('#downloadImgInput').attr('nwsaveas', name);
|
||||
$('#downloadImgInput').click();
|
||||
|
||||
} else {
|
||||
// alert会死?
|
||||
// alert('File not exists');
|
||||
// https://github.com/nwjs/nw.js/wiki/Notification
|
||||
var notification = new window.Notification('Warning', {
|
||||
body: 'File not exists',
|
||||
// icon: appIcon
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this.openInBrowser = new gui.MenuItem({
|
||||
label: 'Open link in browser',
|
||||
click: function() {
|
||||
// document.execCommand("selectAll");
|
||||
// document.execCommand('paste');
|
||||
// https://github.com/nwjs/nw.js/wiki/Shell
|
||||
openExternal(winHref);
|
||||
}
|
||||
});
|
||||
this.menu.append(this.cut);
|
||||
this.menu.append(this.copy);
|
||||
this.menu.append(this.paste);
|
||||
this.menu.append(new gui.MenuItem({ type: 'separator' }));
|
||||
this.menu.append(this.saveAs);
|
||||
this.menu.append(new gui.MenuItem({ type: 'separator' }));
|
||||
this.menu.append(this.openInBrowser);
|
||||
|
||||
// You can have submenu!
|
||||
/*
|
||||
var submenu = new gui.Menu();
|
||||
submenu.append(new gui.MenuItem({ label: 'checkbox 啊' , type: 'checkbox'}));
|
||||
submenu.append(new gui.MenuItem({ label: 'Item 2', type: 'checkbox'}));
|
||||
submenu.append(new gui.MenuItem({ label: 'Item 3'}));
|
||||
this.openInBrowser.submenu = submenu;
|
||||
*/
|
||||
}
|
||||
Menu.prototype.canCopy = function(bool) {
|
||||
this.cut.enabled = bool;
|
||||
this.copy.enabled = bool;
|
||||
};
|
||||
Menu.prototype.canPaste = function(bool) {
|
||||
this.paste.enabled = bool;
|
||||
};
|
||||
Menu.prototype.canSaveAs = function(bool) {
|
||||
this.saveAs.enabled = bool;
|
||||
};
|
||||
Menu.prototype.canOpenInBroswer = function(bool) {
|
||||
this.openInBrowser.enabled = bool;
|
||||
};
|
||||
Menu.prototype.popup = function(x, y) {
|
||||
this.menu.popup(x, y);
|
||||
};
|
||||
var menu = new Menu();
|
||||
var FS = require('fs');
|
||||
|
||||
// 右键菜单
|
||||
var winHref = '';
|
||||
var $curTarget;
|
||||
$('#noteTitle, #searchNoteInput, #searchNotebookForList, #addTagInput, #wmd-input, #preview-contents, #editorContent').on('contextmenu', function (e) {
|
||||
e.preventDefault();
|
||||
var $target = $(e.target);
|
||||
$curTarget = $target;
|
||||
var text = $target.text();
|
||||
winHref = $target.attr('href');
|
||||
if(!winHref) {
|
||||
winHref = text;
|
||||
}
|
||||
// 判断是否满足http://leanote.com
|
||||
if(winHref) {
|
||||
if(winHref.indexOf('http://127.0.0.1') < 0 && isURL(winHref)) {
|
||||
} else {
|
||||
winHref = false;
|
||||
}
|
||||
}
|
||||
|
||||
menu.canOpenInBroswer(!!winHref);
|
||||
menu.canSaveAs($target.is('img') && $target.attr('src'));
|
||||
var selectionType = window.getSelection().type.toUpperCase();
|
||||
// var clipData = gui.Clipboard.get().get();
|
||||
// menu.canPaste(clipData.length > 0);
|
||||
menu.canPaste(true);
|
||||
menu.canCopy(selectionType === 'RANGE');
|
||||
menu.popup(e.originalEvent.x, e.originalEvent.y);
|
||||
});
|
Reference in New Issue
Block a user