From 972b1c855f55110624e9d71cbf42c8b63b4e51f9 Mon Sep 17 00:00:00 2001 From: John Martin Date: Thu, 6 Jun 2013 12:18:29 +0100 Subject: [PATCH] [#953] Adds JS module for disabling visibility field on dataset creation Essentially we get the current value of the visibility dropdown and then if no org is selected then we disable the visibilty dropdown and set it to be public. --- .../javascript/modules/dataset-visibility.js | 32 +++++++++++++++++++ ckan/public/base/javascript/resource.config | 1 + .../snippets/package_basic_fields.html | 16 ++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 ckan/public/base/javascript/modules/dataset-visibility.js diff --git a/ckan/public/base/javascript/modules/dataset-visibility.js b/ckan/public/base/javascript/modules/dataset-visibility.js new file mode 100644 index 00000000000..5bfa4963bb0 --- /dev/null +++ b/ckan/public/base/javascript/modules/dataset-visibility.js @@ -0,0 +1,32 @@ +/* Dataset visibility toggler + * When no organization is selected in the org dropdown then set visibility to + * public always and disable dropdown + */ +this.ckan.module('dataset-visibility', function ($, _) { + return { + currentValue: false, + options: { + organizations: $('#field-organizations'), + visibility: $('#field-private'), + currentValue: null + }, + initialize: function() { + $.proxyAll(this, /_on/); + this.options.currentValue = this.options.visibility.val(); + this.options.organizations.on('change', this._onOrganizationChange); + this._onOrganizationChange(); + }, + _onOrganizationChange: function() { + var value = this.options.organizations.val(); + if (value) { + this.options.visibility + .prop('disabled', false) + .val(this.options.currentValue); + } else { + this.options.visibility + .prop('disabled', true) + .val('False'); + } + } + }; +}); diff --git a/ckan/public/base/javascript/resource.config b/ckan/public/base/javascript/resource.config index 80ed4faf9bc..3fa94db51cc 100644 --- a/ckan/public/base/javascript/resource.config +++ b/ckan/public/base/javascript/resource.config @@ -36,6 +36,7 @@ ckan = modules/activity-stream.js modules/dashboard.js modules/table-toggle-more.js + modules/dataset-visibility.js main = apply_html_class diff --git a/ckan/templates/package/snippets/package_basic_fields.html b/ckan/templates/package/snippets/package_basic_fields.html index 8330a36ec1c..3b011645e80 100644 --- a/ckan/templates/package/snippets/package_basic_fields.html +++ b/ckan/templates/package/snippets/package_basic_fields.html @@ -57,8 +57,14 @@ {% set dataset_has_organization = data.owner_org or data.group_id %} {% set organizations_available = h.organizations_available('create_dataset') %} {% set user_is_sysadmin = h.check_access('sysadmin') %} +{% set show_organizations_selector = organizations_available and (user_is_sysadmin or dataset_is_draft) %} +{% set show_visibility_selector = dataset_has_organization or (organizations_available and (user_is_sysadmin or dataset_is_draft)) %} -{% if organizations_available and (user_is_sysadmin or dataset_is_draft) %} +{% if show_organizations_selector and show_visibility_selector %} +
+{% endif %} + +{% if show_organizations_selector %}
@@ -74,12 +80,12 @@
{% endif %} -{% if dataset_has_organization or (organizations_available and (user_is_sysadmin or dataset_is_draft)) %} +{% if show_visibility_selector %} {% block package_metadata_fields_visibility %}
- {% for option in [(true, _('Private')), (false, _('Public'))] %} {% endfor %} @@ -89,4 +95,8 @@ {% endblock %} {% endif %} +{% if show_organizations_selector and show_visibility_selector %} +
+{% endif %} + {% endblock %}