GUACAMOLE-526: Merge use toArray as necessary for orderBy.

This commit is contained in:
Nick Couchman
2018-04-30 17:40:15 -04:00
4 changed files with 51 additions and 9 deletions

View File

@@ -64,9 +64,9 @@
translate="CLIENT.HELP_SHARE_LINK" translate="CLIENT.HELP_SHARE_LINK"
translate-values="{LINKS : getShareLinkCount()}"></p> translate-values="{LINKS : getShareLinkCount()}"></p>
<table> <table>
<tr ng-repeat="link in client.shareLinks | orderBy: name"> <tr ng-repeat="link in client.shareLinks | toArray | orderBy: value.name">
<th>{{link.name}}</th> <th>{{link.value.name}}</th>
<td><a href="{{link.href}}" target="_blank">{{link.href}}</a></td> <td><a href="{{link.value.href}}" target="_blank">{{link.value.href}}</a></td>
</tr> </tr>
</table> </table>
</div> </div>

View File

@@ -9,6 +9,6 @@
<select class="time-zone" <select class="time-zone"
ng-disabled="!region" ng-disabled="!region"
ng-model="model" ng-model="model"
ng-options="name for (name, value) in timeZones[region] | orderBy: name"></select> ng-options="timeZone.value as timeZone.key for timeZone in timeZones[region] | toArray | orderBy: key"></select>
</div> </div>

View File

@@ -19,20 +19,62 @@
/** /**
* A filter for transforming an object into an array of all non-inherited * A filter for transforming an object into an array of all non-inherited
* property values. * property key/value pairs. The resulting array contains one object for each
* property in the original object, where the "key" property contains the
* original property key and the "value" property contains the original
* property value.
*/ */
angular.module('index').filter('toArray', [function toArrayFactory() { angular.module('index').filter('toArray', [function toArrayFactory() {
return function toArrayFiter(input) { /**
* The name of the property to use to store the cached result of converting
* an object to an array. This property is added to each object converted,
* such that the same array is returned each time unless the original
* object has changed.
*
* @type String
*/
var CACHE_KEY = '_guac_toArray';
return function toArrayFilter(input) {
// If no object is available, just return an empty array // If no object is available, just return an empty array
if (!input) { if (!input) {
return []; return [];
} }
return Object.keys(input).map(function fetchValueByKey(key) { // Translate object into array of key/value pairs
return input[key]; var array = [];
angular.forEach(input, function fetchValueByKey(value, key) {
array.push({
key : key,
value : value
});
}); });
// Sort consistently by key
array.sort(function compareKeys(a, b) {
if (a.key < b.key) return -1;
if (a.key > b.key) return 1;
return 0;
});
// Define non-enumerable property for holding cached array
if (!input[CACHE_KEY]) {
Object.defineProperty(input, CACHE_KEY, {
value : [],
enumerable : false,
configurable : true,
writable : true
});
}
// Update cache if resulting array is different
if (!angular.equals(input[CACHE_KEY], array))
input[CACHE_KEY] = array;
return input[CACHE_KEY];
}; };
}]); }]);

View File

@@ -32,7 +32,7 @@
<tr> <tr>
<th>{{'MANAGE_CONNECTION.FIELD_HEADER_PROTOCOL' | translate}}</th> <th>{{'MANAGE_CONNECTION.FIELD_HEADER_PROTOCOL' | translate}}</th>
<td> <td>
<select ng-model="connection.protocol" ng-options="protocol.name as getProtocolName(protocol.name) | translate for protocol in protocols | toArray | orderBy: name"></select> <select ng-model="connection.protocol" ng-options="protocol.value.name as getProtocolName(protocol.value.name) | translate for protocol in protocols | toArray | orderBy: value.name"></select>
</td> </td>
</tr> </tr>
</table> </table>