Skip to content

Commit

Permalink
Merge pull request #1934 from nextcloud/move-latly-used-tags-to-the-top
Browse files Browse the repository at this point in the history
Move lately used tags to the top
  • Loading branch information
MorrisJobke authored Dec 5, 2016
2 parents 7ce5303 + 8e5651c commit 8fdfb41
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function getLastModified() {
*
* @param ISystemTag $tag
*
* @return SystemTagNode
* @return SystemTagMappingNode
*/
private function makeNode(ISystemTag $tag) {
return new SystemTagMappingNode(
Expand Down
28 changes: 28 additions & 0 deletions apps/systemtags/appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

return [
'routes' => [
['name' => 'LastUsed#getLastUsedTagIds', 'url' => '/lastused', 'verb' => 'GET'],
]
];
6 changes: 6 additions & 0 deletions apps/systemtags/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
},
escapeMarkup: function(m) {
return m;
},
sortResults: function(results) {
results.sort(function(a, b) {
return OC.Util.naturalSortCompare(a.get('name'), b.get('name'));
});
return results;
}
}
};
Expand Down
35 changes: 35 additions & 0 deletions apps/systemtags/js/systemtagsfilelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @type Array.<string>
*/
_systemTagIds: [],
_lastUsedTags: [],

_clientSideSort: true,
_allowSelection: false,
Expand All @@ -58,6 +59,7 @@

var $controls = this.$el.find('#controls').empty();

_.defer(_.bind(this._getLastUsedTags, this));
this._initFilterField($controls);
},

Expand All @@ -67,7 +69,19 @@
OCA.Files.FileList.prototype.destroy.apply(this, arguments);
},

_getLastUsedTags: function() {
var self = this;
$.ajax({
type: 'GET',
url: OC.generateUrl('/apps/systemtags/lastused'),
success: function (response) {
self._lastUsedTags = response;
}
});
},

