1CKEDITOR.dialog.add( 'tagsDialog', function ( editor )
2  {
3    var existingTags = {};
4    var lang = editor.lang.tags;
5    var tstr = "test";
6    var tags_pos = editor.tags_pos();
7    var generateTags = function ()
8      {
9
10        var content = editor.document.$.children[0].children[1].innerHTML.replace(/<[^<>]*>/g,'').replace(/&gt;/g,'>');
11
12        var tagregexp = (/\{\{tag>.*?\}\}/g);
13        var matches = tagregexp.exec(content);
14        var ele = [];
15        var tagexists = function (name)
16          {
17            if (matches != null) {
18              var wordmatch = new RegExp('( |\>)'+ name.toUpperCase()+'( |\})','g');
19              for (var i = 0; i<matches.length; i++) {
20                if (wordmatch.test(matches[i].toUpperCase())) {
21                  return true;
22                }
23              }
24              return false;
25            } else {
26              return false;
27            }
28          };
29	  existingTags = editor.existing_tags();
30        var curHBox = {
31                        type: 'hbox',
32                        children: []
33                      }
34        ele[ele.length] = curHBox;
35        for (var i=0; i<existingTags.length; i++) {
36          curHBox.children[curHBox.children.length] =
37            {
38              type: 'checkbox',
39              id: existingTags[i].id,
40              label: existingTags[i].name,
41              'default': existingTags[i].name == '' ? '' : (tagexists(existingTags[i].id) ? 'checked' : ''),
42              userdef: false
43            }
44          if ( ((i % 5)==4) && ((i+1)<existingTags.length-1)) {
45            curHBox = {
46                        type: 'hbox',
47                        children: []
48                      }
49            ele[ele.length] = curHBox;
50          }
51        }
52        ele[ele.length] =
53            {
54              type: 'checkbox',
55              id: '',
56              label: '',
57              'default': '',
58              userdef: false
59            }
60
61        var tmp = ele[ele.length-1];
62        tmp.userdef = true;
63        tmp.id = 'user_def_box';
64        tmp.label = lang.customTags
65        ele[ele.length-1] =
66          {
67            type: 'vbox',
68            children: [
69                tmp,
70                {
71                    type: 'text',
72                    id: 'user_def_text',
73                },
74            {
75                type: 'html',
76                html: lang.info
77                //'If <code>Cursor</code> is selected, the old tag syntax must be selected and it will be replaced. If there are no old tags, the the new tag(s) will  be inserted at the cursor'
78         },
79        {
80            type: 'radio',
81            id: 'which',
82            label: lang.radioLabel, // 'Place tags at: ',
83            items: [ [ lang.bottom, 'bottom' ], [ lang.cursor, 'cursor' ], [lang.top, 'top'] ],
84            style: 'color: green',
85            'default': tags_pos,
86            onClick: function() {
87                //alert( 'Current value: ' + this.getValue() );
88            }
89        },
90            ]
91          }
92
93        return ele;
94      };
95    var d =
96      {
97        title:  lang.dlgTitle,
98        minWidth: 500,
99        minHeight: 200,
100        contents: [
101            {
102                id: 'tab-basic',
103                label: 'Settings',
104                elements: generateTags()
105            }
106        ],
107        onOk: function()
108          {
109            var dialog = this;
110            var codeB = "{{tag>";
111            var codeE = "}}";
112            var selected = "";
113            var which = 'unset';
114            var dialog = this;
115            for (var i = 0; i<existingTags.length; i++) {
116              selected += dialog.getValueOf('tab-basic',existingTags[i].id) ? existingTags[i].id+' ' : '';
117            }
118            selected += dialog.getValueOf('tab-basic','user_def_box') ? dialog.getValueOf('tab-basic','user_def_text')+' ' : '';
119            which = dialog.getValueOf('tab-basic','which') ;
120            selected = selected.replace(/\s+$/,"");
121            data = editor.getData();
122            selected = "{{tag&gt;" + selected + "}}";
123            if(which == 'cursor') {
124                 editor.insertHtml(selected);
125            }
126            else if(which == 'top')  {
127                 editor.setData(selected+data.replace(/\{\{tag&gt;.*?\}\}/g,''));
128            }
129            else {
130                editor.setData(data.replace(/\{\{tag&gt;.*?\}\}/g,'')+selected);
131                }
132
133          },
134        onShow: function() {
135
136          var dialog = this;
137          var existing ="";
138          var on_page;  //tags found in page tag markup
139          var custom_insert = "";  // tags found on page that are not in existing tags
140          for (var i = 0; i<existingTags.length; i++) {
141              existing += dialog.getValueOf('tab-basic',existingTags[i].id) ? existingTags[i].id+' ' : '';   // check checkboxes for current tags
142            }
143     //     alert(existing  + '<');
144          var content = editor.document.$.children[0].children[1].innerHTML.replace(/<[^<>]*>/g,'').replace(/&gt;/g,'>');
145          var tagregexp = (/\{\{tag>(.*?)\}\}/g);
146          var matches = tagregexp.exec(content);
147         if(!matches) return;
148          matches[1] = matches[1].replace(/&nbsp;/g, "  ");
149          matches[1] = matches[1].replace(/\s+/g," " );
150
151         on_page = matches[1].split(/\s/);
152         /* Bring checkboxes up-to-date */
153          jQuery("table[class*='dialog'] :checkbox[class*='cke']").each(function( index ) {
154                    var labelText = jQuery('label[for='+  this.id  +']').text();
155                    var tag = labelText.match(/^[\w\-\d]+$/);
156                   if(tag && !on_page.includes(labelText)) {
157                         jQuery(this).prop('checked',false) ;
158                   }
159                   else if(tag && on_page.includes(labelText) && !jQuery(this).prop('checked')){
160                        jQuery(this).prop('checked',true) ;
161                        existing += labelText + ' ';
162                   }
163           });
164
165        /* Add as yet un-registered tags to list of custom tags   */
166         for(i=0; i< on_page.length; i++) {
167               var r= new RegExp(on_page[i]);
168              if(!existing.match(r)) {
169                  custom_insert += on_page[i] + " ";
170              }
171         }
172           if(custom_insert) {    // add custom tags to custom tags box
173              custom_insert=custom_insert.replace(/\s+$/,"");
174              dialog.setValueOf('tab-basic','user_def_box',1) ;
175              dialog.setValueOf('tab-basic','user_def_text',custom_insert);
176           }
177
178        }
179      };
180    return d;
181  }
182);
183