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

111 lines
3.4 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() {
2016-03-16 07:47:33 +00:00
$(document).click(function(e) {
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');
}
});
2016-03-15 10:18:45 +00:00
},
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-15 10:18:45 +00:00
this.$el.addClass('hl');
this.$el.find('.op-icon').removeClass('vh');
},
2016-03-16 07:47:33 +00:00
rmHighlight: function() {
2016-03-15 10:18:45 +00:00
this.$el.removeClass('hl');
this.$el.find('.op-icon').addClass('vh');
},
2016-03-16 07:47:33 +00:00
toggleSyncedRepos: function(e) {
var $current_icon= $(e.currentTarget).children('.dir-icon'),
$current_popover = $(e.currentTarget).next('.device-libs-popover');
$('.device-libs-popover').not($current_popover).addClass('hide');
$('.dir-icon').not($current_icon).removeClass('icon-caret-up').addClass('icon-caret-down');
$current_popover.toggleClass('hide');
if ($current_icon.hasClass('icon-caret-up')) {
$current_icon.removeClass('icon-caret-up').addClass('icon-caret-down');
} else {
$current_icon.removeClass('icon-caret-down').addClass('icon-caret-up');
2016-03-15 10:18:45 +00:00
}
2016-03-16 07:47:33 +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;
});