1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-30 08:53:49 +00:00
seahub/static/scripts/app/views/side-nav.js

209 lines
6.9 KiB
JavaScript
Raw Normal View History

2015-11-21 09:11:24 +00:00
define([
'jquery',
'underscore',
'backbone',
2016-04-23 05:56:46 +00:00
'common',
'js.cookie'
], function($, _, Backbone, Common, Cookie) {
2015-11-21 09:11:24 +00:00
'use strict';
var sideNavView = Backbone.View.extend({
el: '#side-nav',
template: _.template($("#side-nav-tmpl").html()),
enableModTemplate: _.template($("#myhome-mods-enable-form-tmpl").html()),
initialize: function() {
this.default_cur_tab = 'mine';
this.group_expanded = false;
if (Cookie.get('group_expanded') &&
Cookie.get('group_expanded') == 'true') {
this.group_expanded = true;
}
2015-11-21 09:11:24 +00:00
this.data = {
'cur_tab': this.default_cur_tab,
'show_group_list': this.group_expanded, // when cur_tab is not 'group'
'groups': app.pageOptions.groups,
2015-11-24 04:10:17 +00:00
'mods_enabled': app.pageOptions.user_mods_enabled,
2016-08-10 06:22:04 +00:00
'can_add_repo': app.pageOptions.can_add_repo
2015-11-21 09:11:24 +00:00
};
var _this = this;
$('#js-toggle-side-nav').click(function() {
_this.show();
return false;
});
$(window).resize(function() {
if ($(window).width() >= 768) {
_this.show();
}
});
2015-11-21 09:11:24 +00:00
},
render: function() {
this.$el.html(this.template(this.data));
return this;
},
events: {
'click #group-nav a:first': 'toggleGroupList',
'click #enable-mods': 'enableMods',
'click .js-close-side-nav': 'closeNav',
2016-05-07 09:01:31 +00:00
'click li > a': 'visitLink',
'click .js-about': 'showAbout'
2015-11-21 09:11:24 +00:00
},
2016-04-23 05:56:46 +00:00
toggleGroupList: function() {
var $icon = $('#group-nav .toggle-icon');
$icon.toggleClass('icon-caret-left icon-caret-down');
2015-11-21 09:11:24 +00:00
$('#group-nav .grp-list').slideToggle();
if ($icon.hasClass('icon-caret-down')) {
Cookie.set('group_expanded', 'true');
2016-05-20 02:56:39 +00:00
this.data.show_group_list = true;
2016-04-23 05:56:46 +00:00
} else {
Cookie.set('group_expanded', 'false');
2016-05-20 02:56:39 +00:00
this.data.show_group_list = false;
2016-04-23 05:56:46 +00:00
}
2015-11-21 09:11:24 +00:00
return false;
},
enableMods: function () {
var mods_enabled = app.pageOptions.user_mods_enabled;
var form = $(this.enableModTemplate({
'mods_available': app.pageOptions.user_mods_available,
'mods_enabled': mods_enabled
}));
2016-04-12 03:17:27 +00:00
form.modal({focus:false});
2015-11-21 09:11:24 +00:00
$('#simplemodal-container').css('height', 'auto');
$('.checkbox-orig', form).click(function() {
$(this).parent().toggleClass('checkbox-checked');
});
var checkbox = $('[name="personal_wiki"]'),
original_checked = checkbox.prop('checked'),
_this = this;
form.submit(function() {
var cur_checked = checkbox.prop('checked');
if (cur_checked == original_checked) {
return false;
}
Common.ajaxPost({
form: form,
form_id: form.attr('id'),
post_url: Common.getUrl({
'name': 'toggle_personal_modules'
}),
post_data: {'personal_wiki': cur_checked },
after_op_success: function () {
if (cur_checked) {
mods_enabled.push('personal wiki');
} else {
var index = mods_enabled.indexOf('personal wiki');
if (index > -1) {
mods_enabled.splice(index, 1); // rm the item
}
}
$.modal.close();
_this.render();
}
});
return false;
});
2016-04-12 03:17:27 +00:00
return false;
2015-11-21 09:11:24 +00:00
},
setCurTab: function(cur_tab, options) {
2015-11-21 09:11:24 +00:00
this.data.cur_tab = cur_tab || this.default_cur_tab;
if (options) {
$.extend(this.data, options);
}
2016-05-18 06:48:23 +00:00
2016-05-19 09:46:08 +00:00
if (this.$clickedTab) {
// The user click a link and this.$clickedTab is set by visitLink()
this.$('.tab-cur').removeClass('tab-cur');
this.$clickedTab.addClass('tab-cur');
this.$clickedTab = null;
} else {
// the first time the side nav is rendered or the side nav is re-rendered
// when dismiss a group, leave a group
this.render();
var curTabTop = this.$('.tab-cur').offset().top;
var visibleHeight = $(window).height() - this.$('.side-nav-footer').outerHeight(true);
if (curTabTop > visibleHeight) {
this.$('.side-nav-con').css({'overflow':'auto'}).scrollTop(curTabTop - visibleHeight + this.$('.tab-cur').outerHeight(true) + 10).removeAttr('style');
}
2016-05-18 06:48:23 +00:00
}
},
updateGroups: function() {
var _this = this;
$.ajax({
url: Common.getUrl({name: 'groups'}),
type: 'GET',
dataType: 'json',
cache: false,
success: function(data) {
var groups = [];
for (var i = 0, len = data.length; i < len; i++) {
groups.push({
'id': data[i].id,
'name': data[i].name
});
}
groups.sort(function(a, b) {
return Common.compareTwoWord(a.name, b.name);
});
// update app.pageOptions.groups
app.pageOptions.groups = groups;
_this.data.groups = groups;
_this.render();
},
error: function() {
}
});
},
show: function() {
this.$el.css({ 'left':'0px' });
},
hide: function() {
this.$el.css({ 'left':'-300px' });
},
closeNav: function() {
this.hide();
return false;
},
2016-05-18 06:48:23 +00:00
visitLink: function(e) {
if ($(e.target).attr('href') !== "#") {
2016-05-19 09:46:08 +00:00
this.$clickedTab = $(e.target).parent();
2016-05-18 06:48:23 +00:00
}
if ($(window).width() < 768) {
2016-05-18 06:48:23 +00:00
if ($(e.target).attr('href') !== "#") {
// except for groups toggle link
this.hide();
}
}
return true;
},
2015-11-21 09:11:24 +00:00
2016-05-07 09:01:31 +00:00
showAbout: function() {
var $about = this.$('.about-content');
$about.modal();
return false;
}
2015-11-21 09:11:24 +00:00
});
return sideNavView;
});