Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
(function ($, ns) {
2
  H5PEditor.init = function ($form, $type, $upload, $create, $editor, $library, $params, $maxScore, $title, cancelSubmitCallback) {
3
    H5PEditor.$ = H5P.jQuery;
4
    H5PEditor.basePath = H5PIntegration.editor.libraryUrl;
5
    H5PEditor.fileIcon = H5PIntegration.editor.fileIcon;
6
    H5PEditor.ajaxPath = H5PIntegration.editor.ajaxPath;
7
    H5PEditor.filesPath = H5PIntegration.editor.filesPath;
8
    H5PEditor.apiVersion = H5PIntegration.editor.apiVersion;
9
    H5PEditor.contentLanguage = H5PIntegration.editor.language;
10
 
11
    // Semantics describing what copyright information can be stored for media.
12
    H5PEditor.copyrightSemantics = H5PIntegration.editor.copyrightSemantics;
13
    H5PEditor.metadataSemantics = H5PIntegration.editor.metadataSemantics;
14
 
15
    // Required styles and scripts for the editor
16
    H5PEditor.assets = H5PIntegration.editor.assets;
17
 
18
    // Required for assets
19
    H5PEditor.baseUrl = '';
20
    H5PEditor.enableContentHub = H5PIntegration.editor.enableContentHub;
21
 
22
    if (H5PIntegration.editor.nodeVersionId !== undefined) {
23
      H5PEditor.contentId = H5PIntegration.editor.nodeVersionId;
24
    }
25
 
26
    if (H5PIntegration.editor.hub !== undefined) {
27
      H5PIntegration.Hub = {
28
        contentSearchUrl: H5PIntegration.editor.hub.contentSearchUrl
29
      };
30
    }
31
 
32
    var h5peditor;
33
    $create.hide();
34
    var library = $library.val();
35
 
36
    $type.change(function () {
37
      if ($type.filter(':checked').val() === 'upload') {
38
        $create.hide();
39
        $upload.show();
40
      }
41
      else {
42
        $upload.hide();
43
        if (h5peditor === undefined) {
44
          h5peditor = new ns.Editor(library, $params.val(), $editor[0]);
45
        }
46
        $create.show();
47
      }
48
    });
49
 
50
    if ($type.filter(':checked').val() === 'upload') {
51
      $type.change();
52
    }
53
    else {
54
      $type.filter('input[value="create"]').attr('checked', true).change();
55
    }
56
 
57
    // Duplicate the submit button input because it is not posted when calling $form.submit()
58
    const $submitters = $form.find('input[type="submit"]');
59
    let isCanceling = false;
60
    $submitters.click(function () {
61
      // Create hidden input and give it the value
62
      const name = $(this).prop('name');
63
      const value = $(this).prop('value');
64
      $('<input type="hidden" name="' + name + '" value="' + value + '" />').appendTo($form);
65
 
66
      // Allow caller to cancel validation and submission of form on button click
67
      if (cancelSubmitCallback) {
68
        isCanceling = cancelSubmitCallback($(this));
69
      }
70
    });
71
 
72
    let formIsUpdated = false;
73
    $form.submit(function (event) {
74
      if ($type.length && $type.filter(':checked').val() === 'upload') {
75
        return; // Old file upload
76
      }
77
 
78
      if (isCanceling) {
79
        return;
80
      }
81
 
82
      if (h5peditor !== undefined && !formIsUpdated) {
83
 
84
        // Get content from editor
85
        h5peditor.getContent(function (content) {
86
 
87
          // Set the title field to the metadata title if the field exists
88
          $title.val(content.title);
89
 
90
          // Set main library
91
          $library.val(content.library);
92
 
93
          // Set params
94
          $params.val(content.params);
95
 
96
          // Submit form data
97
          formIsUpdated = true;
98
          $form.submit();
99
        });
100
 
101
        // Stop default submit
102
        event.preventDefault();
103
      }
104
    });
105
  };
106
 
107
  H5PEditor.getAjaxUrl = function (action, parameters) {
108
    var url = H5PIntegration.editor.ajaxPath + action;
109
 
110
    if (parameters !== undefined) {
111
      var separator = url.indexOf('?') === -1 ? '?' : '&';
112
      for (var property in parameters) {
113
        if (parameters.hasOwnProperty(property)) {
114
          url += separator + property + '=' + parameters[property];
115
          separator = '&';
116
        }
117
      }
118
    }
119
 
120
    return url;
121
  };
122
})(H5P.jQuery, H5PEditor);