(js) Fix edition of mail tags

pull/237/head
Francis Lachapelle 2017-06-15 16:04:47 -04:00
parent 157d3050bf
commit d72c614d29
3 changed files with 142 additions and 147 deletions

View File

@ -78,7 +78,7 @@
<md-menu-content width="4"> <md-menu-content width="4">
<md-menu-item ng-hide="viewer.showFlags"> <md-menu-item ng-hide="viewer.showFlags">
<md-button label:aria-label="Add a tag" <md-button label:aria-label="Add a tag"
ng-click="viewer.showFlags = true"> ng-click="viewer.addFlags()">
<var:string label:value="Add a tag"/> <var:string label:value="Add a tag"/>
</md-button> </md-button>
</md-menu-item> </md-menu-item>
@ -180,6 +180,7 @@
</div> </div>
<div class="sg-padded hide-xs" ng-show="viewer.showFlags"> <div class="sg-padded hide-xs" ng-show="viewer.showFlags">
<md-chips class="sg-readonly" <md-chips class="sg-readonly"
sg-focus-on="flags"
ng-model="viewer.message.flags"> ng-model="viewer.message.flags">
<md-chip-template> <md-chip-template>
<span class="sg-chip-color"> <span class="sg-chip-color">
@ -195,10 +196,9 @@
<md-item-template> <md-item-template>
<div layout="row" layout-align="start center"> <div layout="row" layout-align="start center">
<div class="sg-color-chip" <div class="sg-color-chip"
ng-style="{'background-color': tag.color}"><!-- color --></div> ng-style="{ 'background-color': tag.color }"><!-- color --></div>
<div md-highlight-text="viewer.tags.searchText" <div md-highlight-text="viewer.tags.searchText"
md-highlight-flags="^i" md-highlight-flags="^i">{{ tag.description }}</div>
ng-bind="tag.description"><!-- description --></div>
</div> </div>
</md-item-template> </md-item-template>
</md-autocomplete> </md-autocomplete>

View File

@ -18,7 +18,8 @@
scope.$on('sgFocusOn', function(e, name) { scope.$on('sgFocusOn', function(e, name) {
if (name === attr.sgFocusOn) { if (name === attr.sgFocusOn) {
elem[0].focus(); elem[0].focus();
elem[0].select(); if (typeof elem[0].select == 'function')
elem[0].select();
} }
}); });
}; };

View File

