-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblock.js
110 lines (93 loc) · 1.99 KB
/
block.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
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
/**
* Module dependencies
*/
var each = require('each');
var classes = require('classes');
var fastdom = require('fastdom');
var afterTransition = require('after-transition');
var clone = require('clone');
var translate = require('translate');
/**
* expose Block
*/
module.exports = Block;
/**
* Block
* @param {Object} attr
* @param {Cast} cast
*/
function Block(attr, cast){
this.cast = cast;
this.attr = attr;
this.pos = {};
this.el = document.createElement('div');
this.classes = classes(this.el);
this.el.className = 'Cast-item hidden';
this.rendered = false;
this.hidden = true;
this.cast.emit('view-created', this);
}
/**
* Set attributes
* @param {Object} attr
*/
Block.prototype.set = function(attr){
this.attr = attr;
};
/**
* Hide the block
* @return {Block}
*/
Block.prototype.hide = function(fn){
if (this.hidden) return;
fastdom.write(function(){
if (fn) afterTransition.once(this.el, fn);
this.classes.add('hidden');
this.el.setAttribute('aria-hidden', true);
}.bind(this));
this.hidden = true;
return this;
};
/**
* Show the block
* @return {Block}
*/
Block.prototype.show = function(){
if (!this.hidden) return;
fastdom.write(function(){
this.classes.remove('hidden');
this.el.setAttribute('aria-hidden', false);
}.bind(this));
this.hidden = false;
return this;
};
/**
* Remove block element from dom
* @return {Block}
*/
Block.prototype.remove = function(){
this.cast.emit('view-destroyed', this);
this.el.parentNode.removeChild(this.el);
return this;
};
/**
* Update position
* @return {Block}
*/
Block.prototype.position = function(pos){
if (pos) this.pos = pos;
var style = this.el.style;
fastdom.write(function(){
style.width = this.pos.width + 'px';
style.height = this.pos.height + 'px';
translate(this.el, this.pos.left, this.pos.top);
}.bind(this));
return this;
};
/**
* Get our attr
* @return {Object}
*/
Block.prototype.toJSON = function(){
return clone(this.attr);
};