-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
76 lines (59 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var events = require('events');
var util = require('util');
var fs = require('fs');
var defaultcss = require('defaultcss');
var domify = require('domify');
var $ = require('dombo');
var ALT = 18;
var $window = $(window);
var style = fs.readFileSync(__dirname + '/index.css', 'utf-8');
var html = fs.readFileSync(__dirname + '/index.html', 'utf-8');
var TitleBar = function(options) {
if(!(this instanceof TitleBar)) return new TitleBar(options);
events.EventEmitter.call(this);
this._options = options || {};
var element = domify(html);
var $element = $(element);
this.element = element;
if(this._options.draggable !== false) $element.addClass('webkit-draggable');
var self = this;
var close = $('.titlebar-close', element)[0];
var minimize = $('.titlebar-minimize', element)[0];
var fullscreen = $('.titlebar-fullscreen', element)[0];
$element.on('click', function(e) {
var target = e.target;
if(close.contains(target)) self.emit('close', e);
else if(minimize.contains(target)) self.emit('minimize', e);
else if(fullscreen.contains(target)) {
if(e.altKey) self.emit('maximize', e);
else self.emit('fullscreen', e);
}
});
$element.on('dblclick', function(e) {
var target = e.target;
if(close.contains(target) || minimize.contains(target) || fullscreen.contains(target)) return;
self.emit('maximize', e);
});
};
util.inherits(TitleBar, events.EventEmitter);
TitleBar.prototype.appendTo = function(target) {
if(typeof target === 'string') target = $(target)[0];
if(this._options.style !== false) defaultcss('titlebar', style);
var $element = $(this.element);
$window.on('keydown', this._onkeydown = function(e) {
if(e.keyCode === ALT) $element.addClass('alt');
});
$window.on('keyup', this._onkeyup = function(e) {
if(e.keyCode === ALT) $element.removeClass('alt');
});
target.appendChild(this.element);
return this;
};
TitleBar.prototype.destroy = function() {
var parent = this.element.parentNode;
if(parent) parent.removeChild(this.element);
$window.off('keydown', this._onkeydown);
$window.off('keyup', this._onkeyup);
return this;
};
module.exports = TitleBar;