-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
165 lines (129 loc) · 4.37 KB
/
browser.js
File metadata and controls
165 lines (129 loc) · 4.37 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Module dependencies.
*/
var fs = require('fs');
var debug = require('debug')('glint-block-meta');
var merge = require('utils-merge');
var dot = require('dot');
var domify = require('domify');
var insertCss = require('insert-css');
var addClass = require('amp-add-class');
var removeClass = require('amp-remove-class');
var c = require('./config');
var template = fs.readFileSync(__dirname + '/index.dot', 'utf-8');
var style = fs.readFileSync(__dirname + '/style.css', 'utf-8');
var compiled = dot.template(template);
/**
* Expose MetaBlock element.
*/
exports = module.exports = MetaBlock;
/**
* Initialize a new `MetaBlock` element.
* @param {Object} options object
*/
function MetaBlock(options) {
if (!(this instanceof MetaBlock)) return new MetaBlock(options);
merge(this, c);
merge(this, options);
}
/**
* API functions.
*/
MetaBlock.prototype.api = MetaBlock.api = 'block-provider';
MetaBlock.prototype.place = function() {
return 'wherever';
};
MetaBlock.prototype.load = function(content) {
// display none on load
this.el = this.el || document.querySelector('.topnav');
if (this.el) this.el.style.display = 'none';
//this.el.removeAttribute('contenteditable');
if (typeof content === 'undefined' || typeof content === 'null') return;
this.content = content;
this.setContent(this.content);
this.storeHeaderElements(this.content);
return this.content;
};
MetaBlock.prototype.edit = function() {
var self = this;
// 1. enable or append metablock template
this.el = document.querySelector(this.selector);
if (!this.el) {
// insert css and template
insertCss(style);
var options = this.content ? this.content.blocks ? this.content.blocks : {} : {};
var html = compiled(options);
document.body.appendChild(domify(html));
// add topnav event listeners
this.el = document.querySelector(this.selector);
this.handle = document.querySelector(this.selectorHandle);
this.nav = document.querySelector(this.selectorWindow);
setTimeout(function(){
calculatePositions();
self.el.style.top = self.hidePosition;
}, 100);
var timed = undefined;
function enter(e) {
if (timed) clearTimeout(timed);
calculatePositions();
self.el.style.top = self.showPosition;
addClass(self.el, 'topnav-flyout');
}
function leave(e) {
timed = setTimeout(function() {
calculatePositions();
self.el.style.top = self.hidePosition;
removeClass(self.el, 'topnav-flyout');
}, 100);
}
function calculatePositions() {
self.navHeight = self.nav.getBoundingClientRect().height;
self.showPosition = -20 + 'px';
self.hidePosition = (-20 - self.navHeight) + 'px';
}
this.handle.addEventListener('mouseover', enter);
this.handle.addEventListener('mouseout', leave);
this.nav.addEventListener('mouseover', enter);
this.nav.addEventListener('mouseout', leave);
}
// 2. display the metadata topnav
this.el = document.querySelector(this.selector);
if (this.el) this.el.style.display = '';
// 3. load inputs with content
this.setContent(this.content);
return this.content;
};
MetaBlock.prototype.save = function() {
// save all nested blocks
this.content = this.getContent();
return this.content;
};
/**
* Base functions.
*/
MetaBlock.prototype.getContent = function() {
this.getMetaBlockElement();
var title = this.title ? this.title.value : '';
var description = this.description ? this.description.value : '';
var content = {title: title, description: description};
this.storeHeaderElements(content);
return content;
};
MetaBlock.prototype.setContent = function(content) {
this.getMetaBlockElement();
if (!this.title || !this.description) return;
if (!content) content = this.defaults;
this.title.value = content.title;
this.description.value = content.description;
};
MetaBlock.prototype.getMetaBlockElement = function() {
this.title = document.querySelector('.metadata-title');
this.description = document.querySelector('.metadata-description');
};
MetaBlock.prototype.storeHeaderElements = function(content) {
if (!content) return;
document.title = content.title;
var description = document.head.querySelector('meta[name=description]');
if (!description) description = document.head.appendChild(domify('<meta name="description" >'));
description.setAttribute('content', content.description);
};