xref: /plugin/farmer/script/plugins.js (revision 954c40ad15965c60e6a9a47948b3cd54d507ff25)
1(function(exports) {
2    'use strict';
3
4    jQuery(document).ready(function () {
5        // Plugin Management for all Animals
6        var bulkPluginSelector = jQuery('#farmer__bulkPluginSelect');
7        if (jQuery().chosen) {
8            bulkPluginSelector.chosen({
9                width:           '100%',
10                search_contains: true,
11                "placeholder_text_single": LANG.plugins.farmer.pluginSelect
12            });
13        }
14        bulkPluginSelector.change(function () {
15            jQuery(".bulkButton").prop('disabled',false);
16        });
17
18        // Plugin Management of a single Animal
19        var animalSelector = jQuery('#farmer__animalSelect');
20        if (jQuery().chosen) {
21            animalSelector.chosen({
22                width:           '100%',
23                search_contains: true,
24                "placeholder_text_single": LANG.plugins.farmer.animalSelect
25            });
26        }
27        animalSelector.change(function () {
28            var animal = animalSelector.val();
29            jQuery.post(
30                DOKU_BASE + 'lib/exe/ajax.php',
31                {
32                    call: 'plugin_farmer_getPlugins_' + animal
33                },
34                function(data) {
35                    var submitButton = jQuery('<button type="submit" value="updateSingleAnimal" name="plugin_farmer[submit_type]">'+LANG.plugins.farmer.submit+'</button>');
36                    var resetButton = jQuery('<button type="reset">'+LANG.plugins.farmer.reset+'</button>');
37                    var pluginContainer = jQuery('#farmer__animalPlugins');
38                    pluginContainer.html('');
39                    pluginContainer.append(submitButton, resetButton);
40                    jQuery.each(data[0], function(index, value) {
41                        var checked = 'checked';
42                        if (typeof data[1][value] !== 'undefined' && data[1][value] === 0) {
43                            checked = '';
44                        }
45                        var pluginLabel = jQuery('<label></label>')
46                            .append(jQuery('<span></span>').text(value))
47                            .append(jQuery('<input class="edit" type="checkbox"  name="plugin_farmer_plugins[' + value + ']" ' + checked + '>'))
48                        ;
49                        pluginContainer.append(pluginLabel);
50                    });
51                    pluginContainer.append(submitButton.clone(), resetButton.clone());
52
53                    // data is array you returned with action.php
54                },
55                'json'
56            );
57        });
58
59        // make sure there's enough space for the dropdown
60        jQuery('select').on('chosen:showing_dropdown', function(evt, params) {
61            jQuery(evt.target).parent('fieldset').animate({
62                "padding-bottom": '20em'
63            }, 400);
64        }).on('chosen:hiding_dropdown', function(evt, params) {
65            jQuery(evt.target).parent('fieldset').animate({
66                "padding-bottom": '7px'
67            }, 400);
68        });
69
70
71        jQuery("input[name=bulkSingleSwitch]:radio").change(function () {
72            if (jQuery('#farmer__bulk').prop("checked")) {
73                jQuery('#farmer__bulkForm').css('display','initial');
74            } else {
75                jQuery('#farmer__bulkForm').css('display','none');
76            }
77            if (jQuery('#farmer__single').prop("checked")) {
78                jQuery('#farmer__singlePluginForm').css('display','initial');
79            } else {
80                jQuery('#farmer__singlePluginForm').css('display','none');
81            }
82        });
83        jQuery('#farmer__bulk').click();
84
85
86    });
87
88})(this.farmer__plugin = {});
89