-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog-manager.html
138 lines (112 loc) · 4.14 KB
/
dialog-manager.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-button/paper-button.html" />
<link rel="import" href="../paper-dialog/paper-dialog.html" />
<link rel="import" href="../paper-spinner/paper-spinner.html" />
<dom-module id="dialog-manager">
<script>
function DialogBuilder(polymerElementRef) {
this._html = '';
this._appendHtml = '';
this._polymerElementRef = polymerElementRef;
}
DialogBuilder.prototype.html = function(html) {
this._html = html;
return this;
};
DialogBuilder.prototype.appendHtml = function(appendHtml) {
this._appendHtml = appendHtml;
return this;
};
DialogBuilder.prototype.title = function(title) {
this._title = title;
return this;
};
DialogBuilder.prototype.body = function(body) {
this._body = body;
return this;
};
DialogBuilder.prototype.confirm = function(confirm, autoFocus) {
this._confirm = new String(confirm);
if(autoFocus) {
this._autoFocus = this._confirm;
}
return this;
};
DialogBuilder.prototype.dismiss = function(dismiss, autoFocus) {
this._dismiss = new String(dismiss);
if(autoFocus) {
this._autoFocus = this._dismiss;
}
return this;
};
DialogBuilder.prototype.attrs = function(obj) {
this._attrs = obj;
return this;
};
DialogBuilder.prototype.spinner = function() {
this._attrs = this._attrs || {};
this._attrs.modal = 'true';
this._spinner = true;
return this;
};
DialogBuilder.prototype.build = function(callback) {
var d = document.createElement('paper-dialog');
if(this._attrs) {
for (var key1 in this._attrs) {
d.setAttribute(key1, this._attrs[key1]);
}
} else if(this._polymerElementRef.defaultAttributes) {
for (var key2 in this._polymerElementRef.defaultAttributes) {
d.setAttribute(key2, this._polymerElementRef.defaultAttributes[key2]);
}
}
d.innerHTML = this._html +
(this._title? ('<h2>' + this._title + '</h2>') : '')+
(this._body? ('<p>' + this._body + '</p>') : '') +
(this._spinner? ('<paper-spinner active></paper-spinner>') : '') +
((this._confirm||this._dismiss) ? (
'<div class="buttons custom">' +
(this._dismiss? ('<paper-button dialog-dismiss ' + (this._autoFocus==this._dismiss?'autofocus':'') + '>' + this._dismiss + '</paper-button>') : '') +
(this._confirm? ('<paper-button dialog-confirm ' + (this._autoFocus==this._confirm?'autofocus':'') + '>' + this._confirm + '</paper-button>') : '') +
'</div>'
):'') + this._appendHtml;
var b = Polymer.dom(this._polymerElementRef).appendChild(d);
b.addEventListener('iron-overlay-closed', function(customEvent) {
customEvent.preventDefault();
if(callback) callback(customEvent);
b['remove']();
});
var originalShow = b['open'];
b['open'] = function() {
this._polymerElementRef.async(function() {
originalShow.bind(b)();
});
return b;
}.bind(this);
return b;
};
DialogBuilder.prototype.open = function(callback) {
return this.build(callback).open();
};
Polymer({
is:'dialog-manager',
properties: {
defaultAttributes:{type:Object,value:{}}
},
start:function() {
return new DialogBuilder(this);
},
removeDialog:function(node) {
Polymer.dom(this).removeChild(node);
},
getAllDialogs:function() {
return Polymer.dom(this).childNodes;
},
removeAllDialogs:function() {
Polymer.dom(this).childNodes.forEach(function(child){
child.remove();
}.bind(this));
}
});
</script>
</dom-module>