@ -10,100 +10,89 @@
function MessageController($window, $scope, $state, $mdMedia, $mdDialog, sgConstant, stateAccounts, stateAccount, stateMailbox, stateMessage, sgHotkeys, encodeUriFilter, sgSettings, ImageGallery, focus, Dialog, Calendar, Component, Account, Mailbox, Message) { function MessageController($window, $scope, $state, $mdMedia, $mdDialog, sgConstant, stateAccounts, stateAccount, stateMailbox, stateMessage, sgHotkeys, encodeUriFilter, sgSettings, ImageGallery, focus, Dialog, Calendar, Component, Account, Mailbox, Message) {
var vm = this, popupWindow = null, hotkeys = []; var vm = this, popupWindow = null, hotkeys = [];
// Expose controller this.$onInit = function() {
$window.$messageController = vm; // Expose controller
$window.$messageController = vm;
// Initialize image gallery service // Initialize image gallery service
ImageGallery.setMessage(stateMessage); ImageGallery.setMessage(stateMessage);
vm.$state = $state; this.$state = $state;
vm.accounts = stateAccounts; this.accounts = stateAccounts;
vm.account = stateAccount; this.account = stateAccount;
vm.mailbox = stateMailbox; this.mailbox = stateMailbox;
vm.message = stateMessage; this.message = stateMessage;
vm.service = Message; this.service = Message;
vm.tags = { searchText: '', selected: '' }; this.tags = { searchText: '', selected: '' };
vm.showFlags = stateMessage.flags && stateMessage.flags.length > 0; this.showFlags = stateMessage.flags && stateMessage.flags.length > 0;
vm.$showDetailedRecipients = false; this.$showDetailedRecipients = false;
vm.toggleDetailedRecipients = toggleDetailedRecipients;
vm.filterMailtoLinks = filterMailtoLinks;
vm.deleteMessage = deleteMessage;
vm.close = close;
vm.reply = reply;
vm.replyAll = replyAll;
vm.forward = forward;
vm.edit = edit;
vm.openPopup = openPopup;
vm.closePopup = closePopup;
vm.newMessage = newMessage;
vm.toggleRawSource = toggleRawSource;
vm.showRawSource = false;
vm.print = print;
vm.convertToEvent = convertToEvent;
vm.convertToTask = convertToTask;
_registerHotkeys(hotkeys); vm.showRawSource = false;
// One-way refresh of the parent window when modifying the message from a popup window. _registerHotkeys(hotkeys);
if ($window.opener) {
// Update the message flags. The message must be displayed in the parent window. // One-way refresh of the parent window when modifying the message from a popup window.
$scope.$watchCollection(function() { return vm.message.flags; }, function(newTags, oldTags) { if ($window.opener) {
var ctrls; // Update the message flags. The message must be displayed in the parent window.
if (newTags || oldTags) { $scope.$watchCollection(function() { return vm.message.flags; }, function(newTags, oldTags) {
ctrls = $parentControllers(); var ctrls;
if (ctrls.messageCtrl) { if (newTags || oldTags) {
ctrls.messageCtrl.service.$timeout(function() { ctrls = $parentControllers();
ctrls.messageCtrl.showFlags = true; if (ctrls.messageCtrl) {
ctrls.messageCtrl.message.flags = newTags; ctrls.messageCtrl.service.$timeout(function() {
ctrls.messageCtrl.showFlags = true;
ctrls.messageCtrl.message.flags = newTags;
});
}
}
});
// Update the "isflagged" (star icon) of the message. The mailbox must be displayed in the parent window.
$scope.$watch(function() { return vm.message.isflagged; }, function(isflagged, wasflagged) {
var ctrls = $parentControllers();
if (ctrls.mailboxCtrl) {
ctrls.mailboxCtrl.service.$timeout(function() {
var message = _.find(ctrls.mailboxCtrl.selectedFolder.$messages, { uid: vm.message.uid });
message.isflagged = isflagged;
}); });
} }
} });
}); }
// Update the "isflagged" (star icon) of the message. The mailbox must be displayed in the parent window. else {
$scope.$watch(function() { return vm.message.isflagged; }, function(isflagged, wasflagged) { // Flatten new tags when coming from the predefined list of tags (Message.$tags) and
var ctrls = $parentControllers(); // sync tags with server when adding or removing a tag.
if (ctrls.mailboxCtrl) { $scope.$watchCollection(function() { return vm.message.flags; }, function(_newTags, _oldTags) {
ctrls.mailboxCtrl.service.$timeout(function() { var newTags, oldTags, tags;
var message = _.find(ctrls.mailboxCtrl.selectedFolder.$messages, { uid: vm.message.uid }); if (_newTags || _oldTags) {
message.isflagged = isflagged; newTags = _newTags || [];
}); oldTags = _oldTags || [];
} _.forEach(newTags, function(tag, i) {
}); if (angular.isObject(tag))
} newTags[i] = tag.name;
else {
// Flatten new tags when coming from the predefined list of tags (Message.$tags) and
// sync tags with server when adding or removing a tag.
$scope.$watchCollection(function() { return vm.message.flags; }, function(_newTags, _oldTags) {
var newTags, oldTags, tags;
if (_newTags || _oldTags) {
newTags = _newTags || [];
oldTags = _oldTags || [];
_.forEach(newTags, function(tag, i) {
if (angular.isObject(tag))
newTags[i] = tag.name;
});
if (newTags.length > oldTags.length) {
tags = _.difference(newTags, oldTags);
_.forEach(tags, function(tag) {
vm.message.addTag(tag);
}); });
if (newTags.length > oldTags.length) {
tags = _.difference(newTags, oldTags);
_.forEach(tags, function(tag) {
vm.message.addTag(tag);
});
}
else if (newTags.length < oldTags.length) {
tags = _.difference(oldTags, newTags);
_.forEach(tags, function(tag) {
vm.message.removeTag(tag);
});
}
} }
else if (newTags.length < oldTags.length) { });
tags = _.difference(oldTags, newTags); }
_.forEach(tags, function(tag) {
vm.message.removeTag(tag);
});
}
}
});
}
$scope.$on('$destroy', function() { $scope.$on('$destroy', function() {
// Deregister hotkeys // Deregister hotkeys
_.forEach(hotkeys, function(key) { _.forEach(hotkeys, function(key) {
sgHotkeys.deregisterHotkey(key); sgHotkeys.deregisterHotkey(key);
});
}); });
});
}; // $onInit
/** /**
@ -130,17 +119,17 @@
keys.push(sgHotkeys.createHotkey({ keys.push(sgHotkeys.createHotkey({
key: l('hotkey_reply'), key: l('hotkey_reply'),
description: l('Reply to the message'), description: l('Reply to the message'),
callback: _unlessInDialog(reply) callback: _unlessInDialog(angular.bind(vm, vm.reply))
})); }));
keys.push(sgHotkeys.createHotkey({ keys.push(sgHotkeys.createHotkey({
key: l('hotkey_replyall'), key: l('hotkey_replyall'),
description: l('Reply to sender and all recipients'), description: l('Reply to sender and all recipients'),
callback: _unlessInDialog(replyAll) callback: _unlessInDialog(angular.bind(vm, vm.replyAll))
})); }));
keys.push(sgHotkeys.createHotkey({ keys.push(sgHotkeys.createHotkey({
key: l('hotkey_forward'), key: l('hotkey_forward'),
description: l('Forward selected message'), description: l('Forward selected message'),
callback: _unlessInDialog(forward) callback: _unlessInDialog(angular.bind(vm, vm.forward))
})); }));
keys.push(sgHotkeys.createHotkey({ keys.push(sgHotkeys.createHotkey({
key: l('hotkey_flag'), key: l('hotkey_flag'),
@ -152,7 +141,7 @@
key: hotkey, key: hotkey,
callback: _unlessInDialog(function($event) { callback: _unlessInDialog(function($event) {
if (vm.mailbox.$selectedCount() === 0) if (vm.mailbox.$selectedCount() === 0)
deleteMessage(); vm.deleteMessage();
$event.preventDefault(); $event.preventDefault();
}), }),
})); }));
@ -187,13 +176,18 @@
return ctrls; return ctrls;
} }
function toggleDetailedRecipients($event) { this.addFlags = function($event) {
vm.$showDetailedRecipients = !vm.$showDetailedRecipients; this.showFlags = true;
focus("flags");
};
this.toggleDetailedRecipients = function($event) {
this.$showDetailedRecipients = !this.$showDetailedRecipients;
$event.stopPropagation(); $event.stopPropagation();
$event.preventDefault(); $event.preventDefault();
} };
function filterMailtoLinks($event) { this.filterMailtoLinks = function($event) {
var href, match, to, cc, bcc, subject, body, data; var href, match, to, cc, bcc, subject, body, data;
if ($event.target.tagName == 'A' && 'href' in $event.target.attributes) { if ($event.target.tagName == 'A' && 'href' in $event.target.attributes) {
href = $event.target.attributes.href.value; href = $event.target.attributes.href.value;
@ -222,9 +216,9 @@
newMessage($event, data); // will stop event propagation newMessage($event, data); // will stop event propagation
} }
} }
} };
function deleteMessage() { this.deleteMessage = function() {
var mailbox, message, state, nextMessage, previousMessage, var mailbox, message, state, nextMessage, previousMessage,
parentCtrls = $parentControllers(); parentCtrls = $parentControllers();
@ -280,11 +274,11 @@
} }
catch (error) {} catch (error) {}
} }
closePopup(); vm.closePopup();
}); });
} };
function showMailEditor($event, message) { function _showMailEditor($event, message) {
if (_messageDialog() === null) { if (_messageDialog() === null) {
_messageDialog( _messageDialog(
$mdDialog $mdDialog
@ -303,49 +297,49 @@
}) })
.finally(function() { .finally(function() {
_messageDialog(null); _messageDialog(null);
closePopup(); vm.closePopup();
}) })
); );
} }
} }
function close() { this.close = function() {
$state.go('mail.account.mailbox').then(function() { $state.go('mail.account.mailbox').then(function() {
vm.message = null; vm.message = null;
delete stateMailbox.selectedMessage; delete stateMailbox.selectedMessage;
}); });
} };
function reply($event) { this.reply = function($event) {
var message = vm.message.$reply(); var message = this.message.$reply();
showMailEditor($event, message); _showMailEditor($event, message);
} };
function replyAll($event) { this.replyAll = function($event) {
var message = vm.message.$replyAll(); var message = this.message.$replyAll();
showMailEditor($event, message); _showMailEditor($event, message);
} };
function forward($event) { this.forward = function($event) {
var message = vm.message.$forward(); var message = this.message.$forward();
showMailEditor($event, message); _showMailEditor($event, message);
} };
function edit($event) { this.edit = function($event) {
vm.message.$editableContent().then(function() { this.message.$editableContent().then(function() {
showMailEditor($event, vm.message); _showMailEditor($event, vm.message);
}); });
} };
function openPopup() { this.openPopup = function() {
var url = [sgSettings.baseURL(), var url = [sgSettings.baseURL(),
'UIxMailPopupView#!/Mail', 'UIxMailPopupView#!/Mail',
vm.message.accountId, this.message.accountId,
// The double-encoding is necessary // The double-encoding is necessary
encodeUriFilter(encodeUriFilter(vm.message.$mailbox.path)), encodeUriFilter(encodeUriFilter(this.message.$mailbox.path)),
vm.message.uid] this.message.uid]
.join('/'), .join('/'),
wId = vm.message.$absolutePath(); wId = this.message.$absolutePath();
popupWindow = $window.open(url, wId, popupWindow = $window.open(url, wId,
["width=680", ["width=680",
"height=520", "height=520",
@ -358,47 +352,47 @@
"menubar=0", "menubar=0",
"copyhistory=0"] "copyhistory=0"]
.join(',')); .join(','));
} };
function closePopup() { this.closePopup = function() {
if ($window.opener) if ($window.opener)
$window.close(); $window.close();
} };
function newMessage($event, editableContent) { this.newMessage = function($event, editableContent) {
vm.account.$newMessage().then(function(message) { this.account.$newMessage().then(function(message) {
angular.extend(message.editable, editableContent); angular.extend(message.editable, editableContent);
showMailEditor($event, message); _showMailEditor($event, message);
}); });
$event.stopPropagation(); $event.stopPropagation();
$event.preventDefault(); $event.preventDefault();
} };
function toggleRawSource($event) { this.toggleRawSource = function($event) {
if (!vm.showRawSource && !vm.message.$rawSource) { if (!this.showRawSource && !this.message.$rawSource) {
Message.$$resource.post(vm.message.id, "viewsource").then(function(data) { Message.$$resource.post(this.message.id, "viewsource").then(function(data) {
vm.message.$rawSource = data; vm.message.$rawSource = data;
vm.showRawSource = true; vm.showRawSource = true;
}); });
} }
else { else {
vm.showRawSource = !vm.showRawSource; this.showRawSource = !this.showRawSource;
} }
} };
function print($event) { this.print = function($event) {
$window.print(); $window.print();
} };
function convertToEvent($event) { this.convertToEvent = function($event) {
return convertToComponent($event, 'appointment'); return _convertToComponent($event, 'appointment');
} };
function convertToTask($event) { this.convertToTask = function($event) {
return convertToComponent($event, 'task'); return _convertToComponent($event, 'task');
} };
function convertToComponent($event, type) { function _convertToComponent($event, type) {
vm.message.$plainContent().then(function(data) { vm.message.$plainContent().then(function(data) {
var componentData = { var componentData = {
pid: Calendar.$defaultCalendar(), pid: Calendar.$defaultCalendar(),