first steps to add subject editing
authorymh <ymh.work@gmail.com>
Tue, 01 Mar 2016 23:35:33 +0100
changeset 135 66c1d31e2f97
parent 134 c06d08c8a1b8
child 136 5fed7e1716c2
first steps to add subject editing
common/corpus-common-addon/addon/utils/utils.js
server/bo_client/app/locales/en/translations.js
server/bo_client/app/locales/fr/translations.js
server/bo_client/app/pods/components/bo-doc-bnf-autocomplete/component.js
server/bo_client/app/pods/components/bo-doc-bnf-autocomplete/template.hbs
server/bo_client/app/pods/components/bo-doc-new-subject/component.js
server/bo_client/app/pods/components/bo-doc-new-subject/template.hbs
server/bo_client/app/pods/components/bo-doc-subject-row/component.js
server/bo_client/app/pods/components/bo-doc-subject-row/styles.scss
server/bo_client/app/pods/components/bo-doc-subject-row/template.hbs
server/bo_client/app/pods/components/bo-doc-subjects/component.js
server/bo_client/app/pods/components/bo-doc-subjects/styles.scss
server/bo_client/app/pods/components/bo-doc-subjects/template.hbs
server/bo_client/app/pods/components/bo-doc-viaf-autocomplete/component.js
server/bo_client/app/templates/components/bo-doc-bnf-autocomplete/suggestion.hbs
server/bo_client/app/templates/doc.hbs
server/bo_client/config/environment.js
server/bo_client/tests/integration/pods/components/bo-doc-bnf-autocomplete/component-test.js
server/bo_client/tests/integration/pods/components/bo-doc-new-subject/component-test.js
server/bo_client/tests/integration/pods/components/bo-doc-subject-row/component-test.js
server/bo_client/tests/integration/pods/components/bo-doc-subjects/component-test.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/corpus-common-addon/addon/utils/utils.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,5 @@
+import * as constants from 'corpus-common-addon/utils/constants';
+
+export function isBnfLink(s) {
+  return s.startsWith(constants.BNF_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_URL) || s.startsWith(constants.BNF_ARK_BASE_ID);
+};
--- a/server/bo_client/app/locales/en/translations.js	Sun Feb 28 09:30:23 2016 +0100
+++ b/server/bo_client/app/locales/en/translations.js	Tue Mar 01 23:35:33 2016 +0100
@@ -20,6 +20,9 @@
     "contributors_th_url": "URL",
     "contributors_th_role": "Role",
     "contributors_th_actions": "Actions",
+    "subjects_th_label": "Label",
+    "subjects_th_url": "URL",
+    "subjects_th_actions": "Actions",
     "olac_role_annotator": "annotator",
     "olac_role_author": "author",
     "olac_role_compiler": "compiler",
--- a/server/bo_client/app/locales/fr/translations.js	Sun Feb 28 09:30:23 2016 +0100
+++ b/server/bo_client/app/locales/fr/translations.js	Tue Mar 01 23:35:33 2016 +0100
@@ -19,6 +19,9 @@
     "contributors_th_url": "URL",
     "contributors_th_role": "Role",
     "contributors_th_actions": "Actions",
+    "subjects_th_label": "Label",
+    "subjects_th_url": "URL",
+    "subjects_th_actions": "Actions",
     "olac_role_annotator": "annotateur",
     "olac_role_author": "auteur",
     "olac_role_compiler": "compilateur",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-bnf-autocomplete/component.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,96 @@
