imageannotation.html 9.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<%! from django.utils.translation import ugettext as _ %>

<style type="text/css">
    .openseadragon1{
        width: 100%;
        height: 600px;
        cursor: all-scroll;
    }
</style>

<div class="annotatable-wrapper">
lduarte1991 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    <div class="annotatable-header">
        % if display_name is not UNDEFINED and display_name is not None:
            <div class="annotatable-title">${display_name}</div>
        % endif
    </div>
    % if instructions_html is not UNDEFINED and instructions_html is not None:
    <div class="annotatable-section shaded">
        <div class="annotatable-section-title">
            ${_('Instructions')}
            <a class="annotatable-toggle annotatable-toggle-instructions expanded" href="javascript:void(0)">${_('Collapse Instructions')}</a>
        </div>
        <div class="annotatable-section-body annotatable-instructions">
            ${instructions_html}
        </div>
    </div>
    % endif
    <div class="annotatable-section">
        <div class="annotatable-content">
            <div id="imageHolder" class="openseadragon1"> 
31 32 33
                <%namespace name='static' file='/static_content.html'/>
                ${static.css(group='style-vendor-tinymce-content', raw=True)}
                ${static.css(group='style-vendor-tinymce-skin', raw=True)}
34
                ${static.css(group='style-xmodule-annotations', raw=True)}
35
            </div> 
lduarte1991 committed
36 37 38 39 40
            <div id="catchDIV">
                <div class="annotationListContainer">${_('Note: only instructors may annotate.')}</div>
            </div>
        </div>
    </div>
41 42 43
</div>