_initFilterField: function($container) {
var self = this;
this.$filterField = $('<input type="hidden" name="tags"/>');
$container.append(this.$filterField);
this.$filterField.select2({
Expand Down Expand Up @@ -112,6 +126,27 @@
return OC.SystemTags.getDescriptiveTag(tag)[0].outerHTML;
},

sortResults: function(results) {
results.sort(function(a, b) {
var aLastUsed = self._lastUsedTags.indexOf(a.id);
var bLastUsed = self._lastUsedTags.indexOf(b.id);

if (aLastUsed !== bLastUsed) {
if (bLastUsed === -1) {
return -1;
}
if (aLastUsed === -1) {
return 1;
}
return aLastUsed < bLastUsed ? -1 : 1;
}

// Both not found
return OC.Util.naturalSortCompare(a.name, b.name);
});
return results;
},

escapeMarkup: function(m) {
// prevent double markup escape
return m;
Expand Down
32 changes: 32 additions & 0 deletions apps/systemtags/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
Expand All @@ -46,6 +47,8 @@ class Listener {
protected $activityManager;
/** @var IUserSession */
protected $session;
/** @var IConfig */
protected $config;
/** @var \OCP\SystemTag\ISystemTagManager */
protected $tagManager;
/** @var \OCP\App\IAppManager */
Expand All @@ -61,6 +64,7 @@ class Listener {
* @param IGroupManager $groupManager
* @param IManager $activityManager
* @param IUserSession $session
* @param IConfig $config
* @param ISystemTagManager $tagManager
* @param IAppManager $appManager
* @param IMountProviderCollection $mountCollection
Expand All @@ -69,13 +73,15 @@ class Listener {
public function __construct(IGroupManager $groupManager,
IManager $activityManager,
IUserSession $session,
IConfig $config,
ISystemTagManager $tagManager,
IAppManager $appManager,
IMountProviderCollection $mountCollection,
IRootFolder $rootFolder) {
$this->groupManager = $groupManager;
$this->activityManager = $activityManager;
$this->session = $session;
$this->config = $config;
$this->tagManager = $tagManager;
$this->appManager = $appManager;
$this->mountCollection = $mountCollection;
Expand Down Expand Up @@ -126,6 +132,11 @@ public function event(ManagerEvent $event) {
$this->activityManager->publish($activity);
}
}


if ($actor !== '' && ($event->getEvent() === ManagerEvent::EVENT_CREATE || $event->getEvent() === ManagerEvent::EVENT_UPDATE)) {
$this->updateLastUsedTags($actor, $event->getTag());
}
}

/**
Expand Down Expand Up @@ -213,6 +224,27 @@ public function mapperEvent(MapperEvent $event) {
$this->activityManager->publish($activity);
}
}

if ($actor !== '' && $event->getEvent() === MapperEvent::EVENT_ASSIGN) {
foreach ($tags as $tag) {
$this->updateLastUsedTags($actor, $tag);
}
}
}

/**
* @param string $actor
* @param ISystemTag $tag
*/
protected function updateLastUsedTags($actor, ISystemTag $tag) {
$lastUsedTags = $this->config->getUserValue($actor, 'systemtags', 'last_used', '[]');
$lastUsedTags = json_decode($lastUsedTags, true);

array_unshift($lastUsedTags, $tag->getId());
array_unique($lastUsedTags);
$lastUsedTags = array_slice($lastUsedTags, 0, 10);

$this->config->setUserValue($actor, 'systemtags', 'last_used', json_encode($lastUsedTags));
}

/**
Expand Down
59 changes: 59 additions & 0 deletions apps/systemtags/lib/Controller/LastUsedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\SystemTags\Controller;


use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

class LastUsedController extends Controller {

/** @var IConfig */
protected $config;

/** @var IUserSession */
protected $userSession;

/**
* @param string $appName
* @param IRequest $request
* @param IConfig $config
* @param IUserSession $userSession
*/
public function __construct($appName, IRequest $request, IConfig $config, IUserSession $userSession) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
}

/**
* @NoAdminRequired
*/
public function getLastUsedTagIds() {
$lastUsed = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'systemtags', 'last_used', '[]');
$tagIds = json_decode($lastUsed, true);
return new DataResponse(array_map(function($id) { return (string) $id; }, $tagIds));
}
}
31 changes: 31 additions & 0 deletions core/js/systemtags/systemtagsinputfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

_newTag: null,

_lastUsedTags: [],

className: 'systemTagsInputFieldContainer',

template: function(data) {
Expand Down Expand Up @@ -97,6 +99,8 @@
_.defer(self._refreshSelection);
});

_.defer(_.bind(this._getLastUsedTags, this));

_.bindAll(
this,
'_refreshSelection',
Expand All @@ -108,6 +112,17 @@
);
},

_getLastUsedTags: function() {
var self = this;
$.ajax({
type: 'GET',
url: OC.generateUrl('/apps/systemtags/lastused'),
success: function (response) {
self._lastUsedTags = response;
}
});
},

/**
* Refreshes the selection, triggering a call to
* select2's initSelection
Expand Down Expand Up @@ -211,6 +226,7 @@
}, {
success: function(model) {
self._addToSelect2Selection(model.toJSON());
self._lastUsedTags.unshift(model.id);
self.trigger('select', model);
},
error: function(model, xhr) {
Expand Down Expand Up @@ -238,6 +254,7 @@
return false;
} else {
tag = this.collection.get(e.object.id);
this._lastUsedTags.unshift(tag.id);
}
this._newTag = null;
this.trigger('select', tag);
Expand Down Expand Up @@ -400,6 +417,20 @@
var aSelected = selectedItems.indexOf(a.id) >= 0;
var bSelected = selectedItems.indexOf(b.id) >= 0;
if (aSelected === bSelected) {
var aLastUsed = self._lastUsedTags.indexOf(a.id);
var bLastUsed = self._lastUsedTags.indexOf(b.id);

if (aLastUsed !== bLastUsed) {
if (bLastUsed === -1) {
return -1;
}
if (aLastUsed === -1) {
return 1;
}
return aLastUsed < bLastUsed ? -1 : 1;
}

// Both not found
return OC.Util.naturalSortCompare(a.name, b.name);
}
if (aSelected && !bSelected) {
Expand Down

0 comments on commit 8fdfb41

Please sign in to comment.