+import Ember from 'ember';
+import URI from 'urijs';
+import suggestionTemplate from 'bo-client/templates/components/bo-doc-bnf-autocomplete/suggestion';
+import _ from 'lodash/lodash';
+
+export default Ember.Component.extend({
+  env: function() {
+    return Ember.getOwner(this).resolveRegistration('config:environment');
+  },
+  actions: {
+    setBnfId: function(value) {
+      if(_.isString(value)) {
+        this.set('bnfId', {
+          label: value,
+          value: value,
+          id: "",
+          url: "",
+          nametype: "" }
+        );
+      }
+      else {
+        this.set('bnfId', value);
+      }
+    },
+  },
+
+  limit: 15,
+
+  display(selection) {
+    return selection.value;
+  },
+
+  setValue(selection) {
+    let typeahead = Ember.$(this.$(), ".aupac-typeahead");
+    if(selection) {
+      if(_.isString(selection)) {
+        typeahead.typeahead('val', selection);
+      } else {
+        typeahead.typeahead('val', selection.value);
+      }
+
+    } else {
+      typeahead.typeahead('val', '');
+    }
+  },
+
+  keyDown(event) {
+    if(event.which === this.get('constants').KEY_CODES.RETURN) {
+      Ember.$(this.$(), ".aupac-typeahead").typeahead('close');
+    }
+  },
+
+  suggestionTemplate: suggestionTemplate,
+
+  _registerAfterRender: function() {
+    Ember.run.schedule('afterRender', this, function () {
+      this.set('registerAs', this);
+    });
+  }.on('init'),
+
+
+  bnfSource : function(query, syncResults, asyncResults) {
+    var url = URI(this.env().APP['bo-doc-bnf-autocomplete'].bnfQueryUrl).query({term: query}).toString();
+    Ember.$.ajax({
+      url: url,
+      dataType: "jsonp",
+      success: function(data) {
+        var queryTextRes = {
+          label: query,
+          value: query,
+          id: "",
+          url: "",
+          nametype: ""
+        };
+        if (data.result) {
+          var results = Ember.$.map( data.result, function(item) {
+            var retLbl = item.term + " [" + item.nametype + "]",
+                bnfurl = this.env().APP['bo-doc-viaf-autocomplete'].viafBaseUrl + item.bnfid;
+            return {
+              label: retLbl,
+              value: item.term,
+              id: item.bnfid,
+              url: bnfurl,
+              nametype: item.nametype
+            };
+          });
+          results.unshift(queryTextRes);
+          asyncResults( results );
+        } else {
+          asyncResults([queryTextRes]);
+        }
+      },
+    });
+  }
+
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-bnf-autocomplete/template.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,13 @@
+{{aupac-typeahead action=(action 'setBnfId')
+    class=(if inputclass inputclass)
+    source=bnfSource
+    placeholder=(if placeholder placeholder)
+    display=display
+    async=true
+    hint=false
+    suggestionTemplate=suggestionTemplate
+    selection=(readonly initialBnfId)
+    allowFreeInput=false
+    limit=15
+    setValue=setValue
+}}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-new-subject/component.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,46 @@
+import Ember from 'ember';
+
+export default Ember.Component.extend({
+  tagName: "tr",
+  bnfId: null,
+  initialBnfId: null,
+  actions: {
+    addNewContributor: function() {
+      if(!this.get('valuesNotSet')) {
+        this.get('onAdd')(this.getProperties('label','url'));
+        this.clearProperties();
+      }
+    }
+  },
+  clearProperties: function() {
+    var subjectDef = {
+      bnfId: null,
+    };
+    this.setProperties(subjectDef);
+    //clear typeahead
+    this.$('.aupac-typeahead').typeahead('val','');
+  },
+
+  _clearAfterRender: function() {
+    Ember.run.schedule('afterRender', this, function () {
+      this.clearProperties();
+    });
+  }.on('init'),
+
+  valuesNotSet: Ember.computed('url', function() {
+    return !( this.get('url') );
+  }),
+
+  label: Ember.computed('bnfId', function() {
+    var bnfId = this.get('bnfId');
+    return (bnfId == null)?"":bnfId.value;
+  }),
+
+  url: Ember.computed('bnfId', function() {
+    var bnfId = this.get('bnfId');
+    return (bnfId == null)?"":bnfId.url;
+  }),
+
+  urlPresent: Ember.computed.bool('url'),
+
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-new-subject/template.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,12 @@
+<td class="col-md-1">&nbsp;</td>
+<td class="col-md-10" colspan="2">
+  <div class='row'>
+    {{bo-doc-viaf-autocomplete inputclass="form-control" placeholder=(t 'bo.contributors_th_name') viafId=viafId initialViafId=initialViafId class='col-md-11'}}
+    {{#if urlPresent }}
+    <a target="_blank" href="{{url}}" class="col-md-1 bo-client-bnf-32x32-icon bo-client-force-block" title="{{url}}">&nbsp;</a>
+    {{ else }}
+    <div class="col-md-1 bo-client-blank-32x32-icon">&nbsp;</div>
+    {{/if}}
+  </div>
+</td>
+<td class='bo-doc-actions-col col-md-1'>{{fa-icon "plus" class=(if valuesNotSet "bo-doc-no-edit-icons" "bo-doc-edit-icons") click=(action "addNewContributor")}}</td>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subject-row/component.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,11 @@
+import Ember from 'ember';
+import * as utils from 'corpus-common-addon/utils/utils';
+
+export default Ember.Component.extend({
+  tagName: "tr",
+  isSubjectLink: Ember.computed.match('subject', /^http\:\/\//),
+  isBnfLink: Ember.computed('subject', function() {
+    return utils.isBnfLink(this.get('subject'));
+  }),
+  isNotBnfLink: Ember.computed.not("isBnfLink")
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subject-row/styles.scss	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,3 @@
+.bo-doc-subject-not-bnf {
+  color: $gray;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subject-row/template.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,6 @@
+<td class="col-md-1">{{add-one index}}</td>
+<td {{bind-attr class=":col-md-5 :bo-doc-subject-label isNotBnfLink:bo-doc-subject-not-bnf"}}>{{component (get-link-type subject) url=subject}}</td>
+<td {{bind-attr class=":col-md-5 isNotBnfLink:bo-doc-subject-not-bnf"}}>{{#if isSubjectLink}}{{subject}}{{/if}}</td>
+<td class='bo-doc-actions-col col-md-1'>
+  {{#if isBnfLink }}{{fa-icon "trash-o" class="bo-doc-edit-icons" }}{{/if}}
+</td>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subjects/component.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,21 @@
+import Ember from 'ember';
+import _ from 'lodash/lodash';
+import * as utils from 'corpus-common-addon/utils/utils';
+
+export default Ember.Component.extend({
+  isEditing: false,
+  subjectsList: Ember.computed("document.subjects[]", function() {
+    return _.union(
+      _.filter(this.get("document").get("subjects"), utils.isBnfLink),
+      _.filter(this.get("document").get("subjects"), function(s) { return !utils.isBnfLink(s); })
+    );
+  }),
+  actions: {
+    toggleEditSubjects: function() {
+      this.set('isEditing', !this.get('isEditing'));
+      this.$('#doc-subjects-table-pane').slideToggle();
+      this.$('#doc-subjects-list-pane').slideToggle();
+    },
+  }
+
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subjects/styles.scss	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,5 @@
+
+#doc-subjects-table-pane {
+	display: none;
+	margin-top: 20px;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/pods/components/bo-doc-subjects/template.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,36 @@
+<div class="col-md-1">
+    {{ fa-icon "pencil" class='bo-doc-edit-icons' click=(action "toggleEditSubjects")}}
+</div>
+<div class="col-md-11">
+  <div class="content-value">{{#each model.subjects as |subject index|}}{{if index ", "}}{{component (get-link-type subject) url=subject}}{{/each}}</div>
+</div>
+
+<div class="col-md-11">
+  <div class="row">
+    <div class="content-title">{{t 'bo.document_subjects'}}</div>
+  </div>
+  <div class="row" id="doc-subjects-list-pane">
+    <div class="col-md-12 content-value">{{#each document.subjects as |subject index|}}{{if index ", "}}{{component (get-link-type subject) url=subject}}{{/each}}</div>
+  </div>
+  <div class="row" id="doc-subjects-table-pane">
+    <div class="col-md-12 panel panel-default">
+      {{#if isEditing}}
+      <table class="table table-striped table-condensed">
+        <thead>
+          <tr>
+            <th class="col-md-1">#</th>
+            <th class="col-md-5">{{t 'bo.subjects_th_label'}}</th>
+            <th class="col-md-5">{{t 'bo.subjects_th_url'}}</th>
+            <th class="col-md-1">{{t 'bo.subjects_th_actions'}}</th>
+          </tr>
+        </thead>
+        <tbody>
+        {{#each subjectsList as |subject index|}}
+          {{bo-doc-subject-row subject=subject index=index}}
+        {{/each}}
+        </tbody>
+      </table>
+      {{/if}}
+    </div>
+  </div>
+</div>
--- a/server/bo_client/app/pods/components/bo-doc-viaf-autocomplete/component.js	Sun Feb 28 09:30:23 2016 +0100
+++ b/server/bo_client/app/pods/components/bo-doc-viaf-autocomplete/component.js	Tue Mar 01 23:35:33 2016 +0100
@@ -11,7 +11,6 @@
 
   actions: {
     setViafId: function(value) {
-      console.log('setViafId',value);
       if(_.isString(value)) {
         this.set('viafId', {
           label: value,
@@ -30,12 +29,10 @@
   limit: 15,
 
   display(selection) {
-    console.log('DISPLAY',selection);
     return selection.value;
   },
 
   setValue(selection) {
-    console.log('setValue',selection);
     let typeahead = Ember.$(this.$(), ".aupac-typeahead");
     if(selection) {
       if(_.isString(selection)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/app/templates/components/bo-doc-bnf-autocomplete/suggestion.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,1 @@
+<div class='typeahead-suggestion'>{{#if model}}{{model.label}} - {{model.url}}{{/if}}</div>
--- a/server/bo_client/app/templates/doc.hbs	Sun Feb 28 09:30:23 2016 +0100
+++ b/server/bo_client/app/templates/doc.hbs	Tue Mar 01 23:35:33 2016 +0100
@@ -38,13 +38,7 @@
                 </div>
               </div>
               <div class="row">
-                <div class="col-md-1">
-                  &nbsp;
-                </div>
-                <div class="col-md-11">
-                  <div class="content-title">{{t 'bo.document_subjects'}}</div>
-                  <div class="content-value">{{#each model.subjects as |subject index|}}{{if index ", "}}{{component (get-link-type subject) url=subject}}{{/each}}</div>
-                </div>
+                {{bo-doc-subjects document=model}}
               </div>
           </div>
           <div class="col-md-4">
--- a/server/bo_client/config/environment.js	Sun Feb 28 09:30:23 2016 +0100
+++ b/server/bo_client/config/environment.js	Tue Mar 01 23:35:33 2016 +0100
@@ -24,6 +24,10 @@
       'bo-doc-viaf-autocomplete' : {
           viafQueryUrl: "http://viaf.org/viaf/AutoSuggest?query=",
           viafBaseUrl: "http://viaf.org/viaf/",
+      },
+      'bo-doc-bnf-autocomplete' : {
+          bnfQueryUrl: "http://data.bnf.fr/search-letter/?term=",
+          bnfBaseUrl: "http://data.bnf.fr/",
       }
     },
   };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/tests/integration/pods/components/bo-doc-bnf-autocomplete/component-test.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,24 @@
+import { moduleForComponent, test } from 'ember-qunit';
+import hbs from 'htmlbars-inline-precompile';
+
+moduleForComponent('bo-doc-bnf-autocomplete', 'Integration | Component | bo doc bnf autocomplete', {
+  integration: true
+});
+
+test('it renders', function(assert) {
+  // Set any properties with this.set('myProperty', 'value');
+  // Handle any actions with this.on('myAction', function(val) { ... });"
+
+  this.render(hbs`{{bo-doc-bnf-autocomplete}}`);
+
+  assert.equal(this.$().text().trim(), '');
+
+  // Template block usage:"
+  this.render(hbs`
+    {{#bo-doc-bnf-autocomplete}}
+      template block text
+    {{/bo-doc-bnf-autocomplete}}
+  `);
+
+  assert.equal(this.$().text().trim(), 'template block text');
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/tests/integration/pods/components/bo-doc-new-subject/component-test.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,24 @@
+import { moduleForComponent, test } from 'ember-qunit';
+import hbs from 'htmlbars-inline-precompile';
+
+moduleForComponent('bo-doc-new-subject', 'Integration | Component | bo doc new subject', {
+  integration: true
+});
+
+test('it renders', function(assert) {
+  // Set any properties with this.set('myProperty', 'value');
+  // Handle any actions with this.on('myAction', function(val) { ... });"
+
+  this.render(hbs`{{bo-doc-new-subject}}`);
+
+  assert.equal(this.$().text().trim(), '');
+
+  // Template block usage:"
+  this.render(hbs`
+    {{#bo-doc-new-subject}}
+      template block text
+    {{/bo-doc-new-subject}}
+  `);
+
+  assert.equal(this.$().text().trim(), 'template block text');
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/tests/integration/pods/components/bo-doc-subject-row/component-test.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,24 @@
+import { moduleForComponent, test } from 'ember-qunit';
+import hbs from 'htmlbars-inline-precompile';
+
+moduleForComponent('bo-doc-subject-row', 'Integration | Component | bo doc subject row', {
+  integration: true
+});
+
+test('it renders', function(assert) {
+  // Set any properties with this.set('myProperty', 'value');
+  // Handle any actions with this.on('myAction', function(val) { ... });"
+
+  this.render(hbs`{{bo-doc-subject-row}}`);
+
+  assert.equal(this.$().text().trim(), '');
+
+  // Template block usage:"
+  this.render(hbs`
+    {{#bo-doc-subject-row}}
+      template block text
+    {{/bo-doc-subject-row}}
+  `);
+
+  assert.equal(this.$().text().trim(), 'template block text');
+});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/bo_client/tests/integration/pods/components/bo-doc-subjects/component-test.js	Tue Mar 01 23:35:33 2016 +0100
@@ -0,0 +1,24 @@
+import { moduleForComponent, test } from 'ember-qunit';
+import hbs from 'htmlbars-inline-precompile';
+
+moduleForComponent('bo-doc-subjects', 'Integration | Component | bo doc subjects', {
+  integration: true
+});
+
+test('it renders', function(assert) {
+  // Set any properties with this.set('myProperty', 'value');
+  // Handle any actions with this.on('myAction', function(val) { ... });"
+
+  this.render(hbs`{{bo-doc-subjects}}`);
+
+  assert.equal(this.$().text().trim(), '');
+
+  // Template block usage:"
+  this.render(hbs`
+    {{#bo-doc-subjects}}
+      template block text
+    {{/bo-doc-subjects}}
+  `);
+
+  assert.equal(this.$().text().trim(), 'template block text');
+});