Skip to content

Commit

Permalink
Make performance improvement and refactoring by reducing language fil…
Browse files Browse the repository at this point in the history
…e size #10

Move language reading to a service #10
  • Loading branch information
aurimus committed Oct 8, 2014
1 parent 2d39fea commit 2d25339
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
9 changes: 5 additions & 4 deletions public/modules/core/languages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ Ruby commands to make the new file out of the original:

```
langs = File.read("languages_orig.json")
langs_json = JSON.parse(langs)
langs_iso2 = langs_json.select { |lang| lang.has_key?('iso_639_2b')}
langs = langs_iso2.collect {|l| {'key' => l['iso_639_2b'], 'name' => l['name']} }
langs = JSON.parse(langs)
langs = langs.select { |lang| lang.has_key?('iso_639_2b')}
langs = langs.collect {|lang| { lang['iso_639_2b'] => lang['name'] } }
langs = langs.reduce(Hash.new, :merge)
File.open('languages.json', 'w') { |f| f.write(langs.to_json)}
```

One liner:
`
File.open('languages.json', 'w') { |f| f.write(JSON.parse(File.read("languages_orig.json")).select { |lang| lang.has_key?('iso_639_2b')}.collect {|l| {'key' => l['iso_639_2b'], 'name' => l['name']} }.to_json)}
File.open('languages.json', 'w') { |f| f.write(JSON.parse(File.read("languages_orig.json")).select { |lang| lang.has_key?('iso_639_2b')}.collect {|l| {l['iso_639_2b'] => l['name']} }.reduce(Hash.new, :merge).to_json)}
`
2 changes: 1 addition & 1 deletion public/modules/core/languages/languages.json

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions public/modules/core/services/languages.client.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

angular.module('core').factory('Languages', [
function() {
var service = {}

service.get = function (type) {
if (type == 'array'){
var langs_arr = []
for (var code in window.languages) {
langs_arr.push({key: code, name: window.languages[code]})
}
return langs_arr;
} else if (type == 'object'){
return window.languages;
}
}

return service;
}
]);
5 changes: 3 additions & 2 deletions public/modules/users/controllers/profile.client.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

angular.module('users').controller('ProfileController', ['$scope', '$stateParams', '$state', '$location', '$log', '$modal', 'Users', 'UserProfiles', 'Authentication',
function($scope, $stateParams, $state, $location, $log, $modal, Users, UserProfiles, Authentication) {
angular.module('users').controller('ProfileController', ['$scope', '$stateParams', '$state', '$location', '$log', '$modal', 'Languages', 'Users', 'UserProfiles', 'Authentication',
function($scope, $stateParams, $state, $location, $log, $modal, Languages, Users, UserProfiles, Authentication) {

$scope.user = Authentication.user; // Currently logged in user
$scope.profile = false; // Profile to show
$scope.languages = Languages.get('object');

// We landed here from profile editor, show success message
// @todo: nice notifications https://github.com/Trustroots/trustroots/issues/24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

angular.module('users').controller('EditProfileController', ['$scope', '$modal', '$http', '$stateParams', '$state', '$location', 'Users', 'Authentication',
function($scope, $modal, $http, $stateParams, $state, $location, Users, Authentication) {
angular.module('users').controller('EditProfileController', ['$scope', '$modal', '$http', '$stateParams', '$state', '$location', 'Languages', 'Users', 'Authentication',
function($scope, $modal, $http, $stateParams, $state, $location, Languages, Users, Authentication) {
$scope.user = Authentication.user;
$scope.profile = false;
$scope.languages = window.languages;
$scope.languages = Languages.get('array');

// If user is not signed in then redirect back home
if (!$scope.user) $location.path('/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h4>Last seen</h4>
<div repeat="language in languages">
{{language}}
</div>
<div ng-click="loadLanguages()">
<div>
<ui-select multiple ng-model="user.languages">
<ui-select-match placeholder="Type in languages...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="language.key as language in languages | filter:$select.search">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h4>Last seen</h4>

<p>
<h4>Languages</h4>
...
<div ng-repeat="code in user.languages">{{languages[code]}}</div>
</p>

<p data-ng-show="hasConnectedAdditionalSocialAccounts()">
Expand Down

0 comments on commit 2d25339

Please sign in to comment.