From 07b246f7c9239d594dbfcd460eda590ece4e4fd4 Mon Sep 17 00:00:00 2001 From: timwis Date: Tue, 5 Apr 2016 07:21:56 -0400 Subject: [PATCH] view/edit organizaiton pages --- _config.yml | 8 +++- _data/schemas/default.yml | 8 ++++ _includes/organization-form.html | 17 ++++++++ _layouts/organization.html | 43 +++++++++++++++++++ css/main.css | 1 + scripts/src/index.js | 11 +++++ scripts/src/views/organization.js | 68 +++++++++++++++++++++++++++++++ 7 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 _includes/organization-form.html create mode 100644 _layouts/organization.html create mode 100644 scripts/src/views/organization.js diff --git a/_config.yml b/_config.yml index c866fcdd9..5f686dcc7 100644 --- a/_config.yml +++ b/_config.yml @@ -41,7 +41,8 @@ collections: output: true permalink: /datasets/:path/ organizations: - output: false + output: true + permalink: /organizations/:path/ # Collections (cont'd) defaults: @@ -52,6 +53,11 @@ defaults: layout: dataset category: Uncategorized organization: N/A + - scope: + path: "" + type: organizations + values: + layout: organization # Markdown settings markdown: kramdown diff --git a/_data/schemas/default.yml b/_data/schemas/default.yml index bbbed08d9..ce3d6a004 100644 --- a/_data/schemas/default.yml +++ b/_data/schemas/default.yml @@ -45,3 +45,11 @@ resource_fields: - kmz - shp - xml + +organization_fields: + - field_name: title + label: Title + - field_name: description + label: Description + - field_name: logo + label: Logo diff --git a/_includes/organization-form.html b/_includes/organization-form.html new file mode 100644 index 000000000..110100ca9 --- /dev/null +++ b/_includes/organization-form.html @@ -0,0 +1,17 @@ +
+ + +
+ {% for field in organization_fields %} + {% assign template = field.form_template | default: "form/text.html" %} + {% assign field_name = field.field_name %} + {% assign value = include.organization[field_name] %} + {% include {{ template }} field=field field_name=field_name value=value %} + {% endfor %} +
+ + + {% if include.organization %} + + {% endif %} +
\ No newline at end of file diff --git a/_layouts/organization.html b/_layouts/organization.html new file mode 100644 index 000000000..08e2f542b --- /dev/null +++ b/_layouts/organization.html @@ -0,0 +1,43 @@ +--- +layout: default +--- +{% include breadcrumbs.html parent="Organizations" %} +{% assign schema = page.schema | default: site.schema %} +{% assign organization_fields = site.data.schemas[schema].organization_fields %} +{% assign organization_system_fields = "title|description|logo" | split: "|" %} + + + + + +
+ {% if page.logo %} +
+ {{ page.title }} logo +
+
+ {% else %} +
+ {% endif %} +

+ {{ page.title }} + Edit +

+

{{ page.description }}

+ {% assign dataset_count = site.datasets | where: "organization", page.title | size %} + {{ dataset_count }} datasets +
+
+ + diff --git a/css/main.css b/css/main.css index 7481a0b71..b8538412e 100644 --- a/css/main.css +++ b/css/main.css @@ -36,6 +36,7 @@ dataset { /* Hidden from guests */ .add-dataset-btn, .edit-dataset-btn, +.edit-organization-btn, .admin-link-list-item, .user-issue { display: none; diff --git a/scripts/src/index.js b/scripts/src/index.js index 2b58a5e86..d70321e2f 100644 --- a/scripts/src/index.js +++ b/scripts/src/index.js @@ -9,6 +9,7 @@ import NavView from './views/nav' import DatasetsView from './views/datasets' import DatasetView from './views/dataset' import AddDatasetView from './views/add-dataset' +import OrganizationView from './views/organization' import AdminView from './views/admin' import SetupView from './views/setup' import {queryByHook, setParams} from './util' @@ -61,6 +62,16 @@ const router = { }) AddDatasetView({el: elements.main, file}) }, + '/organizations/(.+)/': function () { + const file = new FileModel({ + user, + repoOwner: settings.REPO_OWNER, + repoName: settings.REPO_NAME, + repoBranch: settings.BRANCH, + filePath: settings.FILE_PATH + }) + OrganizationView({el: elements.main, user, file}) + }, '/admin/': function () { const file = new FileModel({ user, diff --git a/scripts/src/views/organization.js b/scripts/src/views/organization.js new file mode 100644 index 000000000..76d00adca --- /dev/null +++ b/scripts/src/views/organization.js @@ -0,0 +1,68 @@ +import {queryByHook} from '../util' + +export default function (opts) { + const elements = { + editButton: queryByHook('edit-button', opts.el), + readView: queryByHook('read-view', opts.el), + editView: queryByHook('edit-view', opts.el), + form: queryByHook('organization-form', opts.el), + cancelButton: queryByHook('cancel-button', opts.el), + deleteButton: queryByHook('delete-button', opts.el), + alert: queryByHook('alert', opts.el), + commitUrl: queryByHook('commit-url', opts.el) + } + + // If user is logged in and a collaborator, show the Edit Dataset button + if (opts.user.username && opts.user.isCollaborator) elements.editButton.show() + opts.user.on('change', (user) => { + if (user.username && user.isCollaborator) elements.editButton.show() + }) + + // Edit/Cancel buttons toggle read/edit views + elements.editButton.add(elements.cancelButton).on('click', (e) => { + switchView() + e.preventDefault() + }) + + // Edit form submission + elements.form.on('submit', function (e) { + const formData = elements.form.serializeJSON({useIntKeysAsArrayIndex: true}) + const yaml = opts.file.formatFrontMatter(formData) + opts.file.save(yaml) + .then((response) => { + switchView() + alert('success', response.commit.html_url) + }).catch((msg) => { + alert('error') + console.error(msg) + }) + e.preventDefault() + }) + + // Delete button + elements.deleteButton.on('click', function (e) { + if (window.confirm('Delete this organization?')) { + opts.file.remove() + .then((response) => { + alert('success', response.commit.html_url) + }).catch((msg) => { + alert('error') + console.error(msg) + }) + } + e.preventDefault() + }) + + function switchView () { + elements.readView.add(elements.editView).toggle() + } + + // Show alert box on page + function alert (type, commitUrl) { + elements.alert.hide() + $('[data-hook~=alert-' + type +']').show() + if (type === 'success' && commitUrl) { + elements.commitUrl.attr('href', commitUrl) + } + } +}