/
button.js
72 lines (67 loc) · 1.99 KB
/
button.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
define([
'underscore',
'mockup-ui-url/views/base',
'mockup-patterns-tooltip'
], function(_, BaseView, Tooltip) {
'use strict';
var ButtonView = BaseView.extend({
tagName: 'a',
className: 'btn',
eventPrefix: 'button',
context: 'default',
idPrefix: 'btn-',
attributes: {
'href': '#'
},
extraClasses: [],
tooltip: null,
template: '<% if (icon) { %><span class="glyphicon glyphicon-<%= icon %>"></span><% } %> <%= title %>',
events: {
'click': 'handleClick'
},
initialize: function(options) {
if (!options.id) {
var title = options.title || '';
options.id = title !== '' ? title.toLowerCase().replace(' ', '-') : this.cid;
}
BaseView.prototype.initialize.apply(this, [options]);
this.on('render', function() {
this.$el.attr('title', this.options.title || '');
this.$el.attr('aria-label', this.options.title || this.options.tooltip || '');
if (this.context !== null) {
this.$el.addClass('btn-' + this.context);
}
_.each(this.extraClasses, function(klass){
this.$el.addClass(klass);
});
if (this.tooltip !== null) {
this.$el.attr('title', this.tooltip);
var tooltipPattern = new Tooltip(this.$el);
// XXX since tooltip triggers hidden
// suppress so it plays nice with modals, backdrops, etc
this.$el.on('hidden', function(e) {
if (e.type === 'hidden') {
e.stopPropagation();
}
});
}
}, this);
},
handleClick: function(e) {
e.preventDefault();
if (!this.$el.is('.disabled')) {
this.uiEventTrigger('click', this, e);
}
},
serializedModel: function() {
return _.extend({'icon': '', 'title': ''}, this.options);
},
disable: function() {
this.$el.addClass('disabled');
},
enable: function() {
this.$el.removeClass('disabled');
}
});
return ButtonView;
});