Completed
Push — master ( 0caf5d...6eeaac )
by Ismail
47s
created

$.each   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
/*
2
    GD - GeoDiver's JavaScript module
3
4
    Define a global GD (acronym for GeoDiver) object containing all
5
    GD associated methods:
6
*/
7
8
// define global GD object
9
var GD;
10
if (!GD) {
0 ignored issues
show
Bug introduced by
The variable GD seems to be never initialized.
Loading history...
11
    GD = {};
12
}
13
14
// GD module
15
(function() {
16
    GD.setUpValidatorDefaults = function() {
17
        $.validator.addMethod('geoDb', function(value) {
18
            return /^G[DS][SE]\d\d?\d?\d?\d?$/i.test(value);
19
        }, 'Please enter a valid GEO dataset accession number (in the format GDSxxxx).');
20
21
        $.validator.addMethod('checkIfGeoExists', function(value) {
22
            var doesNotExist = [1, 2, 3, 4, 7, 8, 9, 11, 13, 14, 22, 24, 25, 27, 28, 29, 32, 41, 42, 43, 44, 54, 55, 57, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 79, 81, 82, 83, 84, 87, 89, 90, 97, 98, 101, 102, 103, 107, 109, 114, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 136, 137, 138, 139, 140, 141, 142, 143, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 159, 163, 165, 166, 173, 175, 176, 178, 179, 180, 184, 185, 188, 189, 190, 191, 193, 194, 195, 196, 197, 202, 204, 206, 207, 208, 209, 210, 211, 212, 216, 217, 218, 219, 220, 221, 226, 228, 229, 230, 231, 235, 237, 257, 260, 263, 269, 271, 273, 275, 277, 284, 286, 327, 328, 330, 331, 333, 334, 335, 336, 338, 340, 341, 342, 343, 347, 348, 349, 350, 351, 357, 361, 363, 365, 370, 371, 372, 373, 375, 376, 377, 379, 380, 381, 382, 383, 391, 392, 393, 396, 397, 398, 403, 405, 406, 407, 408, 419, 442, 445, 447, 448, 450, 452, 454, 455, 456, 457, 458, 459, 460, 461, 462, 464, 465, 466, 467, 474, 475, 482, 485, 489, 490, 492, 496, 497, 500, 503, 506, 512, 523, 530, 537, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 558, 566, 569, 570, 571, 572, 573, 581, 585, 590, 593, 594, 595, 597, 601, 603, 613, 616, 619, 620, 626, 628, 629, 633, 634, 642, 643, 644, 645, 646, 650, 655, 669, 673, 674, 675, 676, 678, 688, 689, 691, 692, 693, 697, 706, 712, 713, 714, 715, 716, 727, 729, 730, 733, 736, 741, 745, 746, 747, 749, 753, 754, 757, 758, 760, 762, 763, 764, 774, 783, 784, 789, 792, 793, 795, 796, 797, 798, 800, 801, 802, 803, 804, 805, 815, 816, 818, 819, 821, 822, 824, 839, 842, 850, 861, 864, 865, 875, 876, 886, 888, 889, 895, 896, 897, 898, 903, 904, 914, 921, 923, 924, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 941, 950, 952, 957, 973, 974, 975, 978, 979, 980, 985, 986, 990, 1000, 1004, 1005, 1006, 1007, 1008, 1016, 1021, 1024, 1025, 1026, 1029, 1031, 1034, 1035, 1040, 1041, 1042, 1045, 1046, 1052, 1060, 1061, 1066, 1069, 1070, 1075, 1081, 1082, 1086, 1087, 1089, 1090, 1091, 1092, 1097, 1098, 1100, 1101, 1102, 1104, 1105, 1114, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 1151, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183, 1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1206, 1207, 1208, 1216, 1217, 1218, 1223, 1224, 1229, 1242, 1246, 1259, 1260, 1262, 1267, 1268, 1281, 1283, 1291, 1296, 1297, 1308, 1322, 1337, 1341, 1345, 1355, 1356, 1358, 1364, 1368, 1370, 1377, 1378, 1386, 1387, 1391, 1403, 1410, 1415, 1416, 1417, 1418, 1420, 1425, 1426, 1428, 1430, 1432, 1433, 1437, 1441, 1454, 1457, 1458, 1459, 1460, 1461, 1462, 1463, 1470, 1471, 1482, 1483, 1487, 1493, 1506, 1508, 1524, 1525, 1535, 1536, 1537, 1538, 1539, 1540, 1554, 1556, 1558, 1561, 1564, 1566, 1567, 1569, 1570, 1573, 1574, 1575, 1576, 1577, 1578, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1594, 1595, 1598, 1601, 1602, 1603, 1605, 1613, 1614, 1619, 1623, 1624, 1625, 1628, 1639, 1643, 1656, 1659, 1668, 1669, 1671, 1680, 1682, 1683, 1698, 1708, 1720, 1721, 1722, 1725, 1737, 1738, 1740, 1742, 1749, 1751, 1754, 1755, 1762, 1769, 1770, 1775, 1781, 1782, 1787, 1789, 1790, 1811, 1814, 1817, 1818, 1819, 1820, 1822, 1823, 1828, 1829, 1831, 1834, 1856, 1859, 1860, 1861, 1863, 1866, 1867, 1868, 1876, 1885, 1888, 1889, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1907, 1909, 1918, 1919, 1927, 1930, 1935, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1953, 1958, 1961, 1966, 1967, 1969, 1970, 1971, 1991, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2017, 2022, 2033, 2059, 2062, 2067, 2068, 2075, 2076, 2085, 2108, 2116, 2117, 2121, 2122, 2127, 2128, 2129, 2130, 2131, 2133, 2137, 2140, 2148, 2155, 2166, 2198, 2199, 2210, 2220, 2238, 2247, 2252, 2256, 2257, 2262, 2271, 2273, 2275, 2278, 2279, 2280, 2286, 2290, 2293, 2296, 2299, 2305, 2306, 2315, 2337, 2340, 2345, 2348, 2357, 2369, 2376, 2392, 2393, 2394, 2401, 2402, 2403, 2404, 2405, 2407, 2409, 2435, 2436, 2441, 2449, 2450, 2458, 2459, 2465, 2467, 2473, 2476, 2488, 2492, 2500, 2503, 2505, 2506, 2507, 2510, 2512, 2527, 2538, 2539, 2541, 2543, 2544, 2551, 2557, 2560, 2568, 2574, 2620, 2621, 2634, 2644, 2645, 2670, 2679, 2689, 2690, 2692, 2711, 2714, 2726, 2776, 2792, 2793, 2796, 2797, 2798, 2799, 2800, 2801, 2806, 2807, 2828, 2829, 2836, 2837, 2839, 2844, 2845, 2849, 2871, 2888, 2890, 2891, 2894, 2896, 2897, 2898, 2899, 2900, 2907, 2942, 2943, 2979, 2985, 2986, 2992, 2994, 2995, 2997, 3013, 3015, 3016, 3019, 3020, 3021, 3022, 3024, 3026, 3053, 3055, 3063, 3065, 3066, 3067, 3075, 3076, 3090, 3093, 3094, 3098, 3146, 3161, 3164, 3165, 3166, 3167, 3168, 3169, 3170, 3185, 3200, 3201, 3202, 3204, 3205, 3206, 3208, 3212, 3213, 3214, 3219, 3236, 3237, 3246, 3247, 3248, 3249, 3250, 3261, 3263, 3264, 3265, 3266, 3269, 3270, 3271, 3272, 3273, 3275, 3276, 3277, 3278, 3279, 3280, 3286, 3301, 3302, 3303, 3304, 3307, 3314, 3317, 3327, 3328, 3335, 3336, 3337, 3338, 3339, 3340, 3347, 3348, 3359, 3372, 3377, 3378, 3380, 3381, 3389, 3390, 3391, 3409, 3443, 3449, 3450, 3451, 3457, 3458, 3460, 3541, 3542, 3543, 3546, 3563, 3565, 3569, 3584, 3585, 3586, 3587, 3588, 3589, 3590, 3611, 3614, 3617, 3645, 3650, 3651, 3652, 3693, 3695, 3708, 3731, 3760, 3778, 3817, 3821, 3822, 3828, 3845, 3877, 3878, 3879, 3937, 3947, 3967, 3968, 3969, 3970, 3971, 3979, 3994, 4020, 4022, 4031, 4033, 4044, 4049, 4060, 4062, 4068, 4072, 4073, 4075, 4076, 4086, 4087, 4097, 4098, 4111, 4112, 4117, 4122, 4126, 4127, 4139, 4183, 4184, 4187, 4192, 4197, 4241, 4292, 4293, 4405, 4529, 4530, 4561, 4603, 4604, 4605, 4616, 4623, 4624, 4625, 4626, 4627, 4628, 4629, 4630, 4631, 4632, 4633, 4634, 4635, 4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4645, 4646, 4647, 4648, 4649, 4650, 4651, 4652, 4653, 4654, 4655, 4656, 4657, 4658, 4659, 4660, 4661, 4662, 4663, 4666, 4667, 4668, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679, 4680, 4681, 4682, 4683, 4684, 4685, 4686, 4687, 4688, 4689, 4690, 4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4714, 4715, 4716, 4717, 4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4729, 4730, 4731, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 4744, 4745, 4746, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4789, 4790, 4792, 4793, 4860, 4861, 4862, 4863, 4864, 4865, 4866, 4867, 4868, 4869, 4870, 4871, 4872, 4873, 4874, 4875, 4878, 4888, 4889, 4890, 4905, 4906, 4907, 4908, 4909, 4918, 4919, 4920, 4921, 4922, 4923, 4924, 4925, 4927, 4932, 4933, 4934, 4935, 4938, 4939, 4942, 4945, 4946, 4948, 4951, 4952, 4963, 4975, 4976, 4977, 4982, 4995, 4996, 4997, 4998, 4999, 5000, 5001, 5002, 5003, 5004, 5005, 5011, 5027, 5035, 5036, 5038, 5039, 5042, 5043, 5044, 5068, 5069, 5081, 135, 1330, 3666, 3727, 3796, 3890, 3897, 3922, 3946, 3957, 3986, 4807, 5021, 5037];
23
            if (value.length > 3) {
24
                if ($.inArray(parseInt(value.substring(3)), doesNotExist) !== -1 && /^GDS\d\d?\d?\d?$/i.test(value)) {
25
                    return false;
26
                } else { return true; }
27
            } else { return true; }
28
        }, 'This GEO dataset accession number does not exist.');
29
30
        $.validator.setDefaults({
31
            errorClass: 'invalid',
32
            validClass: "valid",
33
            errorPlacement: function(error, element) {
34
                $(element)
35
                    .closest("form")
36
                    .find("label[for='" + element.attr("id") + "']")
37
                    .attr('data-error', error.text());
38
            },
39
        });
40
    };
41
42
    ///// AJAXs
43
44
    GD.loadGeoDbValidation = function() {
45
        'use strict';
46
        $('#load_geo_db').validate({
47
            rules: {
48
                geo_db: {
49
                    geoDb: true,
50
                    checkIfGeoExists: true,
51
                    required: true
52
                },
53
            },
54
            submitHandler: function() {
55
                $('.card-action').remove();
56
                $('#results_section').empty();
57
                var geo_db = $('input[name=geo_db]').val().toUpperCase();
58
                $('#modal_header_text').text('Loading GEO Dataset: ' + geo_db);
59
                $('#modal_text').text('This should take a few seconds. Please leave this page open');
60
                $('#loading_modal').modal({ dismissible: false });
61
                $('#loading_modal').modal('open');
62
                $.ajax({
63
                    type: 'POST',
64
                    url: '/load_geo_db',
65
                    data: $('#load_geo_db').serialize(),
66
                    success: function(response) {
67
                        $(response).insertAfter("#load_geo_card");
68
                        $('#geo_db_summary').html(response);
69
                        $('#geo_db_summary').show();
70
                        $('.adv_param_collapsible').collapsible();
71
                        $("input:radio[name=factor]:first").attr('checked', true);
72
                        $('#' + $("input:radio[name=factor]:first").attr('id') + '_select').show();
73
                        $('#adv_params_btn').click(function() {
74
                            if (!$('#adv_params_params_tabs').hasClass('tabs_initialised')) {
75
                                setTimeout(
76
                                    function() {
77
                                        $('#adv_params_params_tabs').tabs();
78
                                        $('#adv_params_params_tabs').addClass('tabs_initialised');
79
                                    }, 1);
80
                            }
81
                        });
82
                        $('select[name="groupa[]"]').on('change', function() {
83
                            if ($.isEmptyObject($('select[name="groupa[]"]:enabled').val())) {
84
                                $('#groupa_params').attr('data-tooltip', 'This is Group A');
85
                                $('#groupa_params').tooltip({ delay: 50 });
86
                            } else {
87
                                $('#groupa_params').attr('data-tooltip', 'Group A: ' + $('select[name="groupa[]"]:enabled').val().join(", "));
88
                                $('#groupa_params').tooltip({ delay: 50 });
89
                            }
90
                        });
91
                        $('select[name="groupb[]"]').on('change', function() {
92
                            if ($.isEmptyObject($('select[name="groupb[]"]:enabled').val())) {
93
                                $('#groupb_params').attr('data-tooltip', 'This is Group B');
94
                                $('#groupb_params').tooltip({ delay: 50 });
95
                            } else {
96
                                $('#groupb_params').attr('data-tooltip', 'Group B: ' + $('select[name="groupb[]"]:enabled').val().join(", "));
97
                                $('#groupb_params').tooltip({ delay: 50 });
98
                            }
99
                        });
100
                        GD.ensureOnlyOneSelectOption();
101
                        GD.addAdvParamLogic();
102
                        GD.addFactorToggle();
103
                        $('select').material_select();
104
                        $('.tooltipped').tooltip();
105
                        GD.addGeoDbInfo($('input[name=geo_db]').val());
106
                        GD.analyseValidation();
107
                        $('#loading_modal').modal('close');
108
                        if ($('.shepherd-open').length) {
109
                            GD.shepherd.next();
110
                        }
111
                    },
112
                    error: function(e, status) {
113
                        GD.ajaxError(e, status);
114
                    }
115
                });
116
            }
117
        });
118
    };
119
120
    GD.exemplar_results = function() {
121
        $('#exemplar_result').click(function() {
122
            $('input[name=geo_db]').val('GDS724');
123
            $('#load_geo_db').submit();
124
        });
125
    };
126
127
    GD.analyseValidation = function() {
128
        'use strict';
129
        $('#analyse').validate({
130
            rules: {},
131
            submitHandler: function() {
132
                // Manually check if groupa / groupb is empty
133
                if ($.isEmptyObject($('select[name="groupa[]"]:enabled').val()) || $.isEmptyObject($('select[name="groupb[]"]:enabled').val())) {
134
                    $('.select_factors_validations').text('Please select the factors in the two groups to analyse.');
135
                    return false;
136
                }
137
                $('.select_factors_validations').text('');
138
                $('#results_section').empty();
139
                var geo_db = $('input[name=geo_db]').val().toUpperCase();
140
                $('input[name="dgea_cluster_by_genes"]').val($('input[name="dgea_cluster_by_genes"]').is(':checked'));
141
                $('input[name="dgea_cluster_by_samples"]').val($('input[name="dgea_cluster_by_samples"]').is(':checked'));
142
                $('input[name="gsea_cluster_by_genes"]').val($('input[name="gsea_cluster_by_genes"]').is(':checked'));
143
                $('input[name="gsea_cluster_by_samples"]').val($('input[name="gsea_cluster_by_samples"]').is(':checked'));
144
                $('#modal_header_text').text('Analysing GEO Dataset: ' + geo_db);
145
                $('#modal_text').text('This should take a few minutes. Please leave this page open');
146
                $('#loading_modal').modal({ dismissible: false });
147
                $('#loading_modal').modal('open');
148
                $.ajax({
149
                    type: 'POST',
150
                    url: '/analyse',
151
                    data: $('#analyse').serialize(),
152
                    success: function(response) {
153
                        $('#results_section').show();
154
                        $('#results_section').html(response).imagesLoaded().then(function() {
155
                            $('#pca_tabs').tabs(); // init material tabs
156
                            $('#results_tabs').tabs(); // init material tabs
157
                            GD.createPlots();
158
                            $('.materialboxed').materialbox(); // init materialbox
159
                            $('.select_factors input.select-dropdown').removeAttr('disabled');
160
                            GD.download_all_results();
161
                            GD.delete_result();
162
                            GD.share_result();
163
                            GD.show_GeoDB_modal();
164
                            GD.remove_share();
165
                            $('#loading_modal').modal('close');
166
                            $('html, body').animate({
167
                                scrollTop: $('#results_section').offset().top
168
                            });
169
                        });
170
                    },
171
                    error: function(e, status) {
172
                        GD.ajaxError(e, status);
173
                    }
174
                });
175
                return undefined;
176
            }
177
        });
178
    };
179
180
    GD.geneExpressionAjax = function(currentRow, geneId) {
181
        $('#modal_header_text').text('Loading Graphics for Gene: ' + geneId);
182
        $('#modal_text').text('This should take a few seconds. Please leave this page open');
183
        $('#loading_modal').modal({ dismissible: false });
184
        $('#loading_modal').modal('open');
185
        var resultId = currentRow.closest('.results_card').data('result');
186
        var geoDb = currentRow.closest('.results_card').data('geodb');
187
        var user = currentRow.closest('.results_card').data('user');
188
        $.ajax({
189
            type: 'POST',
190
            url: '/gene_expression_graph',
191
            data: { gene_id: geneId, result_id: resultId, geo_db: geoDb, user: user },
192
            success: function(response) {
193
                currentRow.addClass('parent');
194
                currentRow.after('<tr class="child" id="' + geneId + 'ChildRow"><td colspan="8"><div id="' + geneId + 'Plot"></div></td></tr>');
195
                GD.createExpressionPlot(response, geneId);
196
                $('#loading_modal').modal('close');
197
            },
198
            error: function(e, status) {
199
                GD.ajaxError(e, status);
200
            }
201
        });
202
    };
203
204
    GD.interactionNetworkAjax = function(currentRow, pathId) {
205
        $('#modal_header_text').text('Loading Graphics for GeneSet: ' + pathId);
206
        $('#modal_text').text('This should take a few seconds. Please leave this page open');
207
        $('#loading_modal').modal({ dismissible: false });
208
        $('#loading_modal').modal('open');
209
        var resultId = currentRow.closest('.results_card').data('result');
210
        var geoDb = currentRow.closest('.results_card').data('geodb');
211
        var user = currentRow.closest('.results_card').data('user');
212
        $.ajax({
213
            type: 'POST',
214
            url: '/interaction_image',
215
            data: { path_id: pathId, result_id: resultId, geo_db: geoDb, user: user },
216
            success: function(response) {
217
                currentRow.addClass('parent');
218
                currentRow.after(response);
219
                $('#loading_modal').modal('close');
220
            },
221
            error: function(e, status) {
222
                GD.ajaxError(e, status);
223
            }
224
        });
225
    };
226
227
    GD.ajaxError = function(e, status) {
228
        var errorMessage;
229
        if (e.status == 500 || e.status == 400) {
230
            errorMessage = e.responseText;
231
            $('#results_section').show();
232
            $('#results_section').html(errorMessage);
233
            $('#loading_modal').modal('close'); // remove progress notification
234
        } else {
235
            errorMessage = e.responseText;
236
            $('#results_section').show();
237
            $('#results_section').html('<div class="card red lighten-2" role="alert"><div class="card-content white-text"><h3>Oops! GeoDiver is Drowning!</h3><p style="font-size: 1.5rem"><strong>Apologies, there was an error with your request. Please try again.</strong></p><p>Error Message:' + errorMessage + ' The server responded with the status code: ' + String(e.status) + '. Please refresh the page and try again.</p><p>If the error persists, please contact the administrator.</p></div></div>');
238
            $('#loading_modal').modal('close'); // remove progress notification
239
        }
240
    };
241
242
    GD.loadPcRedrawValidation = function() {
243
        $('#pca3d_redraw').validate({
244
            rules: {},
245
            submitHandler: function(form, event) {
246
                // Manually check if groupa / groupb is empty
247
                if ($.isEmptyObject($('select[name="PC3doption1"]').val()) || $.isEmptyObject($('select[name="PC3doption2"]').val()) || $.isEmptyObject($('select[name="PC3doption3"]').val())) {
248
                    $('.select_pc3_validations').text('Please select the Pricipal Components to analyse.');
249
                    return false;
250
                }
251
                // Manually check if PC2dOption1 == PC2dOption2
252
                if (($('select[name="PC3doption1"]').val() === $('select[name="PC3doption2"]').val()) ||
253
                    ($('select[name="PC3doption3"]').val() === $('select[name="PC3doption2"]').val()) ||
254
                    ($('select[name="PC3doption1"]').val() === $('select[name="PC3doption3"]').val())) {
255
                    $('.select_pc3_validations').text('Please select different principal components to analyse.');
256
                    return false;
257
                }
258
                $('.select_pc3_validations').text('');
259
                event.preventDefault(); // because we're not submitting at all
260
                $('#pca3d_plot').empty();
261
                var x = $('select[name=PC3doption1]').val();
262
                var y = $('select[name=PC3doption2]').val();
263
                var z = $('select[name=PC3doption3]').val();
264
                var jsonFile = $('#overview').data("overview-json");
265
                $.getJSON(jsonFile, function(json) {
266
                    GD.create3dPCAScatterPlot(json.pcdata, x, y, z);
267
                });
268
                return undefined;
269
            }
270
        });
271
        $('#pca2d_redraw').validate({
272
            rules: {},
273
            submitHandler: function(form, event) {
274
                // Manually check if PC2dOption1 / PC2dOption2 is empty
275
                if ($.isEmptyObject($('select[name="PC2doption1"]').val()) || $.isEmptyObject($('select[name="PC2doption2"]').val())) {
276
                    $('.select_pc2_validations').text('Please select the principal components to analyse.');
277
                    return false;
278
                }
279
                // Manually check if PC2dOption1 == PC2dOption2
280
                if ($('select[name="PC2doption1"]').val() === $('select[name="PC2doption2"]').val()) {
281
                    $('.select_pc2_validations').text('Please select different principal components to analyse.');
282
                    return false;
283
                }
284
285
                $('.select_pc2_validations').text('');
286
                event.preventDefault(); // because we're not submitting at all
287
                $("#pca2d_plot").empty();
288
                var x = $('select[name=PC2doption1]').val();
289
                var y = $('select[name=PC2doption2]').val();
290
                var jsonFile = $('#overview').data("overview-json");
291
                $.getJSON(jsonFile, function(json) {
292
                    GD.create2dPCAScatterPlot(json.pcdata, x, y);
293
                });
294
                return undefined;
295
            }
296
        });
297
    };
298
299
300
    ///// PLOTS
301
302
    GD.createPlots = function() {
303
        var jsonFile = $('#overview').data("overview-json");
304
        $.getJSON(jsonFile, function(json) {
305
            GD.pcaPlot = GD.createPCAPLOT(json.pc.cumVar, json.pc.expVar, json.pc.pcnames);
306
            GD.pca2dScatterPlot = GD.create2dPCAScatterPlot(json.pcdata, 'PC1', 'PC2');
307
            GD.pca3dScatterPlot = GD.create3dPCAScatterPlot(json.pcdata, 'PC1', 'PC2', 'PC3');
308
309
            GD.initialiatizePcaScatterPlot(json.pc.pcnames);
310
            $('select').material_select();
311
        });
312
313
        $('#pca_tabs').on('click', '.tab a', function() {
314
            if ($(this).attr('href') === '#pca2d') {
315
                Plotly.Plots.resize(GD.pca2dScatterPlot);
316
            } else {
317
                Plotly.Plots.resize(GD.pca3dScatterPlot);
318
            }
319
        });
320
321
        var dgeaJsonFile = $('#DGEA').data("dgea-json");
322
        $.getJSON(dgeaJsonFile, function(json) {
323
            GD.volcanoPlot = GD.createVolcanoPlot(json.vol.logFC, json.vol.pVal, json.vol.genes);
324
            GD.initializeToptable(json.tops, 'dgea-top-table', 'dgea-top-table-wrapper');
325
        });
326
327
        var gseaJsonFile = $('#GSEA').data("gsea-json");
328
        $.getJSON(gseaJsonFile, function(json) {
329
            GD.initializeToptable(json.tops, 'gsea-top-table', 'gsea-top-table-wrapper');
330
        });
331
        window.addEventListener('resize', function() {
332
            /** global: Plotly */
333
            Plotly.Plots.resize(GD.pcaPlot);
334
            Plotly.Plots.resize(GD.volcanoPlot);
335
            Plotly.Plots.resize(GD.pca2dScatterPlot);
336
            Plotly.Plots.resize(GD.pca3dScatterPlot);
337
        });
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
338
        GD.addGraphTooltips();
339
    };
340
341
    GD.create2dPCAScatterPlot = function(pcdata, x, y) {
342
        var group1, group2, group1_name, group2_name, data, layout, parentWidth, PCAplotGd3, pcaPlot;
343
        group1_name = pcdata.group1[0];
344
        group2_name = pcdata.group2[0];
345
        group1 = { x: pcdata[x + '.' + group1_name], y: pcdata[y + '.' + group1_name], text: pcdata.Group1, type: 'scatter', mode: 'markers', name: group1_name, marker: { symbol: 'circle' } };
346
        group2 = { x: pcdata[x + '.' + group2_name], y: pcdata[y + '.' + group2_name], text: pcdata.Group2, type: 'scatter', mode: 'markers', name: group2_name, marker: { symbol: 'square' } };
347
        data = [group1, group2];
348
        layout = { xaxis: { title: x }, yaxis: { title: y }, hovermode: 'closest' };
349
350
        parentWidth = 100;
351
        PCAplotGd3 = Plotly.d3.select('#pca2d_plot')
352
            .style({
353
                width: parentWidth + '%',
354
                'margin-left': (100 - parentWidth) / 2 + '%'
355
            });
356
        pcaPlot = PCAplotGd3.node();
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
357
358
        Plotly.newPlot(pcaPlot, data, layout);
359
        return pcaPlot;
360
    };
361
362
    GD.create3dPCAScatterPlot = function(pcdata, x, y, z) {
363
        var group1, group2, group1_name, group2_name, data, layout, parentWidth, PCAplotGd3, pcaPlot;
364
        group1_name = pcdata.group1[0];
365
        group2_name = pcdata.group2[0];
366
        group1 = { x: pcdata[x + '.' + group1_name], y: pcdata[y + '.' + group1_name], z: pcdata[z + '.' + group1_name], text: pcdata.Group1, type: 'scatter3d', mode: 'markers', name: group1_name, marker: { symbol: 'circle' } };
367
        group2 = { x: pcdata[x + '.' + group2_name], y: pcdata[y + '.' + group2_name], z: pcdata[z + '.' + group2_name], text: pcdata.Group2, type: 'scatter3d', mode: 'markers', name: group2_name, marker: { symbol: 'square' } };
368
        data = [group1, group2];
369
        layout = { scene: { xaxis: { title: x }, yaxis: { title: y }, zaxis: { title: z } }, hovermode: 'closest' };
370
371
        parentWidth = 100;
372
        PCAplotGd3 = Plotly.d3.select('#pca3d_plot')
373
            .style({
374
                width: parentWidth + '%',
375
                'margin-left': (100 - parentWidth) / 2 + '%'
376
            });
377
        pcaPlot = PCAplotGd3.node();
378
        Plotly.newPlot(pcaPlot, data, layout);
379
        return pcaPlot;
380
    };
381
382
    GD.createPCAPLOT = function(cumVar, expVar, pcaNames) {
383
        var CumulativePCA = { x: pcaNames, y: cumVar, type: 'scatter', name: 'Cumulative PCA' };
384
        var PCA = { x: pcaNames, y: expVar, type: 'scatter', name: 'PCA' };
385
        var data = [CumulativePCA, PCA];
386
        var layout = { legend: { x: 0, y: 100, traceorder: 'normal' } };
387
388
        var parentWidth = 100;
389
        var PCAplotGd3 = Plotly.d3.select('#PCA_plot')
390
            .style({
391
                width: parentWidth + '%',
392
                'margin-left': (100 - parentWidth) / 2 + '%'
393
            });
394
        var pcaPlot = PCAplotGd3.node();
395
        Plotly.newPlot(pcaPlot, data, layout);
396
        return pcaPlot;
397
    };
398
399
    //
400
    GD.createVolcanoPlot = function(xValues, yValues, genes) {
401
        var trace1 = { x: xValues, y: yValues, text: genes, mode: 'markers', type: 'scatter', name: 'volcano_plot', marker: { size: 7.5 } };
402
        var data = [trace1];
403
        var layout = { xaxis: { title: 'Log 2 Fold Change' }, yaxis: { title: '-Log10(P Value)' }, hovermode: 'closest' };
404
        var parentWidth = 100;
405
        var volcanoPlotGd3 = Plotly.d3.select('#volcano_plot')
406
            .style({
407
                width: parentWidth + '%',
408
                'margin-left': (100 - parentWidth) / 2 + '%'
409
            });
410
        var volcanoPlot = volcanoPlotGd3.node();
411
        Plotly.newPlot(volcanoPlot, data, layout);
412
        return volcanoPlot;
413
    };
414
415
    GD.createExpressionPlot = function(response, geneId) {
416
        var trace1 = { x: response.group1.x, y: response.group1.y, type: 'bar', name: response.group1_name[0] };
417
        var trace2 = { x: response.group2.x, y: response.group2.y, type: 'bar', name: response.group2_name[0] };
418
        var data = [trace1, trace2];
419
        var layout = { barmode: 'group', xaxis: { title: 'Sample', tickangle: -40, position: -0.5 }, yaxis: { title: 'Expression' } };
420
        var parentWidth = 100;
421
        var expressionPlotGd3 = Plotly.d3.select('#' + geneId + 'Plot')
422
            .style({
423
                width: parentWidth + '%',
424
                'margin-left': (100 - parentWidth) / 2 + '%'
425
            });
426
        var expressionPlot = expressionPlotGd3.node();
427
        Plotly.newPlot(expressionPlot, data, layout);
428
        window.addEventListener('resize', function() {
429
            Plotly.Plots.resize(expressionPlot);
430
        });
431
    };
432
433
    ///// General GD functions
434
    GD.initializeToptable = function(dataset, tableId, tableWrapperId) {
435
        dataset = GD.addPlotIconToTopTable(dataset);
436
        $('#' + tableId).dataTable({
437
            "oLanguage": {
438
                "sStripClasses": "",
439
                "sSearch": "",
440
                "sSearchPlaceholder": "Enter Keywords Here",
441
                "sInfo": "_START_ -_END_ of _TOTAL_",
442
                "sLengthMenu": '<span>Rows per page:</span><select class="browser-default">' +
443
                    '<option value="10">10</option>' +
444
                    '<option value="20">20</option>' +
445
                    '<option value="30">30</option>' +
446
                    '<option value="40">40</option>' +
447
                    '<option value="50">50</option>' +
448
                    '<option value="-1">All</option>' +
449
                    '</select></div>'
450
            },
451
            data: dataset,
452
            order: [
453
                [5, "asc"]
454
            ],
455
            bAutoWidth: false
456
        });
457
458
        GD.makePlotIconClickable(tableWrapperId);
459
460
        $('#' + tableWrapperId).on('click', '.search-toggle', function() {
461
            if ($('.hiddensearch').css('display') == 'none') {
462
                $('.hiddensearch').slideDown();
463
            } else {
464
                $('.hiddensearch').slideUp();
465
            }
466
        });
467
468
        $('#' + tableWrapperId).on('click', '.download-top-table', function() {
469
            $('#modal_header_text').text('Creating Download Link');
470
            $('#modal_text').text('This should take a few seconds. Please leave this page open');
471
            $('#loading_modal').modal({ dismissible: false });
472
            $('#loading_modal').modal('open');
473
            $.fileDownload($(this).attr('href'), {
474
                successCallback: function() {
475
                    $('#loading_modal').modal('close');
476
                },
477
                failCallback: function() {
478
                    $('#loading_modal').modal('close');
479
                }
480
            });
481
            return false; //this is critical to stop the click event which will trigger a normal file download!
482
        });
483
    };
484
485
    GD.addPlotIconToTopTable = function(dataset) {
486
        $.each(dataset, function(key, row) {
487
            row.push('<i class="material-icons child-row-chart light-blue-text text-darken-3">insert_chart</i>');
488
        });
489
        return (dataset);
490
    };
491
492
    GD.makePlotIconClickable = function(tableWrapperId) {
493
        $('#' + tableWrapperId).on('click', '.child-row-chart', function() {
494
            var currentRow = $(this).closest('tr');
495
            var geneId = currentRow.children('td:first').text();
496
            if ($('#' + geneId + 'ChildRow').length) {
497
                $('#' + geneId + 'ChildRow').remove();
498
            } else {
499
                if (tableWrapperId === 'dgea-top-table-wrapper') {
500
                    GD.geneExpressionAjax(currentRow, geneId);
501
                } else if (tableWrapperId === 'gsea-top-table-wrapper') {
502
                    GD.interactionNetworkAjax(currentRow, geneId);
503
                }
504
            }
505
        });
506
    };
507
508
    GD.initialiatizePcaScatterPlot = function(pcnames) {
509
        $.each(pcnames, function(key, value) {
510
            $('#PC2doption1').append($("<option></option>")
511
                .attr("value", value).text(value));
512
            $('#PC2doption2').append($("<option></option>")
513
                .attr("value", value).text(value));
514
            $('#PC3doption1').append($("<option></option>")
515
                .attr("value", value).text(value));
516
            $('#PC3doption2').append($("<option></option>")
517
                .attr("value", value).text(value));
518
            $('#PC3doption3').append($("<option></option>")
519
                .attr("value", value).text(value));
520
        });
521
        GD.loadPcRedrawValidation();
522
    };
523
524
    //
525
    GD.addFactorToggle = function() {
526
        $("input:radio[name=factor]").click(function() {
527
            var target = '#' + $(this).attr('id') + '_select';
528
            if ('#' + $('.select_factors:visible').attr('id') !== target) {
529
                $('.select_factors').hide();
530
                $('.select_factors select').attr("disabled", "disabled");
531
                $(target).show();
532
                $(target + ' select').removeAttr("disabled");
533
            }
534
            $('#groupa_params').attr('data-tooltip', 'This is Group A');
535
            $('#groupa_params').tooltip({ delay: 50 });
536
            $('#groupb_params').attr('data-tooltip', 'This is Group B');
537
            $('#groupb_params').tooltip({ delay: 50 });
538
            GD.ensureOnlyOneSelectOption();
539
        });
540
    };
541
542
    GD.addAdvParamLogic = function() {
543
        GD.show_hide_div('#DGEA_input', '#DGEAparams');
544
        GD.show_hide_div('#GSEA_input', '#GSEAparams');
545
        GD.show_hide_div('#dgea_toptable', '#dgea_toptable_params');
546
        GD.show_hide_div('#dgea_heatmap', '#dgea_heatmap_params');
547
        GD.show_hide_div('#gsea_heatmap', '#gsea_heatmap_params');
548
        $("input:radio[name=gsea_type]").click(function() {
549
            if ($("input:radio[name=gsea_type]:checked").val() == 'ExpVsCtrl') {
550
                $('#gage_select_control').show();
551
            } else {
552
                $('#gage_select_control').hide();
553
            }
554
        });
555
    };
556
557
    GD.show_hide_div = function(checkbox, div) {
558
        $(checkbox).change(function() {
559
            if ($(this).prop("checked")) {
560
                $(div).show();
561
            } else {
562
                $(div).hide();
563
            }
564
        });
565
    };
566
567
    GD.addGeoDbInfo = function(geoAccession) {
568
        var accession = geoAccession.toUpperCase();
569
570
        var jsonFile = GD.protocol() + window.location.host + "/GeoDiver/DBs/" + accession + ".json";
571
        $.getJSON(jsonFile, function(json) {
572
            $('#dataset_accession').text(' ' + json.Accession);
573
            $('#dataset_title').text(json.Title);
574
            $('#dataset_summary').text(json.Description);
575
            $('#dataset_organism').text(json.Sample_Organism);
576
            $('#dataset_summary').text(json.Description);
577
            $('#dataset_citation').text(json.Reference);
578
        });
579
    };
580
581
    GD.addUserDropDown = function() {
582
        $('.dropdown-button').dropdown({
583
            inDuration: 300,
584
            outDuration: 225,
585
            hover: true,
586
            belowOrigin: true,
587
            alignment: 'right'
588
        });
589
    };
590
591
    GD.download_all_results = function() {
592
        $('#results_section').on('click', '#download-all-results', function(event) {
593
            event.preventDefault();
594
            $('#modal_header_text').text('Creating Download Link');
595
            $('#modal_text').text('This should take a few seconds. Please leave this page open');
596
            $('#loading_modal').modal({ dismissible: false });
597
            $('#loading_modal').modal('open');
598
            $.fileDownload($(this).data('download'), {
599
                successCallback: function() {
600
                    $('#loading_modal').modal('close');
601
                },
602
                failCallback: function() {
603
                    $('#loading_modal').modal('close');
604
                }
605
            });
606
            $('#loading_modal').modal('close');
607
            return false; //this is critical to stop the click event which will trigger a normal file download!
608
        });
609
    };
610
611
    GD.delete_result = function() {
612
        $('#results_section').on('click', '#delete_results', function() {
613
            $('#delete_modal').modal('open');
614
            var resultId = $(this).closest('.card').data('result');
615
            var geoDb = $(this).closest('.card').data('geodb');
616
            $('#delete_modal').attr('data-result', resultId);
617
            $('#delete_modal').attr('data-geodb', geoDb);
618
        });
619
620
        $('.delete-results').click(function() {
621
            $('#modal_header_text').text('Deleting Result');
622
            $('#modal_text').text('This should take a few seconds. Please leave this page open');
623
            $('#loading_modal').modal({ dismissible: false });
624
            $('#loading_modal').modal('open');
625
            var resultId = $('#delete_modal').data('result');
626
            var geoDb = $('#delete_modal').data('geodb');
627
            $.ajax({
628
                type: 'POST',
629
                url: '/delete_result',
630
                data: { result_id: resultId, geo_db: geoDb },
631
                success: function() {
632
                    location.reload();
633
                },
634
                error: function(e, status) {
635
                    GD.ajaxError(e, status);
636
                }
637
            });
638
        });
639
    };
640
641
    GD.show_GeoDB_modal = function() {
642
        $('#results_section').on('click', '.open_db_info_modal', function() {
643
            var accession = $(this).data('accession');
644
            GD.addGeoDbInfo(accession);
645
            $('#dbInfo').modal('open');
646
        });
647
    };
648
649
    GD.share_result = function() {
650
        $('#results_section').on('click', '#share_btn', function() {
651
            var share_link = $(this).closest('.card').data('share-link');
652
            $('#share_the_link_btn').show();
653
            $('#share_btn').hide();
654
            $('#share_link_input').val(share_link);
655
            $('#share_link_input').prop("readonly", true);
656
            $('#share_modal').modal('open');
657
            $('#share_modal').attr('data-share-link', share_link);
658
            $('#share_link_input').select();
659
            $.ajax({
660
                type: 'POST',
661
                url: share_link,
662
                error: function(e, status) {
663
                    GD.ajaxError(e, status);
664
                }
665
            });
666
        });
667
        $('#results_section').on('click', '#share_the_link_btn', function() {
668
            var share_link = $(this).closest('.card').data('share-link');
669
            $('#share_link_input1').val(share_link);
670
            $('#share_link_input1').prop("readonly", true);
671
            $('#share_the_link_modal').modal('open');
672
            $('#share_the_link_modal').attr('data-share-link', share_link);
673
            $('#share_link_input1').select();
674
        });
675
676
        $(".share_link_input").focus(function() {
677
            $(this).select();
678
            // Work around Chrome's little problem
679
            $(this).mouseup(function() {
680
                // Prevent further mouseup intervention
681
                $(this).unbind("mouseup");
682
                return false;
683
            });
684
        });
685
    };
686
687
    GD.remove_share = function() {
688
        $('.remove_link').click(function() {
689
            var share_link = $(this).closest('.modal').data('share-link');
690
            var remove_link = share_link.replace(/\/sh\//, '/rm/');
691
            $('#share_the_link_btn').hide();
692
            $('#share_btn').show();
693
            $('#share_modal').modal('close');
694
            $('#share_the_link_modal').modal('close');
695
            $.ajax({
696
                type: 'POST',
697
                url: remove_link,
698
                error: function(e, status) {
699
                    GD.ajaxError(e, status);
700
                }
701
            });
702
        });
703
    };
704
705
    GD.ensureOnlyOneSelectOption = function() {
706
        $('.select_factors:visible select[name="groupa[]"], .select_factors:visible select[name="groupb[]"]').on('change', function() {
707
            // RESET both arrays
708
            $groupAChildren = $('.select_factors:visible select[name="groupa[]"]').siblings('ul').children('li').not(':first-child');
0 ignored issues
show
Bug introduced by
The variable $groupAChildren seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$groupAChildren.
Loading history...
709
            $groupAChildren.prop("disabled", false);
710
            $groupAChildren.removeClass('disabled');
711
            $groupAChildren.find('input:checkbox').prop('disabled', false);
712
            $('.select_factors:visible select[name="groupa[]"]').children().not(':first-child').prop('disabled', false);
713
714
            $groupBChildren = $('.select_factors:visible select[name="groupb[]"]').siblings("ul").children("li").not(":first-child");
0 ignored issues
show
Bug introduced by
The variable $groupBChildren seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$groupBChildren.
Loading history...
715
            $groupBChildren.prop("disabled", false);
716
            $groupBChildren.removeClass('disabled');
717
            $groupBChildren.find('input:checkbox').prop('disabled', false);
718
            $('.select_factors:visible select[name="groupb[]"]').children().not(':first-child').prop('disabled', false);
719
720
            var aValues = $('.select_factors:visible select[name="groupa[]"]').val();
721
            var bValues = $('.select_factors:visible select[name="groupb[]"]').val();
722
            var selector, valueIndex;
723
724
            $.each(aValues, function(i) {
725
                selector = '.select_factors:visible select[name="groupb[]"] option[value="' + aValues[i] + '"]';
726
                $(selector).prop('disabled', true);
727
                valueIndex = $(selector).index() + 1;
728
                $('.select_factors:visible select[name="groupb[]"]').siblings('ul').children('li:nth-child(' + valueIndex + ')').addClass('disabled');
729
                $('.select_factors:visible select[name="groupb[]"]').siblings('ul').children('li:nth-child(' + valueIndex + ')').find('input:checkbox').prop('disabled', true);
730
            });
731
            $.each(bValues, function(i) {
732
                selector = '.select_factors:visible select[name="groupa[]"] option[value="' + bValues[i] + '"]';
733
                $(selector).prop('disabled', true);
734
                valueIndex = $(selector).index() + 1;
735
                $('.select_factors:visible select[name="groupa[]"]').siblings('ul').children('li:nth-child(' + valueIndex + ')').addClass('disabled');
736
                $('.select_factors:visible select[name="groupa[]"]').siblings('ul').children('li:nth-child(' + valueIndex + ')').find('input:checkbox').prop('disabled', true);
737
            });
738
        });
739
    };
740
741
    GD.addGraphTooltips = function() {
742
        $('#dgea-top-table .child-row-chart').tooltip({ tooltip: 'Show a graph for the inidividual expression levels for this Gene', position: 'left' });
743
        $('#gsea-top-table .child-row-chart').tooltip({ tooltip: 'Show a colour coded interaction network for this pathway', position: 'left' });
744
    };
745
746
    GD.setupGoogleAuthentication = function() {
747
        gapi.auth.authorize({
748
            immediate: true,
749
            response_type: 'code',
750
            cookie_policy: 'single_host_origin',
751
            client_id: GD.CLIENT_ID,
752
            scope: 'email'
753
        });
754
        $('.login_button').on('click', function(e) {
755
            e.preventDefault();
756
            /** global: gapi */
757
            gapi.auth.authorize({
758
                immediate: false,
759
                response_type: 'code',
760
                cookie_policy: 'single_host_origin',
761
                client_id: GD.CLIENT_ID,
762
                scope: 'email'
763
            }, function(response) {
764
                if (response && !response.error) {
765
                    // google authentication succeed, now post data to server.
766
                    jQuery.ajax({
767
                        type: 'POST',
768
                        url: "/auth/google_oauth2/callback",
769
                        data: response,
770
                        success: function() {
771
                            // TODO - just update the DOM instead of a redirect
772
                            $(location).attr("href", GD.protocol() + window.location.host + "/analyse");
773
                        }
774
                    });
775
                } else {
776
                    console.log('ERROR Response google authentication failed');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
777
                    // TODO: ERROR Response google authentication failed
778
                }
779
            });
780
        });
781
    };
782
783
    GD.protocol = function() {
784
        if (GD.USING_SLL === "true") {
785
            return "https://";
786
        } else {
787
            return "http://";
788
        }
789
    };
790
791
    GD.guidedTour = function() {
792
        GD.shepherd = new Shepherd.Tour({
0 ignored issues
show
Bug introduced by
The variable Shepherd seems to be never declared. If this is a global, consider adding a /** global: Shepherd */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
793
            defaults: {
794
                classes: "shepherd-element shepherd-open shepherd-theme-arrows",
795
                showCancelLink: true
796
            }
797
        });
798
799
        GD.shepherd.addStep("welcome", {
800
            title: 'Welcome!',
801
            text: [
802
                'Welecome to our GeoDiver Tour!<br>',
803
                'We will go through the analysis of an exemplar GEO dataset.<br>',
804
                'If you have any issues feel free to contact us via the contact form on the <a href="https://geodiver.co.uk" target="_blank">home page</a> '
805
            ],
806
            attachTo: "#load_geo_card bottom",
807
            classes: "shepherd-transparent-text shepard-welcome",
808
            buttons: [{
809
                    text: "Exit",
810
                    classes: "btn-flat btn-flat-background waves-effect waves-light light-grey left",
811
                    action: GD.shepherd.cancel
812
                },
813
                {
814
                    text: "Next",
815
                    action: GD.shepherd.next,
816
                    classes: "btn waves-effect waves-light light-blue accent-4"
817
                }
818
            ]
819
        });
820
        GD.shepherd.addStep("enter_accession", {
821
            title: 'Step 1: Enter GEO Accession Number',
822
            text: [
823
                'Here, you need to enter the accession number of the GEO dataset that you wish to analyse.',
824
                '<strong>For the exemplar datasets, please enter: "GDS724" or "GSE_____"</strong>',
825
                'Alternatively, select the buttons below to analyse the corresponing exemplar dataset',
826
                'You can find GEO datasets to analyse on <a href="https://www.ncbi.nlm.nih.gov/geo/" target="_blank"> NCBI\'s GEO page</a>. However, note that GeoDiver only works with GEO datasets containing expression data.'
827
            ],
828
            classes: "shepard-enter_accession",
829
            attachTo: "#search_geo_dataset bottom",
830
            buttons: [{
831
                    text: "Back",
832
                    classes: "btn-flat btn-flat-background waves-effect waves-light light-grey left",
833
                    action: GD.shepherd.back
834
                },
835
                {
836
                    text: "GDS724",
837
                    classes: 'btn waves-effect waves-light light-blue accent-4',
838
                    action: function() {
839
                        $("#search_geo_db").val('GDS724');
840
                        GD.shepherd.next();
841
                    }
842
                },
843
                {
844
                    text: "GSE____",
845
                    classes: 'btn waves-effect waves-light light-blue accent-4',
846
                    action: function() {
847
                        $("#search_geo_db").val("GSE___");
848
                        GD.shepherd.next();
849
                    }
850
                },
851
                {
852
                    text: "Next",
853
                    classes: 'btn waves-effect waves-light light-blue accent-4',
854
                    action: function() {
855
                        GD.shepherd.next();
856
                    }
857
                }
858
            ]
859
        });
860
        GD.shepherd.addStep("load_datasets", {
861
            title: "Step 2: Load the GEO Dataset",
862
            text: 'Next we need to load the GEO dataset. For this, simply click on the "Load GEO Dataset button.',
863
            attachTo: "#geo_db_title show",
864
            classes: "shepard-load_datasets",
865
            buttons: [{
866
                    text: "Back",
867
                    classes: "btn-flat btn-flat-background waves-effect waves-light light-grey left",
868
                    action: GD.shepherd.back
869
                },
870
                {
871
                    text: "Next",
872
                    classes: 'btn waves-effect waves-light light-blue accent-4',
873
                    action: GD.shepherd.next
874
                }
875
            ]
876
        });
877
        GD.shepherd.addStep("geodb_dataset", {
878
            title: "Step 3: The GEO Dataset",
879
            text: [
880
                'The GEO dataset has now been loaded.',
881
                'You can find out more about the GEO dataset by clicking on the blue info button at the end of GEO Dataset title.'
882
            ],
883
            attachTo: "#geo_db_title top",
884
            classes: "shepard-geodb_dataset",
885
            buttons: [{
886
                    text: "Back",
887
                    classes: "btn-flat btn-flat-background waves-effect waves-light light-grey left",
888
                    action: GD.shepherd.back
889
                },
890
                {
891
                    text: "Next",
892
                    classes: "btn waves-effect waves-light light-blue accent-4",
893
                    action: GD.shepherd.next
894
                }
895
            ]
896
        });
897
        GD.shepherd.addStep("example", {
898
            title: "Step 4: Select Factor of Interest",
899
            text: [
900
                "Next select the factor that you are most interested in analysing.",
901
            ],
902
            attachTo: "#geo_db_title top",
903
            classes: "shepard-geodb_dataset",
904
            buttons: [{
905
                    text: "Back",
906
                    classes: "btn-flat btn-flat-background waves-effect waves-light light-grey left",
907
                    action: GD.shepherd.back
908
                },
909
                {
910
                    text: "More Info",
911
                    classes: "btn waves-effect waves-light light-blue accent-4",
912
                    action: GD.shepherd.next
913
                },
914
                {
915
                    text: "Next",
916
                    classes: "btn waves-effect waves-light light-blue accent-4",
917
                    action: GD.shepherd.next
918
                }
919
            ]
920
        });
921
        return GD.shepherd.start();
922
    };
923
924
    GD.initTour = function() {
925
        if (window.location.href.indexOf("analyse#guided_tour") !== -1) {
926
            GD.guidedTour();
927
        }
928
    };
929
}());
930
931
(function($) {
932
    $.each(["show", "hide"], function(i, ev) {
933
        var el = $.fn[ev];
934
        $.fn[ev] = function() {
935
            this.trigger(ev);
936
            return el.apply(this, arguments);
937
        };
938
    });
939
940
    $.fn.exists = function() { return this.length > 0; };
941
942
    // Fn to allow an event to fire after all images are loaded
943
    $.fn.imagesLoaded = function() {
944
        var $imgs = this.find('img[src!=""]');
945
        // if there's no images, just return an already resolved promise
946
        if (!$imgs.length) { return $.Deferred().resolve().promise(); }
947
948
        // for each image, add a deferred object to the array which resolves when the image is loaded (or if loading fails)
949
        var dfds = [];
950
        $imgs.each(function() {
951
            var dfd = $.Deferred();
952
            dfds.push(dfd);
953
            /** global: Image */
954
            var img = new Image();
955
            img.onload = function() { dfd.resolve(); };
956
            img.onerror = function() { dfd.resolve(); };
957
            img.src = this.src;
958
        });
959
        // return a master promise object which will resolve when all the deferred objects have resolved
960
        // i.e. - when all the images are loaded
961
        return $.when.apply($, dfds);
962
    };
963
964
    $(function() {
965
        GD.initTour();
966
        $('.button-collapse').sideNav();
967
        $('select').material_select();
968
        $('#login_modal').modal();
969
        $('.modal').modal();
970
        if ($('#load_geo_db').length) {
971
            GD.setUpValidatorDefaults();
972
            GD.loadGeoDbValidation();
973
        }
974
        if ($('#exemplar_result').length) { GD.exemplar_results(); }
975
        GD.addUserDropDown();
976
    });
977
978
    $(function() {
979
        return $.ajax({
980
            url: 'https://apis.google.com/js/client:plus.js?onload=gpAsyncInit',
981
            dataType: 'script',
982
            cache: true
983
        });
984
    });
985
986
    window.gpAsyncInit = function() {
987
        GD.setupGoogleAuthentication();
988
    };
989
})(jQuery);