1/* DOKUWIKI:include_once jstree.js */
2
3jQuery(function()
4{
5
6    var selectedPageId = null;
7    var selectedNode = null;
8    var selectedNodePath = null;
9    var selectedNodeValue = null;
10    var sectok = jQuery('#fileTree').data('sectok');
11    if(!sectok) return;
12
13    jqModalManager.init();
14
15    jQuery('#event_save').click(function() {
16      var newVal = jQuery('#event_value').val();
17      jQuery.post(
18        DOKU_BASE + 'lib/exe/ajax.php',
19        {
20          call: 'plugin_metaeditor',
21          q: 'setMetaValue',
22          r: selectedPageId,
23          sectok: sectok,
24          opts: {
25            key : selectedNode,
26            oldval : selectedNodeValue,
27            newval : newVal
28          }
29        },
30        function(data) {
31          jqModalManager.msg = data[1];
32          jqModalManager.showInfoDialog();
33          selectedNodeValue = newVal;
34        }
35      );
36
37    });
38
39    function refreshMetaTree() {
40      jQuery.post(
41          DOKU_BASE + 'lib/exe/ajax.php',
42          {
43            call: 'plugin_metaeditor',
44            q: 'getMeta',
45            r: selectedPageId,
46            sectok: sectok
47          },
48          function(data) {
49            jQuery('#event_path').html('');
50            jQuery('#event_value').val('...');
51            selectedNodePath = null;
52            selectedNodeValue = null;
53            selectedNode = null;
54            jQuery('#metaTree').jstree(true).settings.core.data = data;
55            jQuery('#metaTree').jstree(true).refresh();
56          }
57        );
58
59    }
60
61
62    function customMenu(node) {
63    // The default set of all items
64    var items = {
65        /*renameItem: { // The "rename" menu item
66            label: "Rename",
67            action: function () {
68              alert('rename called');
69            }
70        },*/
71        deleteItem: { // The "delete" menu item
72            label: "Delete",
73            action: function () {
74              jqModalManager.data =
75              {
76                call : 'plugin_metaeditor',
77                q: 'deleteMetaValue',
78                r: selectedPageId,
79                sectok: sectok,
80                opts: {
81                   key: selectedNodePath,
82                   oldval: selectedNodeValue
83                }
84              };
85              jqModalManager.msg = "Do you really want to delete the selected node?<br>&nbsp;<br>";
86              jqModalManager.msg += "PageId: " + selectedPageId + "<br>";
87              jqModalManager.msg += "NodePath: " + selectedNodePath + "<br>";
88              jqModalManager.msg += "Value: " + selectedNodeValue;
89              jqModalManager.completeCb = function(data) {
90                  jqModalManager.msg = data[1];
91                  jqModalManager.showInfoDialog();
92                  refreshMetaTree();
93              };
94              jqModalManager.showConfirmDialog();
95            }
96        },
97        createFolderItem: { // The "Create folder" menu item
98            label: "Create Folder",
99            action: function (data) {
100              var tree = jQuery("#metaTree").jstree(true);
101              var nodePath = tree.get_path(node);
102              if(node.li_attr['data-type'] == 'file')
103              {
104                var id = tree.get_parent(node);
105                var parentNode = tree.get_node(id);
106                nodePath = tree.get_path(parentNode);
107              }
108              jqModalManager.data =
109              {
110                call : 'plugin_metaeditor',
111                q: 'createMetaArray',
112                r: selectedPageId,
113                sectok: sectok,
114                opts: {
115                  key : nodePath,
116                  newval: null
117                }
118              };
119              jqModalManager.msg = "Create new folder<br>&nbsp;<br>";
120              jqModalManager.msg += "Parent: " + nodePath;
121              jqModalManager.completeCb = function(data) {
122                jqModalManager.msg = data[1];
123                jqModalManager.showInfoDialog();
124                refreshMetaTree();
125              };
126              jqModalManager.createValue = false;
127              jqModalManager.showCreateDialog();
128            }
129        },
130        createItem: { // The "Create" menu item
131            label: "Create Property",
132            action: function () {
133              var tree = jQuery("#metaTree").jstree(true);
134              var nodePath = tree.get_path(node);
135              if(node.li_attr['data-type'] == 'file')
136              {
137                var id = tree.get_parent(node);
138                var parentNode = tree.get_node(id);
139                nodePath = tree.get_path(parentNode);
140              }
141              jqModalManager.data =
142              {
143                call : 'plugin_metaeditor',
144                q: 'createMetaValue',
145                r: selectedPageId,
146                sectok: sectok,
147                opts: {
148                  key : nodePath,
149                  newkey: null,
150                  newval: null
151                }
152              };
153              jqModalManager.msg = "Create new item<br>&nbsp;<br>";
154              jqModalManager.msg += "Parent: " + nodePath;
155              jqModalManager.completeCb = function(data) {
156                jqModalManager.msg = data[1];
157                jqModalManager.showInfoDialog();
158                refreshMetaTree();
159              };
160              jqModalManager.createValue = true;
161              jqModalManager.showCreateDialog();
162            }
163        }
164      };
165
166    return items;
167    }
168
169    jQuery('#metaTree').on('changed.jstree', function (e, data) {
170      var i, j, r;
171      if(data.selected.length != 1)
172        return;
173      var node = data.instance.get_node(data.selected[0]);
174      r = data.instance.get_path(data.selected[0]);
175      selectedNodePath = r;
176      if(node.li_attr['data-type'] == 'folder')
177        return;
178      //if(!data.instance.is_leaf(node))
179      //  return;
180      selectedNode = r;
181      jQuery.post(
182        DOKU_BASE + 'lib/exe/ajax.php',
183        {
184          call: 'plugin_metaeditor',
185          q: 'getMetaValue',
186          r: selectedPageId,
187          sectok: sectok,
188          opts: {
189            key: r
190          }
191        },
192        function(data) {
193          jQuery('#event_path').html(selectedNode.join(':'));
194          jQuery('#event_value').val(data);
195          selectedNodeValue = data;
196        }
197      );
198
199    })
200    // create the instance
201    .jstree({
202      core : {
203        multiple: false,
204      },
205      plugins: ["wholerow", "contextmenu"],
206      contextmenu: { items: customMenu }
207    });
208
209
210    jQuery('#fileTree').on('changed.jstree', function (e, data) {
211      var i, j, r;
212      if(data.selected.length != 1)
213        return;
214      var node = data.instance.get_node(data.selected[0]);
215      if(!data.instance.is_leaf(node))
216        return;
217      r = node.text;
218      selectedPageId = r;
219      jQuery.post(
220        DOKU_BASE + 'lib/exe/ajax.php',
221        {
222          call: 'plugin_metaeditor',
223          q: 'getMeta',
224          sectok: sectok,
225          r: r
226        },
227        function(data) {
228          jQuery('#metaTree').jstree(true).settings.core.data = data;
229          jQuery('#metaTree').jstree(true).refresh();
230        }
231      );
232
233    })
234    // create the instance
235    .jstree({
236      core : {
237        multiple: false
238      },
239      plugins: ["wholerow"]
240
241    });
242
243
244});
245
246var jqModalManager = {
247    jqConfirmModal : null,
248    jqCreateModal : null,
249    jqInfoModal : null,
250    completeCb : null,
251    data : null,
252    msg : null,
253    createValue: false,
254
255
256    init : function()
257    {
258
259    },
260
261    showCreateDialog : function() {
262       jqModalManager.jqCreateModal = jQuery(document.createElement('div'))
263       .dialog({
264           autoOpen: false,
265           draggable: true,
266           // fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
267           drag: function(event, ui) {
268               var fixPix = jQuery(document).scrollTop();
269               iObj = ui.position;
270               iObj.top = iObj.top - fixPix;
271               jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
272           },
273           title: "Create",
274           resizable: true,
275           buttons: {
276               Create: function() {
277                 if(jqModalManager.createValue)
278                 {
279                   jqModalManager.data.opts.newkey = jQuery("#metaeditor__createInput").val();
280                   jqModalManager.data.opts.newval = jQuery("#metaeditor__createValue").val();
281                 }
282                 else
283                 {
284                   jqModalManager.data.opts.newval = jQuery("#metaeditor__createInput").val();
285                 }
286                 jQuery.post(
287                   DOKU_BASE + 'lib/exe/ajax.php',
288                   jqModalManager.data,
289                   function(data) {
290                     jqModalManager.completeCb(data);
291                   }
292                 );
293                 jqModalManager.hideCreateDialog();
294               },
295               Cancel: function() {
296                 jqModalManager.hideCreateDialog();
297               }
298           }
299       })
300       .html(
301            '<div>' + jqModalManager.msg + '</div>' +
302            '<div>Name <input type="text" id="metaeditor__createInput"></div>' +
303            (jqModalManager.createValue ? '<div>Value <input type="text" id="metaeditor__createValue"></div>' : '')
304            )
305       .parent()
306       .attr('id','metaeditor__create')
307       .show()
308       .appendTo('.dokuwiki:first');
309
310       jQuery('#metaeditor__createInput').focus();
311
312       jQuery('#metaeditor__create').position({
313            my: "center",
314            at: "center",
315            of: window
316        });
317
318        // attach event handlers
319        jQuery('#metaeditor__create .ui-dialog-titlebar-close').click(function(){
320          jqModalManager.hideCreateDialog();
321        });
322
323
324    },
325
326    showInfoDialog : function() {
327       jqModalManager.jqInfoModal = jQuery(document.createElement('div'))
328       .dialog({
329           autoOpen: false,
330           draggable: true,
331           // fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
332           drag: function(event, ui) {
333               var fixPix = jQuery(document).scrollTop();
334               iObj = ui.position;
335               iObj.top = iObj.top - fixPix;
336               jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
337           },
338           title: "Info",
339           resizable: true,
340           buttons: {
341               OK: function() {
342                 jqModalManager.hideInfoDialog();
343               }
344           }
345       })
346       .html(
347            '<div>' + jqModalManager.msg + '</div>'
348            )
349       .parent()
350       .attr('id','metaeditor__info')
351       .show()
352       .appendTo('.dokuwiki:first');
353
354       jQuery('#metaeditor__info').position({
355            my: "center",
356            at: "center",
357            of: window
358        });
359
360           // attach event handlers
361        jQuery('#metaeditor__info .ui-dialog-titlebar-close').click(function(){
362          jqModalManager.hideInfoDialog();
363        });
364    },
365
366    showConfirmDialog : function() {
367       jqModalManager.jqConfirmModal = jQuery(document.createElement('div'))
368       .dialog({
369           autoOpen: false,
370           draggable: true,
371           // fix for dragging: http://stackoverflow.com/questions/17247486/jquery-ui-dialog-dragging-issues
372           drag: function(event, ui) {
373               var fixPix = jQuery(document).scrollTop();
374               iObj = ui.position;
375               iObj.top = iObj.top - fixPix;
376               jQuery(this).closest(".ui-dialog").css("top", iObj.top + "px");
377           },
378           title: "Confirmation",
379           resizable: true,
380           buttons: {
381               Yes: function() {
382                 jQuery.post(
383                   DOKU_BASE + 'lib/exe/ajax.php',
384                   jqModalManager.data,
385                   function(data) {
386                     jqModalManager.completeCb(data);
387                   }
388                 );
389                 jqModalManager.hideConfirmDialog();
390               },
391               Cancel: function() {
392                 jqModalManager.hideConfirmDialog();
393               }
394           }
395       })
396       .html(
397            '<div>' + jqModalManager.msg + '</div>'
398            )
399       .parent()
400       .attr('id','metaeditor__confirm')
401       .show()
402       .appendTo('.dokuwiki:first');
403
404       jQuery('#metaeditor__confirm').position({
405            my: "center",
406            at: "center",
407            of: window
408        });
409
410           // attach event handlers
411        jQuery('#metaeditor__confirm .ui-dialog-titlebar-close').click(function(){
412          jqModalManager.hideConfirmDialog();
413        });
414    },
415
416    hideConfirmDialog : function() {
417        jqModalManager.jqConfirmModal.empty();
418        jqModalManager.jqConfirmModal.remove();
419        jqModalManager.jqConfirmModal = null;
420    },
421
422    hideCreateDialog : function() {
423        jqModalManager.jqCreateModal.empty();
424        jqModalManager.jqCreateModal.remove();
425        jqModalManager.jqCreateModal = null;
426    },
427
428    hideInfoDialog : function() {
429        jqModalManager.jqInfoModal.empty();
430        jqModalManager.jqInfoModal.remove();
431        jqModalManager.jqInfoModal = null;
432    }
433
434
435};
436
437