-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
123 lines (99 loc) · 3.54 KB
/
modal.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
112
113
114
115
116
117
118
119
120
121
122
123
/*!
* Creates a bootstrap modal window
/**
@Sam Ranjbari
**/
var bs = (function(window, dom, undefined) {
var ModalDialog = function(options) {
this.events = {};
this.eventsMapper = {
closed: 'closed',
submitted: 'submitted'
}
this.metadata = $.extend({
Id: 'myModal',
content: 'content...',
headerText: ' ',
actionButtonText: 'Yes, Continue',
actionButtonClass: 'btn-primary',
appendTo: '',
contentTemplateId: null,
bindingData: null,
largeModal: false
}, options || {});
}
ModalDialog.prototype = {
init: function () {
var self = this;
var dfd = $.Deferred();
if (this.modalId().length !== 0) {
this.modalId().remove();
}
$.get("templates/modaltmpl.htm")
.done(function(data) {
self.setup(data);
dfd.resolve();
});
return dfd.promise();
},
setup: function (data) {
var self = this;
if (this.metadata.contentTemplateId != null) {
var tmplMarkup = $(this.metadata.contentTemplateId).html();
this.metadata.content = tmplMarkup;
var compiledContent = _.template(this.metadata.content);
this.metadata.content = compiledContent(this.metadata.bindingData);
}
var compiled = _.template(data);
this.metadata.appendTo = this.metadata.appendTo || 'body';
$(this.metadata.appendTo).append(compiled(this.metadata));
$("#" + this.metadata.Id + " .modalActionButton").click(function (e) {
e.preventDefault();
self.emit(self.eventsMapper.submitted, e);
});
$("#" + this.metadata.Id).on('hidden.bs.modal', function (e) {
self.emit(self.eventsMapper.closed, e);
});
},
show: function(func) {
var self = this;
this.init()
.then(function () {
self.modalId().modal(self.metadata);
if (typeof func === 'function') {
func();
}
});
},
hide: function() {
this.modalId().modal('hide');
},
hideCloseButtons: function() {
//$("#" + this.metadata.Id + " .modalCloseButton").attr('disabled', 'disabled');
$(".modalCloseButton").hide();
},
showCloseButtons: function() {
$(".modalCloseButton").show();
},
on: function(event, callback) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(callback);
},
emit: function(event, data) {
var callbacks = this.events[event];
if (callbacks) {
callbacks.forEach(function(callback) {
callback(data);
});
}
},
modalId: function() {
return $("#" + this.metadata.Id);
}
}
return {
ModalDialog: ModalDialog
}
})(this, this.document);