Completed
Branch master (ebb499)
by Alexey
04:15
created

system/modules/Ui/static/js/fastEdit.js   A

Complexity

Total Complexity 12
Complexity/F 2

Size

Lines of Code 57
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 12
mnd 3
bc 13
fnc 6
bpm 2.1666
cpm 2
noi 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A fastEdit.js ➔ $ 0 3 1
A fastEdit.js ➔ toggleEditor 0 51 2
1
2
var isEditingEnabled;
3
var widgets = {};
4
5
function toggleEditor(btn) {
6
  if (!isEditingEnabled) {
7
    $.each($('.fastEdit'), function () {
8
      if ($(this).closest('a').length > 0) {
9
        var a = $(this).closest('a');
10
        a.replaceWith("<div class = 'historyA " + a.attr('class') + "' style = '" + a.attr('style') + "' href = '" + a.attr('href') + "'>" + a.html() + "</div>");
11
      }
12
    });
13
    $.each($('.fastEdit'), function () {
14
      $(this).attr('contenteditable', true);
15
      $(this).css('position', 'relative');
16
      html = $(this).html();
0 ignored issues
show
Bug introduced by
The variable html seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.html.
Loading history...
17
      var regex = /<!--start:(.*)-->([\s\S]*?)<!--end:\1-->/ig;
18
      while (match = regex.exec(html)) {
0 ignored issues
show
Bug introduced by
The variable match seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.match.
Loading history...
19
        if (match) {
20
          widgets[match[1]] = match[0];
21
        }
22
      }
23
      $(this).html(html.replace(/<!--start:(.*)-->([\s\S]*?)<!--end:\1-->/ig, '$1'));
24
25
      $(this).ckeditor({
26
        //removePlugins: 'stylescombo',
27
        //startupFocus: true,
28
        customConfig: '/static/moduleAsset/libs/libs/ckeditor/inline_config.js'
29
      });
30
31
    });
32
    isEditingEnabled = true;
33
    $(btn).text('Отключить редактирование');
34
  } else {
35
    $.each($('.fastEdit'), function () {
36
      $(this).ckeditor().editor.destroy();
37
      $(this).removeAttr('contenteditable');
38
      $(this).css('position', 'static');
39
      //$(this).attr('class', 'fastEdit');
40
      for (key in widgets) {
0 ignored issues
show
Bug introduced by
The variable key seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.key.
Loading history...
41
        $(this).html($(this).html().replace(new RegExp(key, "gm"), widgets[key]));
42
      }
43
    });
44
    isEditingEnabled = false;
45
    $.each($('.fastEdit'), function () {
46
      if ($(this).closest('.historyA').length > 0) {
47
        var a = $(this).closest('.historyA');
48
        a.removeClass('historyA');
49
        a.replaceWith("<a class = '" + a.attr('class') + "' style = '" + a.attr('style') + "' href = '" + a.attr('href') + "'>" + a.html() + "</a>");
50
      }
51
    });
52
    $(btn).text('Включить редактирование');
53
  }
54
  return;
55
}
56
$(function () {
57
  $('body').append("<div class ='btn-group' style = 'position:fixed;right:0;top:0;z-index:100000;' ><button onclick='toggleEditor(this);return false;' class ='btn btn-default btn-xs' >Включить редактирование</button></div>");
58
})
59