1CKEDITOR.dialog.add( 'tagsDialog', function ( editor )
2  {
3    var existingTags = {};
4    var lang = editor.lang.tags;
5
6    var generateTags = function ()
7      {
8
9        var content = editor.document.$.children[0].children[1].innerHTML.replace(/<[^<>]*>/g,'').replace(/&gt;/g,'>');
10
11        var tagregexp = (/\{\{tag>.*?\}\}/g);
12        var matches = tagregexp.exec(content);
13        var ele = [];
14        var tagexists = function (name)
15          {
16            if (matches != null) {
17              eval('var wordmatch = /( |\>)'+name.toUpperCase()+'( |\})/g');
18              for (var i = 0; i<matches.length; i++) {
19                if (wordmatch.test(matches[i].toUpperCase())) {
20                  return true;
21                }
22              }
23              return false;
24            } else {
25              return false;
26            }
27          };
28        jQuery.ajax(
29          DOKU_BASE + 'lib/exe/ajax.php',
30          {
31            data:
32              {
33                call: 'tagapi_list'
34              },
35            type: "POST",
36            async: false,
37            dataType: "json",
38            success: function(data, textStatus, jqXHR)
39              {
40                existingTags = data.tags;
41                //alert(existingTags);
42              },
43            error: function(jqXHR, textStatus, errorThrown )
44              {
45                alert(textStatus);
46                alert(errorThrown);
47              }
48          }
49        );
50        var curHBox = {
51                        type: 'hbox',
52                        children: []
53                      }
54        ele[ele.length] = curHBox;
55        for (var i=0; i<existingTags.length; i++) {
56          curHBox.children[curHBox.children.length] =
57            {
58              type: 'checkbox',
59              id: existingTags[i].id,
60              label: existingTags[i].name,
61              'default': existingTags[i].name == '' ? '' : (tagexists(existingTags[i].id) ? 'checked' : ''),
62              userdef: false
63            }
64          if ( ((i % 5)==4) && ((i+1)<existingTags.length-1)) {
65            curHBox = {
66                        type: 'hbox',
67                        children: []
68                      }
69            ele[ele.length] = curHBox;
70          }
71        }
72        ele[ele.length] =
73            {
74              type: 'checkbox',
75              id: '',
76              label: '',
77              'default': '',
78              userdef: false
79            }
80
81        var tmp = ele[ele.length-1];
82        tmp.userdef = true;
83        tmp.id = 'user_def_box';
84        tmp.label = lang.customTags
85        ele[ele.length-1] =
86          {
87            type: 'vbox',
88            children: [
89                tmp,
90                {
91                    type: 'text',
92                    id: 'user_def_text',
93                }
94            ]
95          }
96        return ele;
97      };
98    var d =
99      {
100        title:  lang.dlgTitle,
101        minWidth: 500,
102        minHeight: 200,
103        contents: [
104            {
105                id: 'tab-basic',
106                label: 'Settings',
107                elements: generateTags()
108            }
109        ],
110        onOk: function()
111          {
112            var dialog = this;
113            var codeB = "{{tag>";
114            var codeE = "}}";
115            var selected = "";
116            var dialog = this;
117            for (var i = 0; i<existingTags.length; i++) {
118              selected += dialog.getValueOf('tab-basic',existingTags[i].id) ? existingTags[i].id+' ' : '';
119            }
120            selected += dialog.getValueOf('tab-basic','user_def_box') ? dialog.getValueOf('tab-basic','user_def_text')+' ' : '';
121            selected = selected.replace(/\s+$/,"");
122            editor.setData(editor.getData().replace(/\{\{tag&gt;.*?\}\}/g,'')+(selected=='' ? '' : codeB+selected+codeE));
123          }
124      };
125    return d;
126  }
127);
128