Commit 64b3343f authored by Joshua Gould's avatar Joshua Gould

remove event

parent 5845108e
...@@ -2,16 +2,16 @@ ...@@ -2,16 +2,16 @@
* @param options.autohideTabBar * @param options.autohideTabBar
* Whether to autohide the tab bar when only 1 tab showing * Whether to autohide the tab bar when only 1 tab showing
*/ */
morpheus.TabManager = function(options) { morpheus.TabManager = function (options) {
this.options = $.extend({}, { this.options = $.extend({}, {
autohideTabBar : false autohideTabBar: false
}, options); }, options);
var _this = this; var _this = this;
this.activeTabObject = null; this.activeTabObject = null;
this.activeTabId = null; this.activeTabId = null;
this.idToTabObject = new morpheus.Map(); this.idToTabObject = new morpheus.Map();
this.$nav = $('<ul class="nav nav-tabs"></ul>'); this.$nav = $('<ul class="nav nav-tabs"></ul>');
this.$nav.on('click', 'li > a', function(e) { this.$nav.on('click', 'li > a', function (e) {
var tabId = $(this).attr('href'); var tabId = $(this).attr('href');
e.preventDefault(); e.preventDefault();
if (_this.activeTabId !== tabId) { if (_this.activeTabId !== tabId) {
...@@ -23,14 +23,14 @@ morpheus.TabManager = function(options) { ...@@ -23,14 +23,14 @@ morpheus.TabManager = function(options) {
var builder = new morpheus.FormBuilder(); var builder = new morpheus.FormBuilder();
builder.append({ builder.append({
name : 'name', name: 'name',
type : 'text', type: 'text',
value : $.trim($a.contents().first().text()) value: $.trim($a.contents().first().text())
}); });
morpheus.FormBuilder.showOkCancel({ morpheus.FormBuilder.showOkCancel({
title : 'Rename Tab', title: 'Rename Tab',
content : builder.$form, content: builder.$form,
okCallback : function() { okCallback: function () {
var name = $.trim(builder.getValue('name')); var name = $.trim(builder.getValue('name'));
if (name !== '') { if (name !== '') {
_this.activeTabObject.setName(name); _this.activeTabObject.setName(name);
...@@ -40,27 +40,28 @@ morpheus.TabManager = function(options) { ...@@ -40,27 +40,28 @@ morpheus.TabManager = function(options) {
}); });
// edit tab name // edit tab name
} }
// rename // rename
this.$nav.on('dblclick', 'li > a', function(e) { this.$nav.on('dblclick', 'li > a', function (e) {
e.preventDefault(); e.preventDefault();
if ($(this).data('morpheus-rename')) { if ($(this).data('morpheus-rename')) {
rename($(this)); rename($(this));
} }
}); });
this.$nav.on('contextmenu.morpheus', 'li > a', function(e) { this.$nav.on('contextmenu.morpheus', 'li > a', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
e.stopImmediatePropagation(); e.stopImmediatePropagation();
var $a = $(this); var $a = $(this);
if ($a.data('morpheus-rename')) { if ($a.data('morpheus-rename')) {
morpheus.Popup.showPopup([ { morpheus.Popup.showPopup([{
name : 'Rename' name: 'Rename'
} ], { }], {
x : e.pageX, x: e.pageX,
y : e.pageY y: e.pageY
}, e.target, function(event, item) { }, e.target, function (event, item) {
rename($a); rename($a);
}); });
} }
...@@ -68,7 +69,7 @@ morpheus.TabManager = function(options) { ...@@ -68,7 +69,7 @@ morpheus.TabManager = function(options) {
}); });
this.$nav.on('click', 'button', function(e) { // close a tab this.$nav.on('click', 'button', function (e) { // close a tab
// remove the link and tab content // remove the link and tab content
e.preventDefault(); e.preventDefault();
var target = $(this).attr('data-target'); var target = $(this).attr('data-target');
...@@ -76,7 +77,7 @@ morpheus.TabManager = function(options) { ...@@ -76,7 +77,7 @@ morpheus.TabManager = function(options) {
}); });
this.$tabContent = $('<div class="tab-content"></div>'); this.$tabContent = $('<div class="tab-content"></div>');
this.$nav.on('shown.bs.tab', 'a[data-toggle="tab"]', function(e) { this.$nav.on('shown.bs.tab', 'a[data-toggle="tab"]', function (e) {
if (_this.adding) { if (_this.adding) {
return; return;
} }
...@@ -85,16 +86,16 @@ morpheus.TabManager = function(options) { ...@@ -85,16 +86,16 @@ morpheus.TabManager = function(options) {
_this.activeTabId = $(e.target).attr('href'); _this.activeTabId = $(e.target).attr('href');
_this.activeTabObject = _this.idToTabObject.get(_this.activeTabId); _this.activeTabObject = _this.idToTabObject.get(_this.activeTabId);
_this.trigger('change', { _this.trigger('change', {
tab : _this.activeTabObject, tab: _this.activeTabObject,
previous : previous previous: previous
}); });
}); });
}; };
morpheus.TabManager.prototype = { morpheus.TabManager.prototype = {
setTabText : function(id, text) { setTabText: function (id, text) {
this.$nav.find('a').filter('[href=' + id + ']').contents().first() this.$nav.find('a').filter('[href=' + id + ']').contents().first()
.replaceWith(text + '&nbsp;'); .replaceWith(text + '&nbsp;');
this.idToTabObject.get(id).setName(name); this.idToTabObject.get(id).setName(name);
}, },
/** /**
...@@ -107,7 +108,7 @@ morpheus.TabManager.prototype = { ...@@ -107,7 +108,7 @@ morpheus.TabManager.prototype = {
* @param task.tabId * @param task.tabId
* Tab id for task * Tab id for task
*/ */
addTask : function(task) { addTask: function (task) {
var $a = this.$nav.find('a[href=' + task.tabId + ']'); var $a = this.$nav.find('a[href=' + task.tabId + ']');
if ($a.length === 0) { if ($a.length === 0) {
throw new Error(task.tabId + ' not found.'); throw new Error(task.tabId + ' not found.');
...@@ -121,17 +122,17 @@ morpheus.TabManager.prototype = { ...@@ -121,17 +122,17 @@ morpheus.TabManager.prototype = {
tasks.push(task); tasks.push(task);
$i.data('tasks', tasks); $i.data('tasks', tasks);
$a $a
.removeClass('animated flash') .removeClass('animated flash')
.addClass('animated flash') .addClass('animated flash')
.one( .one(
'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
function() { function () {
$(this).removeClass('animated flash'); $(this).removeClass('animated flash');
}); });
$i.addClass('fa fa-spinner fa-spin'); $i.addClass('fa fa-spinner fa-spin');
}, },
removeTask : function(task) { removeTask: function (task) {
var $a = this.$nav.find('a[href=' + task.tabId + ']'); var $a = this.$nav.find('a[href=' + task.tabId + ']');
var $i = $a.find('i'); var $i = $a.find('i');
var tasks = $i.data('tasks'); var tasks = $i.data('tasks');
...@@ -146,9 +147,9 @@ morpheus.TabManager.prototype = { ...@@ -146,9 +147,9 @@ morpheus.TabManager.prototype = {
} }
} }
if (index === -1) { if (index === -1) {
throw new Error(task.id + ' not found in ' + tasks.map(function(t) { throw new Error(task.id + ' not found in ' + tasks.map(function (t) {
return t.id; return t.id;
})); }));
} }
tasks.splice(index, 1); tasks.splice(index, 1);
$i.data('tasks', tasks); $i.data('tasks', tasks);
...@@ -156,18 +157,18 @@ morpheus.TabManager.prototype = { ...@@ -156,18 +157,18 @@ morpheus.TabManager.prototype = {
$i.removeClass('fa fa-spinner fa-spin'); $i.removeClass('fa fa-spinner fa-spin');
} }
}, },
getWidth : function() { getWidth: function () {
return this.$nav.outerWidth() || $(window).width(); return this.$nav.outerWidth() || $(window).width();
}, },
getActiveTab : function() { getActiveTab: function () {
return this.activeTabObject; return this.activeTabObject;
}, },
getActiveTabId : function() { getActiveTabId: function () {
return this.activeTabId; return this.activeTabId;
}, },
/** /**
* *
* @param options * @param options
* @param options.$el * @param options.$el
* the tab element * the tab element
...@@ -182,30 +183,30 @@ morpheus.TabManager.prototype = { ...@@ -182,30 +183,30 @@ morpheus.TabManager.prototype = {
* triggered when true * triggered when true
* @param options.enabled * @param options.enabled
* Whether new tab is enabled * Whether new tab is enabled
* *
*/ */
add : function(options) { add: function (options) {
this.adding = true; this.adding = true;
var id = _.uniqueId('tab'); var id = _.uniqueId('tab');
this.idToTabObject.set('#' + id, options.object); this.idToTabObject.set('#' + id, options.object);
var li = []; var li = [];
li.push('<li role="presentation">'); li.push('<li role="presentation">');
li.push('<a data-morpheus-rename="' + options.rename li.push('<a data-morpheus-rename="' + options.rename
+ '" data-toggle="tab" href="#' + id + '">'); + '" data-toggle="tab" href="#' + id + '">');
li.push(options.title); li.push(options.title);
li.push('&nbsp;<i style="color:black;"></i>'); li.push('&nbsp;<i style="color:black;"></i>');
if (options.closeable) { if (options.closeable) {
li li
.push('&nbsp<button type="button" class="close" aria-label="Close" data-target="#' .push('&nbsp<button type="button" class="close" aria-label="Close" data-target="#'
+ id + id
+ '"><span aria-hidden="true">×</span></button>'); + '"><span aria-hidden="true">×</span></button>');
} }
li.push('</a></li>'); li.push('</a></li>');
var $link = $(li.join('')); var $link = $(li.join(''));
$link.appendTo(this.$nav); $link.appendTo(this.$nav);
var $panel = $('<div tabIndex="-1" style="outline:0;cursor:default;" role="tabpanel" class="tab-pane" id="' var $panel = $('<div tabIndex="-1" style="outline:0;cursor:default;" role="tabpanel" class="tab-pane" id="'
+ id + '"></div>'); + id + '"></div>');
options.$el.appendTo($panel); options.$el.appendTo($panel);
$panel.appendTo(this.$tabContent); $panel.appendTo(this.$tabContent);
if (options.enabled === false) { if (options.enabled === false) {
...@@ -222,14 +223,14 @@ morpheus.TabManager.prototype = { ...@@ -222,14 +223,14 @@ morpheus.TabManager.prototype = {
this.adding = false; this.adding = false;
if (this.options.autohideTabBar) { if (this.options.autohideTabBar) {
this.$nav.css('display', this.idToTabObject.size() > 1 ? '' this.$nav.css('display', this.idToTabObject.size() > 1 ? ''
: 'none'); : 'none');
} }
return { return {
$panel : $panel, $panel: $panel,
id : '#' + id id: '#' + id
}; };
}, },
remove : function(target) { remove: function (target) {
if (target === undefined) { if (target === undefined) {
target = this.activeTabId; target = this.activeTabId;
} }
...@@ -247,14 +248,17 @@ morpheus.TabManager.prototype = { ...@@ -247,14 +248,17 @@ morpheus.TabManager.prototype = {
$a.tab('show'); $a.tab('show');
if (this.options.autohideTabBar) { if (this.options.autohideTabBar) {
this.$nav.css('display', this.idToTabObject.size() > 1 ? '' this.$nav.css('display', this.idToTabObject.size() > 1 ? ''
: 'none'); : 'none');
} }
this.trigger('remove', {
tab: target
});
}, },
setActiveTab : function(id) { setActiveTab: function (id) {
if (id === this.activeTabId) { if (id === this.activeTabId) {
this.trigger('change', { this.trigger('change', {
tab : this.activeTabObject, tab: this.activeTabObject,
previous : null previous: null
}); });
} }
var $a = this.$nav.find('[href=' + id + ']'); var $a = this.$nav.find('[href=' + id + ']');
...@@ -265,16 +269,16 @@ morpheus.TabManager.prototype = { ...@@ -265,16 +269,16 @@ morpheus.TabManager.prototype = {
}, },
/** /**
* *
* @param id * @param id
* The tab id * The tab id
* @param title * @param title
* The title (used to show tooltip) * The title (used to show tooltip)
*/ */
setTabTitle : function(id, title) { setTabTitle: function (id, title) {
this.$nav.find('a').filter('[href=' + id + ']').attr('title', title); this.$nav.find('a').filter('[href=' + id + ']').attr('title', title);
}, },
setTabEnabled : function(id, enabled) { setTabEnabled: function (id, enabled) {
var $a = this.$nav.find('a').filter('[href=' + id + ']'); var $a = this.$nav.find('a').filter('[href=' + id + ']');
if (enabled) { if (enabled) {
$a.parent().removeClass('disabled'); $a.parent().removeClass('disabled');
...@@ -286,4 +290,4 @@ morpheus.TabManager.prototype = { ...@@ -286,4 +290,4 @@ morpheus.TabManager.prototype = {
} }
}; };
morpheus.Util.extend(morpheus.TabManager, morpheus.Events); morpheus.Util.extend(morpheus.TabManager, morpheus.Events);
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment