Proyectos de Subversion Moodle

Rev

Rev 997 | Rev 999 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
365 ariadna 1
define([
2
  "jquery",
3
  "core/yui",
4
  "core/str",
965 ariadna 5
  "core/config",
6
  "core/notification",
365 ariadna 7
  "core/modal_factory",
965 ariadna 8
], function ($, Y, str, config, notification, ModalFactory) {
365 ariadna 9
  var CONFIG;
10
  var NODES = {
11
    DELETE_ICON: '<span class="delete">&#x274C;</span>',
12
  };
13
  var SELECTORS = {
997 ariadna 14
    cesa_notes_ROOT: ".inline-cesa_notes-opener",
365 ariadna 15
    cesa_notes_BASE: "#cesa_notes_base",
16
    cesa_notes_OPENER: ".cesa_notes-opener",
17
    cesa_notes_LISTS: ".cesa_notes_list",
18
  };
19
  var CSS = {
20
    cesa_notes_BASE: "cesa_notes_base",
21
    cesa_notes_OPENER: "cesa_notes-opener",
22
    cesa_notes_LISTS: "cesa_notes_list",
23
  };
24
  var panel = null;
25
  var initnotes = null;
26
  var strdeletenote = M.util.get_string("deletecesa_notes", "block_cesa_notes");
27
 
28
  var cesa_notes = {
29
    /** @alias module:blocks/cesa_notes */
30
    getcesa_notesValidatedUrl: function (baseurl) {
31
      var a = document.createElement("a");
32
      a.href = baseurl;
33
      return a.search.length > 0 ? baseurl : baseurl + "?";
34
    },
35
    /*
36
     * Validation for textarea input text
37
     */
38
    getWarnings: function (status) {
39
      if (status == false) {
40
        $("#addmynote-label-" + CONFIG.instanceid + "  span.warning").html(
41
          CONFIG.maxallowedcharacters_warning
42
        );
43
      } else {
44
        var ta = $("#id_mynotecontent-" + CONFIG.instanceid);
45
        if (ta.val() == "") {
46
          $("#addmynote-label-" + CONFIG.instanceid + "  span.warning").html(
47
            ""
48
          );
49
        } else {
50
          var cl = CONFIG.maxallowedcharacters - ta.val().length;
51
          $("#addmynote-label-" + CONFIG.instanceid + "  span.warning").html(
52
            M.util.get_string("charactersleft", "block_cesa_notes") + cl
53
          );
54
        }
55
      }
56
    },
57
    checkInputText: function () {
58
      var ta = $("#id_mynotecontent-" + CONFIG.instanceid);
59
      if (ta.val().length <= CONFIG.maxallowedcharacters) {
60
        $("#addmynote_submit").removeAttr("disabled", "");
61
        return true;
62
      } else {
63
        $("#addmynote_submit").attr("disabled", "disabled");
64
        return false;
65
      }
66
      return true;
67
    },
68
    toggle_textarea: function (e) {
69
      var ta = $("#id_mynotecontent-" + CONFIG.instanceid);
70
 
71
      if (!ta) {
72
        return false;
73
      }
74
      var focus = e.type == "focusin";
75
      if (focus) {
76
        if (
77
          ta.val() ==
78
          M.util.get_string("placeholdercontent", "block_cesa_notes")
79
        ) {
80
          ta.val("");
81
          $(".textarea").css("border-color", "black");
82
        }
83
      } else {
84
        if (ta.val() == "") {
85
          ta.val(M.util.get_string("placeholdercontent", "block_cesa_notes"));
86
          $(".textarea").css("border-color", "gray");
87
          $("#addmynote-label-" + CONFIG.instanceid + "  span.warning").html(
88
            ""
89
          );
90
        }
91
      }
92
    },
93
    request: function (args) {
94
      var params = {};
95
      var scope = this;
96
      if (args["scope"]) {
97
        scope = args["scope"];
98
      }
99
      params["contextarea"] = scope.currenttab.replace(CONFIG.prefix, "");
100
      params["contextarea"] = params["contextarea"].replace("#", "");
101
      if (args.params) {
102
        for (i in args.params) {
103
          params[i] = args.params[i];
104
        }
105
      }
106
      params["sesskey"] = M.cfg.sesskey;
107
 
108
      var cfg = {
109
        method: "POST",
110
        on: {
111
          start: function () {
112
            //'<div class="mdl-align"><img src="'+M.util.image_url('i/loading', 'core')+'" /></div>';
113
          },
114
          complete: function (id, o, p) {
115
            if (!o) {
116
              alert("IO FATAL");
117
              return false;
1 efrain 118
            }
365 ariadna 119
            var data = Y.JSON.parse(o.responseText);
120
            if (data.error) {
121
              if (data.error == "require_login") {
122
                args.callback(id, data, p);
1 efrain 123
                return true;
365 ariadna 124
              }
125
              alert(data.error);
126
              return false;
1 efrain 127
            } else {
365 ariadna 128
              args.callback(id, data, p);
129
              return true;
1 efrain 130
            }
365 ariadna 131
          },
1 efrain 132
        },
365 ariadna 133
        arguments: {
134
          scope: scope,
1 efrain 135
        },
365 ariadna 136
        headers: {
137
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
1 efrain 138
        },
365 ariadna 139
        data: build_querystring(params),
140
      };
141
      if (args.form) {
142
        cfg.form = args.form;
143
      }
144
      Y.io(this.api, cfg);
145
    },
965 ariadna 146
 
365 ariadna 147
    printcesa_notes: function (e) {
148
      e.preventDefault();
149
      let note_content_input = $("#id_mynotecontent-" + CONFIG.instanceid);
150
      let arg = {
151
        action: "print",
152
      };
1 efrain 153
 
365 ariadna 154
      note_content_input.attr("disabled", true);
155
      note_content_input.css({
156
        backgroundImage:
157
          "url(" + M.util.image_url("i/loading_small", "core") + ")",
158
        backgroundRepeat: "no-repeat",
159
        backgroundPosition: "center center",
160
      });
1 efrain 161
 
365 ariadna 162
      this.request({
163
        params: arg,
164
        callback: function (id, ret, args) {
165
          $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr("disabled");
166
          $("#id_mynotecontent-" + CONFIG.instanceid).css({
167
            backgroundImage: "",
168
          });
1 efrain 169
        },
365 ariadna 170
      });
171
    },
965 ariadna 172
 
365 ariadna 173
    savecesa_notes: function (e) {
174
      e.preventDefault();
175
      var scope = this;
1 efrain 176
 
365 ariadna 177
      if (scope.checkInputText() == false) {
178
        return false;
179
      }
180
      var ta = $("#id_mynotecontent-" + CONFIG.instanceid);
181
      if (
182
        ta.val() == "" ||
183
        ta.val() == M.util.get_string("placeholdercontent", "block_cesa_notes")
184
      ) {
185
        return false;
186
      }
187
      let editingNoteId = ta.attr("data-editnoteid")
188
        ? ta
189
            .attr("data-editnoteid")
190
            .replace("mynote-" + CONFIG.instanceid + "-", "")
191
        : false;
1 efrain 192
 
365 ariadna 193
      var arg = {
194
        contextid: CONFIG.contextid,
195
        content: ta.val(),
196
        action: editingNoteId ? "edit" : "add",
197
        contextarea: scope.currenttabindex,
198
        noteurl: window.location.href,
199
        noteToEditIt: editingNoteId,
200
      };
1 efrain 201
 
365 ariadna 202
      ta.attr("disabled", true);
203
      ta.css({
204
        backgroundImage:
205
          "url(" + M.util.image_url("i/loading_small", "core") + ")",
206
        backgroundRepeat: "no-repeat",
207
        backgroundPosition: "center center",
208
      });
209
      this.request({
210
        params: arg,
211
        callback: function (id, ret, args) {
212
          if (!ret.notes) {
213
            return false;
214
          }
215
 
216
          $("#addmynote-label-" + CONFIG.instanceid + "  span.warning").html(
217
            ""
218
          );
219
          $("#id_mynotecontent-" + CONFIG.instanceid).val(
220
            M.util.get_string("placeholdercontent", "block_cesa_notes")
221
          );
222
          $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr("disabled");
223
          $("#id_mynotecontent-" + CONFIG.instanceid).css({
224
            backgroundImage: "",
225
          });
226
          if (scope.currenttab != scope.defaulttab) {
227
            scope.currenttab = scope.defaulttab;
228
            var tab = scope.currenttab.replace("#", "#tab-");
229
            $(SELECTORS.cesa_notes_BASE + " ul.tabs-menu li").removeClass(
230
              "current"
1 efrain 231
            );
365 ariadna 232
            $(SELECTORS.cesa_notes_BASE + " " + tab).addClass("current");
233
            $(SELECTORS.cesa_notes_BASE + " .tab-content")
234
              .has(scope.currenttab)
235
              .addClass("current");
236
            $(SELECTORS.cesa_notes_BASE + " .tab-content")
237
              .not(scope.currenttab)
238
              .css("display", "none");
239
            $(
240
              SELECTORS.cesa_notes_BASE +
241
                " " +
242
                scope.currenttab +
243
                ".tab-content"
244
            ).css("display", "block");
245
          }
246
          scope.addToList(ret, "add");
247
          scope.displaycesa_notes();
248
          $(SELECTORS.cesa_notes_BASE)
249
            .find(".responsetext")
250
            .html(M.util.get_string("savedsuccess", "block_cesa_notes"));
251
          $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr(
252
            "data-editnoteid"
253
          );
254
 
255
          $(".j-delete-cesa-note").removeClass("show_delete_note_btn");
1 efrain 256
        },
365 ariadna 257
      });
258
    },
259
    addToList: function (notesobj, action = "") {
260
      var scope = this;
261
      var el = $(SELECTORS.cesa_notes_BASE).find(scope.currenttab + "-list");
262
      console.log(el);
263
      if (action == "add") {
264
        console.log(notesobj.notes);
265
        el.prepend(scope.rendercesa_notes(notesobj.notes));
266
      } else {
267
        el.append(scope.rendercesa_notes(notesobj.notes));
268
        $(el)
269
          .find("li")
270
          .sort(sort_li) // sort elements
271
          .appendTo(el); // append again to the list
272
        // sort function callback
273
        function sort_li(a, b) {
274
          return $(b).data("itemid") > $(a).data("itemid") ? 1 : -1;
275
        }
276
      }
277
      $(SELECTORS.cesa_notes_BASE)
278
        .find(scope.currenttab)
279
        .attr("notes-count", notesobj.count);
280
    },
281
    getcesa_notes: function (page = 0) {
282
      var scope = this;
283
      page = parseInt(page);
284
      var el = $(SELECTORS.cesa_notes_BASE).find(scope.currenttab + "-list");
285
      var notescount = el.find("li").length;
286
      var lastpage = Math.ceil(notescount / CONFIG.perpage);
287
      if (notescount > 0 && lastpage > page) {
288
        scope.displaycesa_notes();
289
        return false;
290
      }
291
      var arg = {
292
        contextid: CONFIG.contextid,
293
        action: "get",
294
        page: page,
295
      };
296
      this.request({
297
        params: arg,
298
        callback: function (id, ret, args) {
299
          scope.addToList(ret);
300
          scope.displaycesa_notes();
1 efrain 301
        },
365 ariadna 302
      });
303
    },
304
    updatecesa_notesInfo: function (cesa_notescount, page) {
305
      page = parseInt(page);
306
      cesa_notescount = parseInt(cesa_notescount);
307
      var scope = this;
308
      var paging = "";
309
      if (cesa_notescount > CONFIG.perpage) {
310
        var pagenum = page - 1;
311
        var prevlink = "";
312
        var nextlink = "";
1 efrain 313
 
365 ariadna 314
        if (page > 0) {
315
          prevlink = scope.createLink(
316
            pagenum,
317
            M.util.get_string("previouspage", "block_cesa_notes"),
318
            "previous"
319
          );
320
        }
321
        if (CONFIG.perpage > 0) {
322
          var lastpage = Math.ceil(cesa_notescount / CONFIG.perpage);
323
        } else {
324
          var lastpage = 1;
325
        }
326
        // Uncomment this line if you want to display page number
327
        //paging += '<span class="current-page">' + (page + 1) + '</span>';
328
        pagenum = page + 1;
329
        if (pagenum != lastpage) {
330
          nextlink = scope.createLink(
331
            pagenum,
332
            M.util.get_string("nextpage", "block_cesa_notes"),
333
            "next"
334
          );
335
        }
336
        paging = prevlink;
337
        if (prevlink != "" && nextlink != "") {
338
          paging += '<span class="separator"></span>';
339
        }
340
        paging += nextlink;
1 efrain 341
 
365 ariadna 342
        paging = '<span class="paging">' + paging + "</span>";
343
      }
344
      var noteinfo = $(SELECTORS.cesa_notes_BASE).find(scope.currenttab);
345
      if (cesa_notescount > 0) {
346
        noteinfo
347
          .find(".count")
348
          .html(
349
            M.util.get_string("cesa_notescount", "block_cesa_notes") +
350
              "" +
351
              cesa_notescount
352
          );
353
      } else {
354
        noteinfo
355
          .find(".count")
356
          .html(M.util.get_string("nothingtodisplay", "block_cesa_notes"));
357
      }
358
      noteinfo.find(".cesa_notes-paging").html(paging);
359
    },
360
    /*
361
     * Render notes as html ul li element
362
     */
363
    rendercesa_notes: function (notes) {
364
      if (notes.length < 1) {
365
        return false;
366
      }
367
      var lists = "";
368
      var x = "";
369
      for (x in notes) {
370
        $("#mynote-" + CONFIG.instanceid + "-" + notes[x].id).remove();
371
        var deletelink =
372
          '<a href="#" id="mynote-delete-' +
373
          CONFIG.instanceid +
374
          "-" +
375
          notes[x].id +
376
          '" class="mynote-delete" title="' +
377
          strdeletenote +
378
          '">' +
379
          NODES.DELETE_ICON +
380
          "</a>";
381
        var notedetail = "";
382
        var userdate = '<span class="time">' + notes[x].timecreated + "</span>";
383
        if (notes[x].coursename != "") {
384
          notedetail =
385
            '<div class="note-detail">' +
386
            notes[x].coursename +
387
            " - " +
388
            userdate +
389
            "</div>";
390
        } else {
391
          notedetail = '<div class="note-detail">' + userdate + "</div>";
392
        }
1 efrain 393
 
365 ariadna 394
        var note_html =
395
          '<div class="content" data-noteid="mynote-' +
396
          CONFIG.instanceid +
397
          "-" +
398
          notes[x].id +
399
          '">' +
400
          notes[x].content +
401
          "</div>";
402
        var note_edit_btn =
403
          '<button class="j-cesa-note-content-edit">Editar</button>';
404
        lists +=
405
          '<li id="mynote-' +
406
          CONFIG.instanceid +
407
          "-" +
408
          notes[x].id +
409
          '" data-itemid="' +
410
          notes[x].id +
411
          '">' +
412
          note_edit_btn +
413
          note_html +
414
          notedetail +
415
          "</li>";
416
      }
417
      return lists;
418
    },
419
    createLink: function (page, text, classname) {
420
      var classattribute =
421
        typeof classname != "undefined" ? ' class="' + classname + '"' : "";
422
      return (
423
        '<a href="' +
424
        this.api +
425
        "&page=" +
426
        page +
427
        '"' +
428
        classattribute +
429
        ">" +
430
        text +
431
        "</a>"
432
      );
433
    },
434
    displaycesa_notes: function () {
435
      var scope = this;
436
      var page = parseInt(
437
        $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).attr("onpage")
438
      );
439
      var cesa_notescount = parseInt(
440
        $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).attr("notes-count")
441
      );
442
      var el = $(SELECTORS.cesa_notes_BASE).find(
443
        " " + scope.currenttab + "-list"
444
      );
445
      var notescount = el.find("li").length;
446
      var lastpage = Math.ceil(notescount / CONFIG.perpage);
447
 
448
      if (notescount > 0 && lastpage <= page) {
449
        page = lastpage - 1;
450
      }
451
      var upperlimit = page * CONFIG.perpage + CONFIG.perpage;
452
      var lowerlimit = page * CONFIG.perpage;
453
      el.find("li").css("display", "none");
454
      el.find("li").each(function (i, el) {
455
        if (i >= lowerlimit && i < upperlimit) {
456
          $(el).css("display", "block");
457
        }
458
      });
459
      scope.updatecesa_notesInfo(cesa_notescount, page);
460
      //panel.centerDialogue();
461
    },
462
    registerActions: function () {
463
      var scope = this;
464
 
465
      $("body").delegate("#addmynote_cancel", "click", function () {
466
        panel.hide();
467
      });
468
 
469
      $("body").delegate(".j-cesa-note-content-edit", "click", function () {
470
        let noteText = $(this).parent().find(".text_to_html").text();
471
        let textArea = $(".cesa_notes_base .textarea textarea");
472
        let deleteButton = $(".j-delete-cesa-note");
473
 
474
        deleteButton.addClass("show_delete_note_btn");
475
        textArea.attr(
476
          "data-editnoteid",
477
          $(this).parent().find(".content").attr("data-noteid")
478
        );
479
        textArea.val(noteText);
480
      });
481
      $("body").delegate(
482
        ".modal_cesa_notes_root .modal-header button.close",
483
        "click",
484
        function (e) {
485
          $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr(
486
            "data-editnoteid"
487
          );
488
          $("#id_mynotecontent-" + CONFIG.instanceid).val("");
489
          $(".j-delete-cesa-note").removeClass("show_delete_note_btn");
490
          $("#cesa_notes_delete_modal").removeClass("show_delete_modal");
491
        }
492
      );
493
      $("body").delegate(
494
        "#cesa_notes_modal_cancel_delete",
495
        "click",
496
        function (e) {
497
          $("#cesa_notes_delete_modal").removeClass("show_delete_modal");
498
        }
499
      );
500
      $("body").delegate(
501
        "#cesa_notes_modal_accept_delete",
502
        "click",
503
        function (e) {
504
          var nid = $("#id_mynotecontent-" + CONFIG.instanceid).attr(
505
            "data-editnoteid"
506
          );
507
          if (nid != "" || nid != "undefined") {
508
            var notescount = $(SELECTORS.cesa_notes_BASE).find(
509
              SELECTORS.cesa_notes_LISTS + "-" + scope.currenttab + " > li"
510
            ).length;
511
            var id = nid.replace("mynote-" + CONFIG.instanceid + "-", "");
512
            var arg = {
513
              contextid: CONFIG.contextid,
514
              action: "delete",
515
              noteid: id,
516
              lastnotecounts: notescount,
517
            };
518
            scope.request({
519
              params: arg,
520
              callback: function (id, ret, args) {
521
                args.scope.addToList(ret);
522
                $("#mynote-" + CONFIG.instanceid + "-" + ret.noteid).remove();
523
                $(".j-delete-cesa-note").removeClass("show_delete_note_btn");
524
                $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr(
525
                  "data-editnoteid"
526
                );
527
                $("#id_mynotecontent-" + CONFIG.instanceid).val("");
528
                $("#cesa_notes_delete_modal").removeClass("show_delete_modal");
529
                args.scope.displaycesa_notes();
530
              },
1 efrain 531
            });
365 ariadna 532
          }
533
        }
534
      );
535
      $("body").delegate("#addmynote_submit", "click", function (e) {
536
        scope.savecesa_notes(e);
537
      });
538
      //$('body').delegate('#cesa_notes_print', 'click', function(e) {scope.printcesa_notes(e)});
539
      $("body").delegate(
540
        SELECTORS.cesa_notes_BASE + " ul.tabs-menu li",
541
        "click",
542
        function (e) {
543
          $(this).addClass("current");
544
          $(this).siblings().removeClass("current");
545
          var tab = $(this).attr("id").replace("tab-", "");
546
          $(SELECTORS.cesa_notes_BASE + " .tab-content")
547
            .not("#" + tab)
548
            .css("display", "none");
549
          $(SELECTORS.cesa_notes_BASE + " #" + tab + ".tab-content").css(
550
            "display",
551
            "block"
552
          );
553
          scope.currenttab = "#" + tab;
1 efrain 554
 
365 ariadna 555
          var isloaded = $(scope.currenttab).attr("data-loaded");
556
          if (typeof isloaded == "undefined" || isloaded == false) {
557
            $(SELECTORS.cesa_notes_BASE)
558
              .find(scope.currenttab)
559
              .attr("data-loaded", "true");
560
            scope.getcesa_notes(0);
561
          }
562
        }
563
      );
564
 
565
      $("body").delegate(
566
        "#id_mynotecontent-" + CONFIG.instanceid,
567
        "focus blur",
568
        function (e) {
569
          scope.toggle_textarea(e);
570
        }
571
      );
572
      $("body").delegate(
573
        "#id_mynotecontent-" + CONFIG.instanceid,
574
        "change keypress keyup",
575
        function (e) {
576
          scope.getWarnings(scope.checkInputText());
577
        }
578
      );
579
 
580
      $("body").delegate(
581
        SELECTORS.cesa_notes_BASE + " .cesa_notes-paging .paging a",
582
        "click",
583
        function (e) {
584
          e.preventDefault();
585
          var regex = new RegExp(/[\?&]page=(\d+)/);
586
          var results = regex.exec($(this).attr("href"));
587
          var page = 0;
588
          if (results[1]) {
589
            page = results[1];
590
          }
591
          $(SELECTORS.cesa_notes_BASE)
592
            .find(scope.currenttab)
593
            .attr("onpage", parseInt(page));
594
          scope.getcesa_notes(page);
595
        }
596
      );
597
 
598
      $("body").delegate(
599
        SELECTORS.cesa_notes_BASE + " .j-delete-cesa-note",
600
        "click",
601
        function (e) {
602
          e.preventDefault();
603
          var nid = $("#id_mynotecontent-" + CONFIG.instanceid).attr(
604
            "data-editnoteid"
605
          );
606
          if (nid != "" || nid != "undefined") {
607
            let deleteModal = $("#cesa_notes_delete_modal");
608
            deleteModal.addClass("show_delete_modal");
609
          }
610
        }
611
      );
612
      $("body").delegate(
613
        SELECTORS.cesa_notes_BASE + " a.mynote-delete",
614
        "click",
615
        function (e) {
616
          e.preventDefault();
617
          $("#id_mynotecontent-" + CONFIG.instanceid).removeAttr(
618
            "data-editnoteid"
619
          );
620
          var nid = $(this).attr("id");
621
          if (nid != "" || nid != "undefined") {
622
            var notescount = $(SELECTORS.cesa_notes_BASE).find(
623
              SELECTORS.cesa_notes_LISTS + "-" + scope.currenttab + " > li"
624
            ).length;
625
            var id = nid.replace(
626
              "mynote-delete-" + CONFIG.instanceid + "-",
627
              ""
628
            );
629
            var arg = {
630
              contextid: CONFIG.contextid,
631
              action: "delete",
632
              noteid: id,
633
              lastnotecounts: notescount,
634
            };
635
            scope.request({
636
              params: arg,
637
              callback: function (id, ret, args) {
638
                args.scope.addToList(ret);
639
                $("#mynote-" + CONFIG.instanceid + "-" + ret.noteid).remove();
640
                args.scope.displaycesa_notes();
641
              },
1 efrain 642
            });
365 ariadna 643
          }
644
        }
645
      );
646
    },
982 ariadna 647
    displayDialogue: function (e) {
648
      var scope = cesa_notes;
649
      if (panel === null) {
650
        str
651
          .get_strings([
652
            { key: "cesa_notes", component: "block_cesa_notes" },
653
            { key: "characterlimit", component: "block_cesa_notes" },
654
            { key: "save", component: "block_cesa_notes" },
655
            { key: "cancel" },
656
            {
657
              key: "cesa_notessavedundertab",
658
              component: "block_cesa_notes",
659
              param: CONFIG.contextareas[scope.currenttabindex],
660
            },
661
            { key: "placeholdercontent", component: "block_cesa_notes" },
662
          ])
663
          .done(function (s) {
664
            // Create basic tab structure
665
 
666
            let deleteModal = `<div id="cesa_notes_delete_modal">
667
                                        <div class="cesa_notes_delete_modal_content">
668
                                            <p>¿Estás seguro de que quieres eliminar este apunte?</p>
669
                                            <button id="cesa_notes_modal_cancel_delete">Cancelar</button>
670
                                            <button id="cesa_notes_modal_accept_delete">Eliminar</button>
671
                                        </div>
672
                                        </div>`;
673
            var el = $("<div></div>").append(
674
              $(
675
                '<div id="' +
676
                  CSS.cesa_notes_BASE +
677
                  '" class="' +
678
                  CSS.cesa_notes_BASE +
679
                  '">' +
680
                  deleteModal +
681
                  "</div>"
682
              )
683
                .append(
684
                  '<div class="inputarea"><div class="responsetext"></div><div class="cesa-notes-limit" id="addmynote-label-' +
685
                    CONFIG.instanceid +
686
                    '">' +
687
                    s[1] +
688
                    " " +
689
                    CONFIG.maxallowedcharacters +
690
                    '<span class="warning"></span></div>' +
691
                    '<div class="textarea"><textarea id="id_mynotecontent-' +
692
                    CONFIG.instanceid +
693
                    '" name="mynotecontent" rows="2">' +
694
                    s[5] +
695
                    "</textarea></div>" +
696
                    '<p class="notesavedhint">' +
697
                    s[4] +
698
                    "</p>" +
699
                    '<p class="cesa_notes_buttons_container"><button class="j-delete-cesa-note">Eliminar</button><input type="submit" id="addmynote_submit"/> </p>' +
700
                    '<p class="cesa_note_bottom_container"><a class="cesa_notes_print" target="_blank" href="' +
701
                    CONFIG.printapi +
702
                    '">Imprimir notas</a> </p>' +
703
                    "</div>"
704
                )
705
                .append($('<ul class="tabs-menu"></ul>'))
706
                .append($('<div class="tab"></div>'))
707
            );
708
            el.find("#addmynote_submit").attr("value", s[2]);
709
            el.find("#addmynote_cancel").attr("value", s[3]);
710
            var tabsmenu = "";
711
            var tabcontents = "";
712
            var i = "";
713
            for (i in CONFIG.contextareas) {
714
              if (scope.currenttabindex == i) {
715
                tabsmenu +=
716
                  '<li class="current" id="tab-' +
717
                  CONFIG.prefix +
718
                  i +
719
                  '"><div class="menu-item">' +
720
                  CONFIG.contextareas[i] +
721
                  "</div></li>";
722
              } else {
723
                tabsmenu +=
724
                  '<li class="" id="tab-' +
725
                  CONFIG.prefix +
726
                  i +
727
                  '"><div class="menu-item">' +
728
                  CONFIG.contextareas[i] +
729
                  "</div></li>";
730
              }
731
              tabcontents +=
732
                '<div class="tab-content" id="' +
733
                CONFIG.prefix +
734
                i +
735
                '" onpage="0" notes-count="0">' +
736
                '<div class="notes-info"><div class="cesa_notes-paging"></div><div class="count"></div></div>' +
737
                '<ul id="' +
738
                CONFIG.prefix +
739
                i +
740
                '-list" class="cesa_notes_lists"></ul>' +
741
                "</div>";
742
            }
743
            el.find(".tabs-menu").append(tabsmenu);
744
            el.find(".tab").append($(tabcontents));
745
            ModalFactory.create({
746
              title: s[0],
747
              body: el.html(),
748
            }).then(function (modal) {
749
              if (initnotes === null) {
750
                initnotes = true;
751
                // Get initial notes
752
                scope.getcesa_notes(0);
753
                $(SELECTORS.cesa_notes_BASE)
754
                  .find(scope.currenttab)
755
                  .attr("data-loaded", "true");
756
                $(SELECTORS.cesa_notes_BASE)
757
                  .find(scope.currenttab)
758
                  .css("display", "block");
759
              }
760
 
761
              let root = modal.getRoot();
762
 
763
              root.addClass("modal_cesa_notes_root");
764
 
765
              panel = modal;
766
              panel.show();
767
              scope.registerActions();
768
            });
769
 
770
            /*if (initnotes === null) {
771
                        initnotes = true;
772
                        // Get initial notes
773
                        scope.getcesa_notes(0);
774
                        $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).attr('data-loaded', "true");
775
                        $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).css('display', 'block');
776
                    }
777
 
778
                    panel.show();*/
779
            /*Y.use('moodle-core-notification-dialogue', function() {
780
 
781
                         // TODO: CESA_NOTES Cambiar esto para que no use yui sino https://docs.moodle.org/dev/AMD_Modal
782
 
783
                        panel = new M.core.dialogue({
784
                            draggable: true,
785
                            modal: true,
786
                            closeButton: true,
787
                            headerContent: M.util.get_string('cesa_notes', 'block_cesa_notes'),
788
                            responsive: true,
789
                            classname: 'testing'
790
                        });
791
 
792
                        panel.set('bodyContent', el.html());
793
                        if (initnotes === null) {
794
                            initnotes = true;
795
                            // Get initial notes
796
                            scope.getcesa_notes(0);
797
                            $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).attr('data-loaded', "true");
798
                            $(SELECTORS.cesa_notes_BASE).find(scope.currenttab).css('display', 'block');
799
                        }
800
                        panel.show();
801
                    });*/
802
            //scope.registerActions();
803
          });
804
      } else {
805
        panel.show();
806
      }
807
    },
981 ariadna 808
    /**
809
     * Initialize cesa_notes
810
     * @access public
811
     * @param {int} instanceid
812
     * @param {int} contextid
813
     * @param {int} maxallowedcharacters
814
     * @param {int} perpage
815
     * @param {string} editingicon_pos
816
     * @param {bool} editing
817
     * @param {string} adminurl
818
     * @param {array} contextareas
819
     * @param {string} currenttabindex
820
     */
821
    init: function (args) {
822
      CONFIG = args;
823
      CONFIG.prefix = "cesa_notes_";
824
      CONFIG.printapi = this.getcesa_notesValidatedUrl(
825
        M.cfg.wwwroot + "/blocks/cesa_notes/cesa_notes_print.php"
826
      );
827
      this.perpage = parseInt(CONFIG.perpage);
828
      this.currenttab = "#cesa_notes_" + args.currenttabindex;
829
      this.defaulttab = "#cesa_notes_" + args.currenttabindex;
830
      this.currenttabindex = args.currenttabindex;
831
      this.api = this.getcesa_notesValidatedUrl(
832
        M.cfg.wwwroot + "/blocks/cesa_notes/cesa_notes_ajax.php"
833
      );
834
      var strtitle = M.util.get_string("showcesa_notes", "block_cesa_notes");
996 ariadna 835
 
836
      console.log('Execute init')
997 ariadna 837
 
998 ariadna 838
      var scope = cesa_notes;
839
 
997 ariadna 840
      str
841
      .get_strings([
842
        { key: "cesa_notes", component: "block_cesa_notes" },
843
        { key: "characterlimit", component: "block_cesa_notes" },
844
        { key: "save", component: "block_cesa_notes" },
845
        { key: "cancel" },
846
        {
847
          key: "cesa_notessavedundertab",
848
          component: "block_cesa_notes",
849
          param: CONFIG.contextareas[scope.currenttabindex],
850
        },
851
        { key: "placeholdercontent", component: "block_cesa_notes" },
852
      ])
853
      .done(function (s) {
854
        // Create basic tab structure
855
 
856
        let deleteModal = `<div id="cesa_notes_delete_modal">
857
                                    <div class="cesa_notes_delete_modal_content">
858
                                        <p>¿Estás seguro de que quieres eliminar este apunte?</p>
859
                                        <button id="cesa_notes_modal_cancel_delete">Cancelar</button>
860
                                        <button id="cesa_notes_modal_accept_delete">Eliminar</button>
861
                                    </div>
862
                                    </div>`;
863
 
864
        var el = $("<div></div>").append(
865
          $(
866
            '<div id="' +
867
              CSS.cesa_notes_BASE +
868
              '" class="' +
869
              CSS.cesa_notes_BASE +
870
              '">' +
871
              deleteModal +
872
              "</div>"
873
          )
874
            .append(
875
              '<div class="inputarea"><div class="responsetext"></div><div class="cesa-notes-limit" id="addmynote-label-' +
876
                CONFIG.instanceid +
877
                '">' +
878
                s[1] +
879
                " " +
880
                CONFIG.maxallowedcharacters +
881
                '<span class="warning"></span></div>' +
882
                '<div class="textarea"><textarea id="id_mynotecontent-' +
883
                CONFIG.instanceid +
884
                '" name="mynotecontent" rows="2">' +
885
                s[5] +
886
                "</textarea></div>" +
887
                '<p class="notesavedhint">' +
888
                s[4] +
889
                "</p>" +
890
                '<p class="cesa_notes_buttons_container"><button class="j-delete-cesa-note">Eliminar</button><input type="submit" id="addmynote_submit"/> </p>' +
891
                '<p class="cesa_note_bottom_container"><a class="cesa_notes_print" target="_blank" href="' +
892
                CONFIG.printapi +
893
                '">Imprimir notas</a> </p>' +
894
                "</div>"
895
            )
896
            .append($('<ul class="tabs-menu"></ul>'))
897
            .append($('<div class="tab"></div>'))
898
        );
899
        el.find("#addmynote_submit").attr("value", s[2]);
900
        el.find("#addmynote_cancel").attr("value", s[3]);
901
        var tabsmenu = "";
902
        var tabcontents = "";
903
        var i = "";
904
        for (i in CONFIG.contextareas) {
905
          if (scope.currenttabindex == i) {
906
            tabsmenu +=
907
              '<li class="current" id="tab-' +
908
              CONFIG.prefix +
909
              i +
910
              '"><div class="menu-item">' +
911
              CONFIG.contextareas[i] +
912
              "</div></li>";
913
          } else {
914
            tabsmenu +=
915
              '<li class="" id="tab-' +
916
              CONFIG.prefix +
917
              i +
918
              '"><div class="menu-item">' +
919
              CONFIG.contextareas[i] +
920
              "</div></li>";
921
          }
922
          tabcontents +=
923
            '<div class="tab-content" id="' +
924
            CONFIG.prefix +
925
            i +
926
            '" onpage="0" notes-count="0">' +
927
            '<div class="notes-info"><div class="cesa_notes-paging"></div><div class="count"></div></div>' +
928
            '<ul id="' +
929
            CONFIG.prefix +
930
            i +
931
            '-list" class="cesa_notes_lists"></ul>' +
932
            "</div>";
933
        }
934
        el.find(".tabs-menu").append(tabsmenu);
935
        el.find(".tab").append($(tabcontents));
978 ariadna 936
 
997 ariadna 937
        $(SELECTORS.cesa_notes_ROOT).html(el.html());
938
 
939
        /* if (initnotes === null) {
940
          initnotes = true;
941
          // Get initial notes
942
          scope.getcesa_notes(0);
943
          $(SELECTORS.cesa_notes_BASE)
944
            .find(scope.currenttab)
945
            .attr("data-loaded", "true");
946
          $(SELECTORS.cesa_notes_BASE)
947
            .find(scope.currenttab)
948
            .css("display", "block");
949
        }
950
 
951
        scope.registerActions(); */
952
      });
953
 
954
 
365 ariadna 955
    },
956
  };
739 ariadna 957
 
365 ariadna 958
  return cesa_notes;
959
});