forked from fabacab/wp-seedbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-seedbank.js
103 lines (94 loc) · 3.64 KB
/
wp-seedbank.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
WP_SEEDBANK = {};
WP_SEEDBANK.UI = {};
// User Interface functions.
WP_SEEDBANK.UI.attachDatepicker = function () {
var x = jQuery('.datepicker');
if (x.length) {
x.each(function () {
jQuery(this).datepicker();
});
}
};
WP_SEEDBANK.UI.setupExchangePostVisibilityButton = function (state) {
var showHideButton = document.createElement('button');
showHideButton.setAttribute("class", "button" );
showHideButton.innerHTML = state + ' Deleted Seed Exchanges';
jQuery(showHideButton).click(function (e) {
e.preventDefault();
WP_SEEDBANK.UI.toggleDeletedExchangePostVisibility(state);
if (state == "Hide") {
WP_SEEDBANK.UI.setupExchangePostVisibilityButton('Show');
} else {
WP_SEEDBANK.UI.setupExchangePostVisibilityButton('Hide');
}
jQuery(this).remove();
});
var x = document.getElementById('post-query-submit');
if (x && x.parentNode) {
x.parentNode.appendChild(showHideButton);
}
};
WP_SEEDBANK.UI.toggleDeletedExchangePostVisibility = function (state) {
jQuery('.taxonomy-seedbank_exchange_status').each(function() {
if (state == "Hide") {
if(jQuery(this).text() != "Active") {
jQuery(this).parent().hide();
}
} else {
if(jQuery(this).text() == "Deleted") {
jQuery(this).parent().show();
}
}
});
}
WP_SEEDBANK.UI.toggleBatchExchangeDataSource = function () {
jQuery('#seedbank-batch-exchange-file-upload').hide();
jQuery('#seedbank-batch-exchange-data-source').change(function (e) {
var x = e.target.querySelectorAll('option');
for (var i = 0; i < x.length; i++) {
if (x[i].selected) {
switch (x[i].value) {
case 'another website':
jQuery('#seedbank-batch-exchange-file-upload').hide();
jQuery('#seedbank-batch-exchange-web-fetch').show();
break;
case 'my computer':
jQuery('#seedbank-batch-exchange-file-upload').show();
jQuery('#seedbank-batch-exchange-web-fetch').hide();
break;
}
}
}
});
}
WP_SEEDBANK.UI.prefillScientificName = function () {
var el = (jQuery('.taxonomy-seedbank_scientific_name #parent').length)
? jQuery('.taxonomy-seedbank_scientific_name #parent')
: jQuery('#newseedbank_scientific_name_parent');
el.change(function () {
var x = jQuery(this);
var n = x.children('option:selected').text().trim();
var t;
if (jQuery('#newseedbank_scientific_name').length) {
var t = jQuery('#newseedbank_scientific_name');
} else if (jQuery('#tag-name').length) {
var t = jQuery('#tag-name');
} else if (jQuery('#name').length) {
var t = jQuery('#name');
}
// If not "None" or does not begin with what's being selected
if (x.val().trim() !== '-1' && t.val().trim().indexOf(n) !== 0) {
t.val(n + ' ' + t.val());
}
});
};
WP_SEEDBANK.init = function () {
// TODO: Run these only on the appropriate page.
WP_SEEDBANK.UI.attachDatepicker();
WP_SEEDBANK.UI.toggleBatchExchangeDataSource();
// TODO: Filter these out of the result set from the PHP at some point, eh?
WP_SEEDBANK.UI.toggleDeletedExchangePostVisibility('Hide');
WP_SEEDBANK.UI.setupExchangePostVisibilityButton('Show');
WP_SEEDBANK.UI.prefillScientificName();
};
window.addEventListener('DOMContentLoaded', WP_SEEDBANK.init);