-
Notifications
You must be signed in to change notification settings - Fork 2
/
surface.js
111 lines (91 loc) · 2.99 KB
/
surface.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
111
// Copyright 2011 Daniel Pupius https://github.com/dpup/surface
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Class that manages a single "surface", which corresponds to an element in the DOM.
* Screens provide content that should be displayed when they are active.
*/
goog.provide('surf.Surface');
goog.require('surf');
/**
* @param {string} id
* @param {surf.Content=} opt_defaultContent
* @param {(function(Element, Element) : goog.async.Deferred)=} opt_transitionFn
* @constructor
*/
surf.Surface = function(id, opt_defaultContent, opt_transitionFn) {
this.id = id;
this.el = surf.byId(id);
this.transitionFn = opt_transitionFn || surf.Surface.defaultTransition;
this.activeChild = this.addContent(surf.Surface.DEFAULT, opt_defaultContent);
};
surf.Surface.DEFAULT = 'default';
/**
* @param {Element} from
* @param {Element} to
* @return {goog.async.Deferred}
*/
surf.Surface.defaultTransition = function(from, to) {
if (from) from.style.display = 'none';
if (to) to.style.display = 'block';
return null;
};
/**
* Adds content to a surface.
* @param {string} id The screen id the content belongs too.
* @param {surf.Content=} opt_content The content to add.
* @return {Element} The div that wraps the content.
*/
surf.Surface.prototype.addContent = function(id, opt_content) {
// If content hasn't been passed, see if an element exists in the DOM that matches the id.
// By convention, the element should already be nested in the right element and should have an
// id that is a concatentation of the surface id + '-' + the screen id.
if (!opt_content) {
return surf.byId(this.makeId_(id)) || null;
} else {
var div = surf.dom('div', {id: this.makeId_(id)});
this.transitionFn(div, null);
surf.append(this.el, div);
surf.append(div, opt_content);
return div;
}
};
/**
* @param {string} id The screen id to show.
* @return {goog.async.Deferred}
*/
surf.Surface.prototype.show = function(id) {
var child = surf.byId(this.makeId_(id));
if (!child) {
child = surf.byId(this.makeId_(surf.Surface.DEFAULT));
}
var d = this.transitionFn(this.activeChild, child);
this.activeChild = child;
return d;
};
/**
* @param {string} id The scren id to remove.
*/
surf.Surface.prototype.remove = function(id) {
var child = surf.byId(this.makeId_(id));
if (child) {
surf.remove(child);
}
};
/**
* @param {string} id
* @return {string}
*/
surf.Surface.prototype.makeId_ = function(id) {
return this.id + '-' + id;
};