1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-29 00:17:18 +00:00
seahub/static/scripts/app/views/device.js

123 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-03-15 10:18:45 +00:00
define([
'jquery',
'underscore',
'backbone',
'common',
2016-03-17 03:15:28 +00:00
'moment'
], function($, _, Backbone, Common, Moment) {
2016-03-15 10:18:45 +00:00
'use strict';
var DeviceView = Backbone.View.extend({
tagName: 'tr',
template: _.template($('#device-item-tmpl').html()),
events: {
2016-03-16 07:47:33 +00:00
'mouseenter': 'highlight',
'mouseleave': 'rmHighlight',
2016-03-15 10:18:45 +00:00
'click .unlink-device': 'unlinkDevice',
2016-03-16 07:47:33 +00:00
'click .js-toggle-repos': 'toggleSyncedRepos'
2016-03-15 10:18:45 +00:00
},
initialize: function() {
},
render: function () {
var data = this.model.toJSON();
if (typeof(data['synced_repos']) == 'undefined') {
data['synced_repos'] = new Array();
}
if (data['synced_repos']) {
data['synced_repos_length'] = data['synced_repos'].length;
} else {
data['synced_repos_length'] = 0;
}
// convert to human readable time
var now = new Date(),
2016-03-17 03:15:28 +00:00
last_accessed = Moment(data['last_accessed']);
2016-03-15 10:18:45 +00:00
data['time'] = last_accessed.format('LLLL');
if (last_accessed - now > 0) {
data['time_from_now'] = gettext("Just now");
} else {
data['time_from_now'] = last_accessed.fromNow();
}
this.$el.html(this.template(data));
return this;
},
2016-03-16 07:47:33 +00:00
highlight: function() {
2016-03-21 05:24:55 +00:00
if ($('.device-libs-popover:visible').length) {
return;
}
this.$el.addClass('hl').find('.op-icon').removeClass('vh');
2016-03-15 10:18:45 +00:00
},
2016-03-16 07:47:33 +00:00
rmHighlight: function() {
2016-03-21 05:24:55 +00:00
if ($('.device-libs-popover:visible').length) {
return;
}
this.$el.removeClass('hl').find('.op-icon').addClass('vh');
},
_hidePopover: function(e) {
var view = e.data.view;
var target = e.target || event.srcElement;
if (!$('.js-toggle-repos, .device-libs-popover').is(target)) {
$('.device-libs-popover').addClass('hide');
$('.dir-icon').removeClass('icon-caret-up').addClass('icon-caret-down');
view.rmHighlight();
$(document).off('click', view._hidePopover);
}
2016-03-15 10:18:45 +00:00
},
2016-03-16 07:47:33 +00:00
toggleSyncedRepos: function(e) {
2016-03-21 05:24:55 +00:00
if (this.model.get('synced_repos') == 0) {
return false;
}
2016-03-16 07:47:33 +00:00
2016-03-21 05:24:55 +00:00
var $icon= this.$('.dir-icon'),
$popover = this.$('.device-libs-popover');
2016-03-16 07:47:33 +00:00
2016-03-21 05:24:55 +00:00
if ($popover.is(':hidden')) {
$icon.removeClass('icon-caret-up').addClass('icon-caret-down');
$popover.removeClass('hide');
$(document).on('click', { view: this }, this._hidePopover);
2016-03-16 07:47:33 +00:00
} else {
2016-03-21 05:24:55 +00:00
$icon.removeClass('icon-caret-down').addClass('icon-caret-up');
$popover.addClass('hide');
$(document).off('click', this._hidePopover);
2016-03-15 10:18:45 +00:00
}
2016-03-16 07:47:33 +00:00
2016-03-21 05:24:55 +00:00
return false;
2016-03-15 10:18:45 +00:00
},
unlinkDevice: function() {
var _this = this,
2016-03-16 07:47:33 +00:00
device_name = this.model.get('device_name');
this.model.unlink({
2016-03-15 10:18:45 +00:00
success: function() {
_this.remove();
2016-03-16 07:47:33 +00:00
var msg = gettext("Successfully unlink %(name)s.")
.replace('%(name)s', Common.HTMLescape(device_name));
Common.feedback(msg, 'success');
2016-03-15 10:18:45 +00:00
},
2016-03-16 07:47:33 +00:00
error: function(xhr) {
2016-03-15 10:18:45 +00:00
Common.ajaxErrorHandler(xhr);
}
});
2016-03-16 07:47:33 +00:00
return false;
2016-03-15 10:18:45 +00:00
}
});
return DeviceView;
});