{"action":"created","comment":{"url":"https://api.github.com/repos/guardian/grid/pulls/comments/39968866","id":39968866,"diff_hunk":"@@ -6,125 +6,112 @@ import './usage-rights-editor.css!';\n \n import '../components/gr-confirm-delete/gr-confirm-delete.js';\n \n+\n+import Rx from 'rx';\n+import '../util/rx';\n+\n export var usageRightsEditor = angular.module('kahuna.edits.usageRightsEditor', [\n     'monospaced.elastic',\n-    'gr.confirmDelete'\n+    'gr.confirmDelete',\n+    'util.rx'\n ]);\n \n usageRightsEditor.controller(\n     'UsageRightsEditorCtrl',\n-    ['$q', '$scope', '$window', '$timeout', 'editsService', 'editsApi', 'onValChange',\n-    function($q, $scope, $window, $timeout, editsService, editsApi, onValChange) {\n+    ['$q', '$scope', 'inject$', 'observe$', 'editsService', 'editsApi',\n+    function($q, $scope, inject$, observe$, editsService, editsApi) {\n \n     var ctrl = this;\n+    const multiCat = { name: 'Multiple categories', value: 'multi-cat', properties: [] };\n \n-    ctrl.saving = false;\n-    ctrl.saved = false;\n-    ctrl.categories = [];\n-    ctrl.originalCats = [];\n-    ctrl.model = angular.extend({}, ctrl.usageRights.data);\n-\n-    const multiRights = { name: 'Multiple categories', value: '' };\n-    const noRights = { name: 'None', value: '' };\n-    const catsWithNoRights = () => [noRights].concat(ctrl.originalCats);\n-    const catsWithMultiRights = () => [multiRights].concat(ctrl.originalCats);\n-    const setStandardCats = () => ctrl.categories = ctrl.originalCats;\n-    const setNoRightsCats = () => {\n-        ctrl.categories = catsWithNoRights();\n-        ctrl.category = noRights;\n-    };\n-    const setMultiRightsCats = () => {\n-        ctrl.categories = catsWithMultiRights();\n-        ctrl.category = multiRights;\n-    };\n-\n+    // @return Stream.<Array.<UsageRights>>\n+    const usageRights$ = observe$($scope, () => ctrl.usageRights).startWith([]);\n \n-    const getGroupCategory =\n-        (usageRights, cats) => cats.find(cat => cat.value === usageRights.reduce(\n-            (m, o) => (m == o.data.category) ? o.data.category : {},\n-            usageRights[0].data.category\n-        ));\n+    // @return Stream.<Array.<Category>>\n+    const categories$ = Rx.Observable.fromPromise(editsApi.getUsageRightsCategories());\n \n-    const getGroupModel = usageRights =>\n-        ctrl.multipleUsageRights() ? {} : angular.extend({}, usageRights[0].data);\n-\n-    ctrl.reset = () => {\n-        ctrl.showRestrictions = false;\n-        ctrl.model = {};\n-    };\n-    ctrl.multipleUsageRights = () => ctrl.usageRights.length > 1;\n-\n-    ctrl.update = function() {\n-        ctrl.category = getGroupCategory(ctrl.usageRights, ctrl.categories);\n-\n-        // If we have multi or no rights we need to add the option to\n-        // the drop down and select it to give the user feedback as to\n-        // what's going on (can't use terner).\n-        if (!ctrl.category) {\n-            if (ctrl.usageRights.length > 1) {\n-                setMultiRightsCats();\n-            } else {\n-                setNoRightsCats();\n-            }\n+    // @return Stream.<Array.<Category>>\n+    const displayCategories$ = usageRights$.combineLatest(categories$, (urs, cats) => {\n+        const uniqueCats = getUniqueCats(urs);\n+        if (uniqueCats.length === 1) {\n+            return cats;\n         } else {\n-            setStandardCats();\n+            return [multiCat].concat(cats);\n         }\n+    });\n \n+    // @return Stream.<Category>\n+    const categoryFromUsageRights$ = usageRights$.combineLatest(categories$, (urs, cats) => {\n+        const uniqueCats = getUniqueCats(urs);\n+        if (uniqueCats.length === 1) {\n+            const uniqeCat = uniqueCats[0] || '';\n+            return cats.find(cat => cat.value === uniqeCat);\n+        } else {\n+            return multiCat;\n+        }\n+    });\n \n-        ctrl.model = getGroupModel(ctrl.usageRights);\n-        ctrl.showRestrictions = angular.isDefined(ctrl.model.restrictions);\n-    };\n-\n-    // setting our initial values\n-    editsApi.getUsageRightsCategories().then(cats => {\n-        const categoriesCopy = angular.copy(cats);\n+    // @return Stream.<Category>\n+    const categoryChange$ = observe$($scope, () => ctrl.category).filter(cat => !!cat);\n \n-        categoriesCopy.forEach(cat => {\n-            cat.properties.forEach((property, i) => {\n-                let propertyOptions = property.required ? [] : [{key: 'None', value: null}];\n+    // @return Stream.<Category>\n+    const category$ = categoryFromUsageRights$.merge(categoryChange$).distinctUntilChanged();\n \n-                if (property.options) {\n-                    property.options.forEach(option => {\n-                        propertyOptions.push({key: option, value: option});\n-                    });\n+    const model$ = usageRights$.map(urs => {\n+        const multiModel = reduceObjectsToArrays(urs.map(ur => ur.data));\n+        return Object.keys(multiModel).reduce((model, key) => {\n+            if (unique(multiModel[key]).length === 1) {\n+                model[key] = multiModel[key][0];\n+            }\n+            return model;\n+        }, {});\n+    });\n \n-                    cat.properties[i].options = propertyOptions;\n-                }\n-            });\n-        });\n+    const savingDisabled$ = category$.combineLatest(category$, cat => cat === multiCat);","path":"kahuna/public/js/usage-rights/usage-rights-editor.js","position":133,"original_position":133,"commit_id":"6ed353eb43b5d1c278565aa28238d324c60eaace","original_commit_id":"6ed353eb43b5d1c278565aa28238d324c60eaace","user":{"login":"theefer","id":36964,"avatar_url":"https://avatars.githubusercontent.com/u/36964?v=3","gravatar_id":"","url":"https://api.github.com/users/theefer","html_url":"https://github.com/theefer","followers_url":"https://api.github.com/users/theefer/followers","following_url":"https://api.github.com/users/theefer/following{/other_user}","gists_url":"https://api.github.com/users/theefer/gists{/gist_id}","starred_url":"https://api.github.com/users/theefer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theefer/subscriptions","organizations_url":"https://api.github.com/users/theefer/orgs","repos_url":"https://api.github.com/users/theefer/repos","events_url":"https://api.github.com/users/theefer/events{/privacy}","received_events_url":"https://api.github.com/users/theefer/received_events","type":"User","site_admin":false},"body":"`combineLatest` with itself? Is that a `map`?","created_at":"2015-09-21T13:22:04Z","updated_at":"2015-09-21T13:22:04Z","html_url":"https://github.com/guardian/grid/pull/1277#discussion_r39968866","pull_request_url":"https://api.github.com/repos/guardian/grid/pulls/1277","_links":{"self":{"href":"https://api.github.com/repos/guardian/grid/pulls/comments/39968866"},"html":{"href":"https://github.com/guardian/grid/pull/1277#discussion_r39968866"},"pull_request":{"href":"https://api.github.com/repos/guardian/grid/pulls/1277"}}},"pull_request":{"url":"https://api.github.com/repos/guardian/grid/pulls/1277","id":45430889,"html_url":"https://github.com/guardian/grid/pull/1277","diff_url":"https://github.com/guardian/grid/pull/1277.diff","patch_url":"https://github.com/guardian/grid/pull/1277.patch","issue_url":"https://api.github.com/repos/guardian/grid/issues/1277","number":1277,"state":"open","locked":false,"title":"No usage rights","user":{"login":"jamesgorrie","id":31692,"avatar_url":"https://avatars.githubusercontent.com/u/31692?v=3","gravatar_id":"","url":"https://api.github.com/users/jamesgorrie","html_url":"https://github.com/jamesgorrie","followers_url":"https://api.github.com/users/jamesgorrie/followers","following_url":"https://api.github.com/users/jamesgorrie/following{/other_user}","gists_url":"https://api.github.com/users/jamesgorrie/gists{/gist_id}","starred_url":"https://api.github.com/users/jamesgorrie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamesgorrie/subscriptions","organizations_url":"https://api.github.com/users/jamesgorrie/orgs","repos_url":"https://api.github.com/users/jamesgorrie/repos","events_url":"https://api.github.com/users/jamesgorrie/events{/privacy}","received_events_url":"https://api.github.com/users/jamesgorrie/received_events","type":"User","site_admin":false},"body":"When embarking on doing this, I was left trying to figure out what changed what state, which is a little frustrating.\r\n\r\nThis hopefully makes things a lot more understandable and leaves us able to enhance this in the future.\r\n\r\nWeirdly I noticed that I am doing things similar to what's being done in [`gr-panel`](https://github.com/guardian/grid/blob/master/kahuna/public/js/components/gr-panel/gr-panel.js#L108-L135). Hopefully these will bring us closer to one abstraction to manage this (and coming to the same solution twice is mostly a good sign).\r\n\r\nAnother addition is we can now have a more correct model i.e. If two images are of different categories, but have they same restrictions - this will be reflected.\r\n\r\n__TODO__\r\n- [x] Make sure we have feature parity\r\n- [x] remove legacy code\r\n\r\n","created_at":"2015-09-18T17:22:11Z","updated_at":"2015-09-21T13:22:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3aeb1a4c28500ee0f6d7684de624d4797edf5c4b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/guardian/grid/pulls/1277/commits","review_comments_url":"https://api.github.com/repos/guardian/grid/pulls/1277/comments","review_comment_url":"https://api.github.com/repos/guardian/grid/pulls/comments{/number}","comments_url":"https://api.github.com/repos/guardian/grid/issues/1277/comments","statuses_url":"https://api.github.com/repos/guardian/grid/statuses/6ed353eb43b5d1c278565aa28238d324c60eaace","head":{"label":"guardian:nousagerights","ref":"nousagerights","sha":"6ed353eb43b5d1c278565aa28238d324c60eaace","user":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"repo":{"id":12298112,"name":"grid","full_name":"guardian/grid","owner":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/guardian/grid","description":"The Guardian’s new image management system","fork":false,"url":"https://api.github.com/repos/guardian/grid","forks_url":"https://api.github.com/repos/guardian/grid/forks","keys_url":"https://api.github.com/repos/guardian/grid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/guardian/grid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/guardian/grid/teams","hooks_url":"https://api.github.com/repos/guardian/grid/hooks","issue_events_url":"https://api.github.com/repos/guardian/grid/issues/events{/number}","events_url":"https://api.github.com/repos/guardian/grid/events","assignees_url":"https://api.github.com/repos/guardian/grid/assignees{/user}","branches_url":"https://api.github.com/repos/guardian/grid/branches{/branch}","tags_url":"https://api.github.com/repos/guardian/grid/tags","blobs_url":"https://api.github.com/repos/guardian/grid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/guardian/grid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/guardian/grid/git/refs{/sha}","trees_url":"https://api.github.com/repos/guardian/grid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/guardian/grid/statuses/{sha}","languages_url":"https://api.github.com/repos/guardian/grid/languages","stargazers_url":"https://api.github.com/repos/guardian/grid/stargazers","contributors_url":"https://api.github.com/repos/guardian/grid/contributors","subscribers_url":"https://api.github.com/repos/guardian/grid/subscribers","subscription_url":"https://api.github.com/repos/guardian/grid/subscription","commits_url":"https://api.github.com/repos/guardian/grid/commits{/sha}","git_commits_url":"https://api.github.com/repos/guardian/grid/git/commits{/sha}","comments_url":"https://api.github.com/repos/guardian/grid/comments{/number}","issue_comment_url":"https://api.github.com/repos/guardian/grid/issues/comments{/number}","contents_url":"https://api.github.com/repos/guardian/grid/contents/{+path}","compare_url":"https://api.github.com/repos/guardian/grid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/guardian/grid/merges","archive_url":"https://api.github.com/repos/guardian/grid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/guardian/grid/downloads","issues_url":"https://api.github.com/repos/guardian/grid/issues{/number}","pulls_url":"https://api.github.com/repos/guardian/grid/pulls{/number}","milestones_url":"https://api.github.com/repos/guardian/grid/milestones{/number}","notifications_url":"https://api.github.com/repos/guardian/grid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/guardian/grid/labels{/name}","releases_url":"https://api.github.com/repos/guardian/grid/releases{/id}","created_at":"2013-08-22T13:49:31Z","updated_at":"2015-09-21T04:45:13Z","pushed_at":"2015-09-21T13:07:04Z","git_url":"git://github.com/guardian/grid.git","ssh_url":"git@github.com:guardian/grid.git","clone_url":"https://github.com/guardian/grid.git","svn_url":"https://github.com/guardian/grid","homepage":"","size":22644,"stargazers_count":918,"watchers_count":918,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":43,"mirror_url":null,"open_issues_count":63,"forks":43,"open_issues":63,"watchers":918,"default_branch":"master"}},"base":{"label":"guardian:master","ref":"master","sha":"4bc0a61dd8f3d2f3550d99a8898dd73a81e93502","user":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"repo":{"id":12298112,"name":"grid","full_name":"guardian/grid","owner":{"login":"guardian","id":164318,"avatar_url":"https://avatars.githubusercontent.com/u/164318?v=3","gravatar_id":"","url":"https://api.github.com/users/guardian","html_url":"https://github.com/guardian","followers_url":"https://api.github.com/users/guardian/followers","following_url":"https://api.github.com/users/guardian/following{/other_user}","gists_url":"https://api.github.com/users/guardian/gists{/gist_id}","starred_url":"https://api.github.com/users/guardian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guardian/subscriptions","organizations_url":"https://api.github.com/users/guardian/orgs","repos_url":"https://api.github.com/users/guardian/repos","events_url":"https://api.github.com/users/guardian/events{/privacy}","received_events_url":"https://api.github.com/users/guardian/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/guardian/grid","description":"The Guardian’s new image management system","fork":false,"url":"https://api.github.com/repos/guardian/grid","forks_url":"https://api.github.com/repos/guardian/grid/forks","keys_url":"https://api.github.com/repos/guardian/grid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/guardian/grid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/guardian/grid/teams","hooks_url":"https://api.github.com/repos/guardian/grid/hooks","issue_events_url":"https://api.github.com/repos/guardian/grid/issues/events{/number}","events_url":"https://api.github.com/repos/guardian/grid/events","assignees_url":"https://api.github.com/repos/guardian/grid/assignees{/user}","branches_url":"https://api.github.com/repos/guardian/grid/branches{/branch}","tags_url":"https://api.github.com/repos/guardian/grid/tags","blobs_url":"https://api.github.com/repos/guardian/grid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/guardian/grid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/guardian/grid/git/refs{/sha}","trees_url":"https://api.github.com/repos/guardian/grid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/guardian/grid/statuses/{sha}","languages_url":"https://api.github.com/repos/guardian/grid/languages","stargazers_url":"https://api.github.com/repos/guardian/grid/stargazers","contributors_url":"https://api.github.com/repos/guardian/grid/contributors","subscribers_url":"https://api.github.com/repos/guardian/grid/subscribers","subscription_url":"https://api.github.com/repos/guardian/grid/subscription","commits_url":"https://api.github.com/repos/guardian/grid/commits{/sha}","git_commits_url":"https://api.github.com/repos/guardian/grid/git/commits{/sha}","comments_url":"https://api.github.com/repos/guardian/grid/comments{/number}","issue_comment_url":"https://api.github.com/repos/guardian/grid/issues/comments{/number}","contents_url":"https://api.github.com/repos/guardian/grid/contents/{+path}","compare_url":"https://api.github.com/repos/guardian/grid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/guardian/grid/merges","archive_url":"https://api.github.com/repos/guardian/grid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/guardian/grid/downloads","issues_url":"https://api.github.com/repos/guardian/grid/issues{/number}","pulls_url":"https://api.github.com/repos/guardian/grid/pulls{/number}","milestones_url":"https://api.github.com/repos/guardian/grid/milestones{/number}","notifications_url":"https://api.github.com/repos/guardian/grid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/guardian/grid/labels{/name}","releases_url":"https://api.github.com/repos/guardian/grid/releases{/id}","created_at":"2013-08-22T13:49:31Z","updated_at":"2015-09-21T04:45:13Z","pushed_at":"2015-09-21T13:07:04Z","git_url":"git://github.com/guardian/grid.git","ssh_url":"git@github.com:guardian/grid.git","clone_url":"https://github.com/guardian/grid.git","svn_url":"https://github.com/guardian/grid","homepage":"","size":22644,"stargazers_count":918,"watchers_count":918,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":43,"mirror_url":null,"open_issues_count":63,"forks":43,"open_issues":63,"watchers":918,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/guardian/grid/pulls/1277"},"html":{"href":"https://github.com/guardian/grid/pull/1277"},"issue":{"href":"https://api.github.com/repos/guardian/grid/issues/1277"},"comments":{"href":"https://api.github.com/repos/guardian/grid/issues/1277/comments"},"review_comments":{"href":"https://api.github.com/repos/guardian/grid/pulls/1277/comments"},"review_comment":{"href":"https://api.github.com/repos/guardian/grid/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/guardian/grid/pulls/1277/commits"},"statuses":{"href":"https://api.github.com/repos/guardian/grid/statuses/6ed353eb43b5d1c278565aa28238d324c60eaace"}}}}