client/src/api/WebAnnotationSerializer.js
author Alexandre Segura <mex.zktk@gmail.com>
Tue, 27 Jun 2017 13:12:19 +0200
changeset 98 2e939d9cf193
child 100 6fd752d98933
permissions -rw-r--r--
Introduce WebAnnotationSerializer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
98
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
import Note from '../store/noteRecord';
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
class WebAnnotationSerializer {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     4
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
  static serialize = (note) => {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
    const categories = note.categories;
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     8
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
    const baseAnnotation = {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    10
      '@context': "http://www.w3.org/ns/anno.jsonld",
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
      "type":     "Annotation",
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
    }
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
    const source = "/session/" + note.session + "/notes/" + note._id;
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    16
    return categories.map((category, index) => {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    17
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    18
      let annotation = Object.assign({}, baseAnnotation, {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    19
        id: index
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    20
      });
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    21
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    22
      if (category.hasOwnProperty('hasComment') && category.hasComment) {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    23
        const body = {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    24
          "type": "TextualBody",
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    25
          "value": category.comment,
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    26
          "format": "text/plain"
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    27
        };
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    28
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    29
        annotation = Object.assign({}, annotation, { body })
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    30
      }
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    31
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    32
      return Object.assign({}, annotation, {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    33
        "target": {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    34
          "source": source,
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    35
          "selector": {
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    36
            "type": "TextQuoteSelector",
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    37
            "exact": category.text,
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    38
          }
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    39
        }
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    40
      })
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    41
    });
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    42
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    43
  }
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    44
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    45
}
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    46
2e939d9cf193 Introduce WebAnnotationSerializer.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    47
export default WebAnnotationSerializer