<script>
44 45 46
// TODO: Fix indentation in the following block
(function (require) {
    var init = function() {
lduarte1991 committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    function onClickHideInstructions(){
        //Reset function if there is more than one event handler
        $(this).off();
        $(this).on('click',onClickHideInstructions);
        var hide = $(this).html()=='Collapse Instructions'?true:false,
            cls, txt,slideMethod;
        txt = (hide ? 'Expand' : 'Collapse') + ' Instructions';
        cls = (hide ? ['expanded', 'collapsed'] : ['collapsed', 'expanded']);
        slideMethod = (hide ? 'slideUp' : 'slideDown');
        $(this).text(txt).removeClass(cls[0]).addClass(cls[1]);
        $(this).parents('.annotatable-section:first').find('.annotatable-instructions')[slideMethod]();
    }
    $('.annotatable-toggle-instructions').on('click', onClickHideInstructions);
    
    //Grab uri of the course
    var parts = window.location.href.split("/"), 
        uri = '',
    courseid;
    for (var index = 0; index <= 9; index += 1) uri += parts[index]+"/"; //Get the unit url
    courseid = parts[4] + "/" + parts[5] + "/" + parts[6];
    //Change uri in cms
    var lms_location = $('.sidebar .preview-button').attr('href');
    if (typeof lms_location!='undefined'){
        courseid = parts[4].split(".").join("/");
        uri = window.location.protocol;
        for (var index = 0; index <= 9; index += 1) uri += lms_location.split("/")[index]+"/"; //Get the unit url
    }
    var unit_id = $('#sequence-list').find('.active').attr("data-element");
75
        uri += unit_id;
lduarte1991 committed
76
    var pagination = 100,
77
     is_staff = '${is_course_staff}' === 'True',
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        options = {
        optionsAnnotator: {
            permissions:{
                user: {
                    id:"${user.email}", 
                    name:"${user.username}"
                },
                userString: function (user) {
                    if (user && user.name) 
                        return user.name;
                    return user;
                },
                userId: function (user) {
                    if (user && user.id) 
                        return user.id;
                    return user;
                },
                permissions: {
                        'read':   [],
                        'update': ["${user.email}"],
                        'delete': ["${user.email}"],
                        'admin':  ["${user.email}"]
                },
101
                showViewPermissionsCheckbox: false,
102 103 104 105 106 107
                showEditPermissionsCheckbox: false,
                userAuthorize: function(action, annotation, user) {
                    var token, tokens, _i, _len;
                    if (annotation.permissions) {
                      tokens = annotation.permissions[action] || [];
                      if (is_staff){
lduarte1991 committed
108
                          return true;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                      }
                      if (tokens.length === 0) {
                        return true;
                      }
                      for (_i = 0, _len = tokens.length; _i < _len; _i++) {
                        token = tokens[_i];
                        
                        if (this.userId(user) === token) {

                          return true;
                        }
                      }

                      return false;
                    } else if (annotation.user) {
                      if (user) {
                        return this.userId(user) === this.userId(annotation.user);
                      } else {
                        return false;
                      }
                    }
                    return true;
                  },
            },
            auth: {
                token: "${token}"
            },
            store: {
                // The endpoint of the store on your server.
                prefix: "${annotation_storage}",

                annotationData: {
                    uri: uri,
                },

                urls: {
                    // These are the default URLs.
                    create:  '/create',
                    read:    '/read/:id',
                    update:  '/update/:id',
                    destroy: '/delete/:id',
                    search:  '/search'
                },

                loadFromSearch:{
                    limit:pagination,
                    offset:0,
                    uri:uri,
                    media:'image',
lduarte1991 committed
158
                    userid:'${user.email}',
159 160 161 162 163 164 165 166
                }
            },
            highlightTags:{
                tag: "${tag}",
            },
            richText: {
                tinymce:{
                    selector: "li.annotator-item textarea",
167
                    plugins: "image link codemirror media",
168 169 170
                    menubar: false,
                    toolbar_items_size: 'small',
                    extended_valid_elements : "iframe[src|frameborder|style|scrolling|class|width|height|name|align|id]",
171 172
                    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media | code ",
                    resize: "both",
173 174 175 176 177
                }
            },
        },
        optionsOpenSeadragon:{
            id:                 "imageHolder",
178 179
            annotation_mode: "${annotation_mode}",
            flags: is_staff,
180 181 182 183 184 185 186
            prefixUrl:           "${settings.STATIC_URL}" + "js/vendor/ova/images/",
            ${openseadragonjson}
        },
        optionsOSDA:{},

    };
    var imgURLRoot = "${settings.STATIC_URL}" + "js/vendor/ova/catch/img/";
187
    var osda, annotator, catchOptions, Catch;
188 189
    var startosda = function() {
        //remove old instances
lduarte1991 committed
190 191
        if (Annotator._instances.length !== 0) {
          $('#imageHolder').annotator("destroy");
192
        }
lduarte1991 committed
193 194
        delete osda;
        //Load the plugin Image/Text Annotation
195
        osda = new OpenSeadragonAnnotation($('#imageHolder'),options);
196 197 198 199 200 201

        // TODO: Fix to avoid using global scope!
        window.options = options;
        window.osda = osda;
        // END TODO

lduarte1991 committed
202 203 204
        var userId = ('${default_tab}'.toLowerCase() === 'instructor') ? 
            '${instructor_email}':
            '${user.email}';
205

lduarte1991 committed
206
        //Catch
207 208
        annotator = osda.annotator;
        catchOptions = {
lduarte1991 committed
209 210 211 212 213 214
            media:'image',
            externalLink:false,
            imageUrlRoot:imgURLRoot,
            showMediaSelector: false,
            showPublicPrivate: true,
            userId:userId,
215 216 217
            pagination:pagination,//Number of Annotations per load in the pagination,
            flags:is_staff,
            default_tab: "${default_tab}",
lduarte1991 committed
218
            instructor_email: "${instructor_email}",
219
            annotation_mode: "${annotation_mode}",
lduarte1991 committed
220 221 222 223
        };

        // if annotations are opened to everyone (2) or if they want to create no annotations (1 with no instructor)
        // then the table at the bottom of the source should be displayed
224
        if ("${annotation_mode}" == "everyone" || ("${annotation_mode}" == "instructor" && "${instructor_email}" != "")) {
225
            Catch = new CatchAnnotation($('#catchDIV'),catchOptions);
226 227 228 229
            // TODO: Fix to avoid using global scope!
            window.Catch = Catch;
            // END TODO
        }
lduarte1991 committed
230 231 232

        // if it is in instructor mode only (1), the annotator should be destroyed for all except the instructor
        if ("${annotation_mode}" == "instructor" && "${instructor_email}" == "" && !is_staff)
233
            osda.annotator.destroy();
234
    }
235

236 237 238 239 240 241
    startosda();
    };
    try {
        require(["osda"], init);
    } catch(error) {
        console.log("Error: " + error.message + " - Annotator not loaded in LMS.");
242
    }
243 244
}).call(this, require || RequireJS.require);
</script>