summaryrefslogtreecommitdiffstats
path: root/apps/files/js
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-06-27 13:36:18 +0200
committerVincent Petry <pvince81@owncloud.com>2014-06-27 13:41:01 +0200
commit586b3a9683421181b7cd66aff2d3fabd0f34531e (patch)
tree387b4488859d0173f164d5f94d9b5b58ea1f8a05 /apps/files/js
parent4d6019b73fd8b1a1d8e8fecbeb129bea12feb89d (diff)
downloadnextcloud-server-586b3a9683421181b7cd66aff2d3fabd0f34531e.tar.gz
nextcloud-server-586b3a9683421181b7cd66aff2d3fabd0f34531e.zip
Sync file list with file actions
Whenever file actions are registered later, now the file lists are automatically notified. Added FileActions.addUpdateListener() to be able to receive such notifications. This removes the need for apps to manually call FileActions.display() after registering new actions. This fixes issues with race conditions when file actions are registered after the file list was already rendered.
Diffstat (limited to 'apps/files/js')
-rw-r--r--apps/files/js/app.js4
-rw-r--r--apps/files/js/fileactions.js55
-rw-r--r--apps/files/js/filelist.js22
3 files changed, 76 insertions, 5 deletions
diff --git a/apps/files/js/app.js b/apps/files/js/app.js
index 71802948a5c..45b6b6a0e16 100644
--- a/apps/files/js/app.js
+++ b/apps/files/js/app.js
@@ -32,6 +32,10 @@
// regular actions
fileActions.merge(OCA.Files.fileActions);
+ // in case apps would decide to register file actions later,
+ // replace the global object with this one
+ OCA.Files.fileActions = fileActions;
+
this.files = OCA.Files.Files;
// TODO: ideally these should be in a separate class / app (the embedded "all files" app)
diff --git a/apps/files/js/fileactions.js b/apps/files/js/fileactions.js
index 47a6ab2f04b..fc7c9ccacef 100644
--- a/apps/files/js/fileactions.js
+++ b/apps/files/js/fileactions.js
@@ -22,9 +22,51 @@
defaults: {},
icons: {},
currentFile: null,
+
+ /**
+ * List of handlers to be notified whenever a register() or
+ * setDefault() was called.
+ */
+ _updateListeners: [],
+
initialize: function() {
this.clear();
},
+
+ /**
+ * Adds an update listener to be notified whenever register()
+ * or setDefault() has been called.
+ *
+ * @param Function callback
+ */
+ addUpdateListener: function(callback) {
+ if (!_.isFunction(callback)) {
+ throw 'Argument passed to FileActions.addUpdateListener must be a function';
+ }
+ this._updateListeners.push(callback);
+ },
+
+ /**
+ * Removes an update listener.
+ *
+ * @param Function callback
+ */
+ removeUpdateListener: function(callback) {
+ if (!_.isFunction(callback)) {
+ throw 'Argument passed to FileActions.removeUpdateListener must be a function';
+ }
+ this._updateListeners = _.without(this._updateListeners, callback);
+ },
+
+ /**
+ * Notifies the registered update listeners
+ */
+ _notifyUpdateListeners: function() {
+ for (var i = 0; i < this._updateListeners.length; i++) {
+ this._updateListeners[i](this);
+ }
+ },
+
/**
* Merges the actions from the given fileActions into
* this instance.
@@ -59,15 +101,18 @@
this.actions[mime][name]['permissions'] = permissions;
this.actions[mime][name]['displayName'] = displayName;
this.icons[name] = icon;
+ this._notifyUpdateListeners();
},
clear: function() {
this.actions = {};
this.defaults = {};
this.icons = {};
this.currentFile = null;
+ this._updateListeners = [];
},
setDefault: function (mime, name) {
this.defaults[mime] = name;
+ this._notifyUpdateListeners();
},
get: function (mime, type, permissions) {
var actions = this.getActions(mime, type, permissions);
@@ -133,8 +178,7 @@
display: function (parent, triggerEvent, fileList) {
if (!fileList) {
console.warn('FileActions.display() MUST be called with a OCA.Files.FileList instance');
- // using default list instead, which could be wrong
- fileList = OCA.Files.App.fileList;
+ return;
}
this.currentFile = parent;
var self = this;
@@ -309,9 +353,10 @@
window.FileActions, mime, name, permissions, icon, action, displayName
);
};
- window.FileActions.setDefault = function (mime, name) {
- console.warn('FileActions.setDefault() is deprecated, please use OCA.Files.fileActions.setDefault() instead', mime, name);
- OCA.Files.FileActions.prototype.setDefault.call(window.FileActions, mime, name);
+ window.FileActions.display = function (parent, triggerEvent, fileList) {
+ fileList = fileList || OCA.Files.App.fileList;
+ console.warn('FileActions.display() is deprecated, please use OCA.Files.fileActions.register() which automatically redisplays actions', mime, name);
+ OCA.Files.FileActions.prototype.display.call(window.FileActions, parent, triggerEvent, fileList);
};
})();
diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js
index 55afedb2065..82aa29670df 100644
--- a/apps/files/js/filelist.js
+++ b/apps/files/js/filelist.js
@@ -168,12 +168,22 @@
this.$container.on('scroll', _.bind(this._onScroll, this));
},
+ /**
+ * Destroy / uninitialize this instance.
+ */
+ destroy: function() {
+ // TODO: also unregister other event handlers
+ this.fileActions.removeUpdateListener(this._onFileActionsUpdated);
+ },
+
_initFileActions: function(fileActions) {
this.fileActions = fileActions;
if (!this.fileActions) {
this.fileActions = new OCA.Files.FileActions();
this.fileActions.registerDefaultActions();
}
+ this._onFileActionsUpdated = _.debounce(_.bind(this._onFileActionsUpdated, this), 100);
+ this.fileActions.addUpdateListener(this._onFileActionsUpdated);
},
/**
@@ -488,6 +498,18 @@
},
/**
+ * Event handler for when file actions were updated.
+ * This will refresh the file actions on the list.
+ */
+ _onFileActionsUpdated: function() {
+ console.log('onFileActionsUpdated');
+ var self = this;
+ this.$fileList.find('tr td.filename').each(function() {
+ self.fileActions.display($(this), true, self);
+ });
+ },
+
+ /**
* Sets the files to be displayed in the list.
* This operation will re-render the list and update the summary.
* @param filesArray array of file data (map)