gadget_officejs_page_jio_dropbox_configurator_js.txt 3.96 KB
Newer Older
1
/*global window, rJS, RSVP*/
2
/*jslint nomen: true, indent: 2, maxerr: 3*/
3
(function (window, rJS, RSVP) {
4 5
  "use strict";

6
  function setDropboxConfiguration(gadget, token) {
7 8 9 10 11 12 13 14 15 16
    return new RSVP.Queue()
      .push(function () {
        return RSVP.all([
          gadget.getSetting("portal_type"),
          gadget.getSetting("erp5_attachment_synchro", "")
        ]);
      })
      .push(function (setting) {
        var configuration = {},
          attachment_synchro = setting[1] !== "";
17 18 19
        configuration = {
          type: "replicate",
          query: {
20
            query: 'portal_type:"' + setting[0] + '" ',
21
            limit: [0, 200],
22
            sort_on: [["modification_date", "descending"]]
23 24 25
          },
          use_remote_post: false,
          conflict_handling: 2,
26 27 28 29 30
          check_local_attachment_modification: attachment_synchro,
          check_local_attachment_creation: attachment_synchro,
          check_remote_attachment_modification: attachment_synchro,
          check_remote_attachment_creation: attachment_synchro,
          check_remote_attachment_deletion: attachment_synchro,
31 32 33 34 35 36
          check_local_modification: true,
          check_local_creation: true,
          check_local_deletion: false,
          check_remote_modification: true,
          check_remote_creation: true,
          check_remote_deletion: true,
37 38 39 40 41 42 43 44 45 46
          signature_sub_storage: {
            type: "query",
            sub_storage: {
              type: "uuid",
              sub_storage: {
                type: "indexeddb",
                database: "officejs-dropbox-hash"
              }
            }
          },
47
          local_sub_storage: {
48
            type: "query",
49
            sub_storage: {
50
              type: "uuid",
51
              sub_storage: {
52 53
                type: "indexeddb",
                database: "officejs-dropbox"
54 55 56 57 58
              }
            }
          },
          remote_sub_storage: {
            type: "mapping",
59
            attachment_list: ["data"],
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            attachment: {
              'data': {
                get: {uri_template: 'enclosure'},
                put: {uri_template: 'enclosure'}
              }
            },
            mapping_dict: {
              "portal_type": ["equalSubProperty", "type"]
            },
            sub_storage: {
              type: "query",
              sub_storage: {
                type: "drivetojiomapping",
                sub_storage: {
                  type: "dropbox",
75
                  access_token: token,
76 77 78 79 80 81 82 83 84 85 86 87 88 89
                  root: "sandbox"
                }
              }
            }
          }
        };
        return gadget.setSetting('jio_storage_description', configuration);
      })
      .push(function () {
        return gadget.setSetting('jio_storage_name', "DROPBOX");
      })
      .push(function () {
        return gadget.setSetting('sync_reload', true);
      })
90 91 92 93 94 95 96 97 98 99 100
      .push(function () {
        // XXX Please note this is a work-around to have a usable application
        // waiting for a bug in IndexedDB or jIO to be fixed
        // this bug can been seen on 'fast' devices where
        // data stored in settings is not accessible right after save.
        // it was shown that after redirecting to sync page, the
        // "sync_reload" value got in the sync gadget was "undefined"
        // Hence triggering synchronization and not setting the storage first
        // and redirecting once again on the jio configuration page
        return gadget.getSetting('sync_reload');
      })
101
      .push(function () {
102 103 104 105
        return gadget.redirect({
          command: "display",
          options: {page: 'ojs_sync', auto_repair: 'true'}
        });
106 107 108 109 110 111 112 113 114 115
      });
  }

  var gadget_klass = rJS(window);

  gadget_klass
    .declareAcquiredMethod("redirect", "redirect")
    .declareAcquiredMethod("getSetting", "getSetting")
    .declareAcquiredMethod("setSetting", "setSetting")
    .declareMethod("render", function (options) {
116
      return setDropboxConfiguration(this, options.access_token);
117 118
    });

119
}(window, rJS, RSVP));