diff options
Diffstat (limited to 'public/javascripts/admin_interface.js')
| -rw-r--r-- | public/javascripts/admin_interface.js | 253 |
1 files changed, 193 insertions, 60 deletions
diff --git a/public/javascripts/admin_interface.js b/public/javascripts/admin_interface.js index 450438f..e3d4ff1 100644 --- a/public/javascripts/admin_interface.js +++ b/public/javascripts/admin_interface.js | |||
| @@ -12,7 +12,7 @@ function hide_all() { | |||
| 12 | 12 | ||
| 13 | $(document).ready(function () { | 13 | $(document).ready(function () { |
| 14 | admin_search.initialize(); | 14 | admin_search.initialize(); |
| 15 | 15 | ||
| 16 | tinymce.init({ | 16 | tinymce.init({ |
| 17 | selector: 'textarea.with_editor', | 17 | selector: 'textarea.with_editor', |
| 18 | license_key: 'gpl', | 18 | license_key: 'gpl', |
| @@ -29,40 +29,56 @@ $(document).ready(function () { | |||
| 29 | cccms.setup_autosave(); | 29 | cccms.setup_autosave(); |
| 30 | }); | 30 | }); |
| 31 | } | 31 | } |
| 32 | }); | 32 | }); |
| 33 | |||
| 34 | if ($("#dashboard_search_term").length != 0) { | ||
| 35 | dashboard_search.initialize(); | ||
| 36 | } | ||
| 33 | 37 | ||
| 34 | if ($("#menu_search_term").length != 0) { | 38 | if ($("#menu_search_term").length != 0) { |
| 35 | menu_items.initialize_search(); | 39 | menu_items.initialize_search(); |
| 36 | } | 40 | } |
| 37 | 41 | ||
| 38 | if ($("#menu_item_list").length != 0) { | 42 | if ($("#menu_item_list").length != 0) { |
| 39 | menu_item_sorter.initialize(); | 43 | menu_item_sorter.initialize(); |
| 40 | } | 44 | } |
| 41 | 45 | ||
| 42 | if ($("#metadata").length != 0) { | 46 | if ($("#metadata").length != 0) { |
| 43 | meta_data.initialize(); | 47 | meta_data.initialize(); |
| 44 | } | 48 | } |
| 45 | 49 | ||
| 46 | if ($("#parent_search_term").length != 0) { | 50 | if ($("#parent_search_term").length != 0) { |
| 47 | parent_search.initialize_search(); | 51 | parent_search.initialize_search(); |
| 48 | } | 52 | } |
| 49 | 53 | ||
| 50 | if ($("#move_to_search_term").length != 0) { | 54 | if ($("#move_to_search_term").length != 0) { |
| 51 | move_to_search.initialize_search(); | 55 | move_to_search.initialize_search(); |
| 52 | } | 56 | } |
| 53 | 57 | ||
| 58 | if ($("#event_node_search_term").length != 0) { | ||
| 59 | event_search.initialize_search(); | ||
| 60 | } | ||
| 61 | |||
| 62 | if ($("#related_asset_search_term").length != 0) { | ||
| 63 | related_assets.initialize(); | ||
| 64 | } | ||
| 65 | |||
| 66 | if ($("#rrule_builder").length != 0) { | ||
| 67 | rrule_builder.initialize(); | ||
| 68 | } | ||
| 69 | |||
| 54 | if ($('#recent_changes_toggle').length != 0) { | 70 | if ($('#recent_changes_toggle').length != 0) { |
| 55 | hide_all(); | 71 | hide_all(); |
| 56 | $('#recent_changes_toggle').attr("class", "selected"); | 72 | $('#recent_changes_toggle').attr("class", "selected"); |
| 57 | $('#recent_changes_table').show(); | 73 | $('#recent_changes_table').show(); |
| 58 | 74 | ||
| 59 | $('#recent_changes_toggle').bind("click", function(){ | 75 | $('#recent_changes_toggle').bind("click", function(){ |
| 60 | hide_all(); | 76 | hide_all(); |
| 61 | $('#recent_changes_toggle').attr("class", "selected"); | 77 | $('#recent_changes_toggle').attr("class", "selected"); |
| 62 | $('#recent_changes_table').show(); | 78 | $('#recent_changes_table').show(); |
| 63 | return false; | 79 | return false; |
| 64 | }); | 80 | }); |
| 65 | 81 | ||
| 66 | $('#my_work_toggle').bind("click", function(){ | 82 | $('#my_work_toggle').bind("click", function(){ |
| 67 | hide_all(); | 83 | hide_all(); |
| 68 | $('#my_work_toggle').attr("class", "selected"); | 84 | $('#my_work_toggle').attr("class", "selected"); |
| @@ -76,7 +92,7 @@ $(document).ready(function () { | |||
| 76 | $('#my_work_table').show(); | 92 | $('#my_work_table').show(); |
| 77 | return false; | 93 | return false; |
| 78 | }); | 94 | }); |
| 79 | 95 | ||
| 80 | $('#current_drafts_toggle').bind("click", function(){ | 96 | $('#current_drafts_toggle').bind("click", function(){ |
| 81 | hide_all(); | 97 | hide_all(); |
| 82 | $('#current_drafts_toggle').attr("class", "selected"); | 98 | $('#current_drafts_toggle').attr("class", "selected"); |
| @@ -98,91 +114,108 @@ $(document).ready(function () { | |||
| 98 | return false; | 114 | return false; |
| 99 | }); | 115 | }); |
| 100 | } | 116 | } |
| 101 | 117 | ||
| 102 | jQuery.ajaxSetup({ | 118 | jQuery.ajaxSetup({ |
| 103 | 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");} | 119 | 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");} |
| 104 | }); | 120 | }); |
| 105 | 121 | ||
| 106 | $(document).ajaxSend(function(event, request, settings) { | 122 | $(document).ajaxSend(function(event, request, settings) { |
| 107 | if (typeof(AUTH_TOKEN) == "undefined") return; | 123 | if (typeof(AUTH_TOKEN) == "undefined") return; |
| 108 | // settings.data is a serialized string like "foo=bar&baz=boink" (or null) | 124 | // settings.data is a serialized string like "foo=bar&baz=boink" (or null) |
| 109 | settings.data = settings.data || ""; | 125 | settings.data = settings.data || ""; |
| 110 | settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN); | 126 | settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN); |
| 111 | }); | 127 | }); |
| 112 | 128 | ||
| 113 | }); | 129 | }); |
| 114 | 130 | ||
| 115 | 131 | ||
| 116 | meta_data = { | 132 | meta_data = { |
| 117 | initialize : function() { | 133 | initialize : function() { |
| 118 | $("#metadata").hide(); | 134 | document.getElementById("metadata_details").addEventListener("toggle", function() { |
| 119 | 135 | if (this.open) image_interface.initialize(); | |
| 120 | $("#button").click(function () { | ||
| 121 | |||
| 122 | $("#metadata").slideToggle(1200); | ||
| 123 | image_interface.initialize(); | ||
| 124 | |||
| 125 | if ($("#button").attr("class") == "unselected") { | ||
| 126 | $("#button").attr("class", "selected"); | ||
| 127 | |||
| 128 | } | ||
| 129 | else { | ||
| 130 | $("#button").attr("class", "unselected"); | ||
| 131 | $("#image_browser").hide(); | ||
| 132 | $("#image_browser_toggle").attr("class", "unselected"); | ||
| 133 | } | ||
| 134 | |||
| 135 | return false; | ||
| 136 | }); | 136 | }); |
| 137 | } | 137 | } |
| 138 | }; | 138 | }; |
| 139 | 139 | ||
| 140 | cccms = { | 140 | cccms = { |
| 141 | setup_autosave : function() { | 141 | setup_autosave : function() { |
| 142 | 142 | ||
| 143 | var elements = { | 143 | var elements = { |
| 144 | title : $('#page_title'), | 144 | title : $('#page_title'), |
| 145 | abstract : $('#page_abstract'), | 145 | abstract : $('#page_abstract'), |
| 146 | body : $('#page_body_ifr').contents().find('#tinymce'), | 146 | body : $('#page_body_ifr').contents().find('#tinymce'), |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | var page = { | 149 | var page = { |
| 150 | cached_title : elements.title.val(), | 150 | cached_title : elements.title.val(), |
| 151 | cached_abstract : elements.abstract.val(), | 151 | cached_abstract : elements.abstract.val(), |
| 152 | cached_body : elements.body.html(), | 152 | cached_body : elements.body.html(), |
| 153 | 153 | ||
| 154 | title_has_changed : function() { | 154 | title_has_changed : function() { |
| 155 | return (elements.title.val() != this.cached_title) | 155 | return (elements.title.val() != this.cached_title) |
| 156 | }, | 156 | }, |
| 157 | 157 | ||
| 158 | abstract_has_changed : function() { | 158 | abstract_has_changed : function() { |
| 159 | return (elements.abstract.val() != this.cached_abstract) | 159 | return (elements.abstract.val() != this.cached_abstract) |
| 160 | }, | 160 | }, |
| 161 | 161 | ||
| 162 | body_has_changed : function() { | 162 | body_has_changed : function() { |
| 163 | return elements.body.html() != this.cached_body | 163 | return elements.body.html() != this.cached_body |
| 164 | } | 164 | } |
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | jQuery.fn.submitWithAjax = function(options) { | 167 | jQuery.fn.submitWithAjax = function(options) { |
| 168 | if (page.title_has_changed() || page.abstract_has_changed() || page.body_has_changed()) { | 168 | if (page.title_has_changed() || page.abstract_has_changed() || page.body_has_changed()) { |
| 169 | 169 | ||
| 170 | tinymce.triggerSave(); | ||
| 170 | page.cached_title = elements.title.val(); | 171 | page.cached_title = elements.title.val(); |
| 171 | page.cached_abstract = elements.abstract.val(); | 172 | page.cached_abstract = elements.abstract.val(); |
| 172 | page.cached_body = elements.body.html(); | 173 | page.cached_body = elements.body.html(); |
| 173 | 174 | ||
| 174 | $("#flash").append("<img src='/images/ajax-loader.gif' alt='' />"); | 175 | var data = this.serializeArray().filter(function(field) { |
| 175 | $.post(this.attr("action"), $(this).serialize(), null, "script"); | 176 | return field.name !== "_method"; |
| 176 | 177 | }); | |
| 178 | data.push({ name: "_method", value: "put" }); | ||
| 179 | |||
| 180 | $.ajax({ | ||
| 181 | type: "POST", | ||
| 182 | url: this.attr("data-autosave-url"), | ||
| 183 | data: data, | ||
| 184 | error: function(xhr) { | ||
| 185 | if (xhr.status === 423) { | ||
| 186 | clearInterval(cccms.autosave_timer); | ||
| 187 | cccms.report_lock_lost($("#page_editor > form").attr("data-show-url")); | ||
| 188 | } | ||
| 189 | // any other failure: quietly retried on the next tick | ||
| 190 | } | ||
| 191 | }); | ||
| 192 | |||
| 177 | } | 193 | } |
| 178 | }; | 194 | }; |
| 179 | 195 | ||
| 180 | setInterval('$("#page_editor > form").submitWithAjax()', 7000); | 196 | cccms.autosave_timer = setInterval(function() { |
| 197 | $("#page_editor > form").submitWithAjax(); | ||
| 198 | }, 7000); | ||
| 199 | }, | ||
| 200 | |||
| 201 | report_lock_lost : function(show_url) { | ||
| 202 | var $flash = $("#flash"); | ||
| 203 | if ($flash.length === 0) { | ||
| 204 | $flash = $("<div>", { id: "flash" }).insertAfter(".admin_content_spacer"); | ||
| 205 | } | ||
| 206 | |||
| 207 | var $banner = $("<span>", { "class": "warning" }).text( | ||
| 208 | "This page is now locked by someone else — your changes here can no longer be saved. Copy anything you need, then " | ||
| 209 | ); | ||
| 210 | $banner.append($("<a>", { href: show_url }).text("return to the published page")); | ||
| 211 | $banner.append("."); | ||
| 212 | |||
| 213 | $flash.html($banner); | ||
| 181 | } | 214 | } |
| 182 | } | 215 | } |
| 183 | 216 | ||
| 184 | menu_item_sorter = { | 217 | menu_item_sorter = { |
| 185 | 218 | ||
| 186 | initialize : function() { | 219 | initialize : function() { |
| 187 | $("#menu_item_list").sortable({ | 220 | $("#menu_item_list").sortable({ |
| 188 | axis: 'y', | 221 | axis: 'y', |
| @@ -205,24 +238,24 @@ menu_item_sorter = { | |||
| 205 | } | 238 | } |
| 206 | }); | 239 | }); |
| 207 | }, | 240 | }, |
| 208 | 241 | ||
| 209 | placeholder_helper : function(e,ui) { | 242 | placeholder_helper : function(e,ui) { |
| 210 | $(".ui-state-highlight").html("<td colspan='100%'></td>"); | 243 | $(".ui-state-highlight").html("<td colspan='100%'></td>"); |
| 211 | } | 244 | } |
| 212 | } | 245 | } |
| 213 | 246 | ||
| 214 | image_interface = { | 247 | image_interface = { |
| 215 | 248 | ||
| 216 | initialize : function() { | 249 | initialize : function() { |
| 217 | 250 | ||
| 218 | $("#image_browser").hide(); | 251 | $("#image_browser").hide(); |
| 219 | image_interface.initialize_sortable_image_box(); | 252 | image_interface.initialize_sortable_image_box(); |
| 220 | image_interface.connect_browser_and_box(); | 253 | image_interface.connect_browser_and_box(); |
| 221 | image_interface.set_droppable_behavior(); | 254 | image_interface.set_droppable_behavior(); |
| 222 | image_interface.bind_image_browser_toggle(); | 255 | image_interface.bind_image_browser_toggle(); |
| 223 | }, | 256 | }, |
| 224 | 257 | ||
| 225 | 258 | ||
| 226 | set_droppable_behavior : function() { | 259 | set_droppable_behavior : function() { |
| 227 | $("ul#image_box").droppable({ | 260 | $("ul#image_box").droppable({ |
| 228 | out : function(event, ui) { | 261 | out : function(event, ui) { |
| @@ -238,7 +271,7 @@ image_interface = { | |||
| 238 | } | 271 | } |
| 239 | }); | 272 | }); |
| 240 | }, | 273 | }, |
| 241 | 274 | ||
| 242 | connect_browser_and_box : function() { | 275 | connect_browser_and_box : function() { |
| 243 | $("#image_browser ul li").draggable({ | 276 | $("#image_browser ul li").draggable({ |
| 244 | connectToSortable : 'ul#image_box', | 277 | connectToSortable : 'ul#image_box', |
| @@ -246,9 +279,9 @@ image_interface = { | |||
| 246 | revert : 'invalid' | 279 | revert : 'invalid' |
| 247 | }); | 280 | }); |
| 248 | }, | 281 | }, |
| 249 | 282 | ||
| 250 | initialize_sortable_image_box : function() { | 283 | initialize_sortable_image_box : function() { |
| 251 | 284 | ||
| 252 | $("ul#image_box").sortable({ | 285 | $("ul#image_box").sortable({ |
| 253 | revert : true, | 286 | revert : true, |
| 254 | update : function(event, ui) { | 287 | update : function(event, ui) { |
| @@ -263,9 +296,9 @@ image_interface = { | |||
| 263 | } | 296 | } |
| 264 | }); | 297 | }); |
| 265 | } | 298 | } |
| 266 | }); | 299 | }); |
| 267 | }, | 300 | }, |
| 268 | 301 | ||
| 269 | bind_image_browser_toggle : function() { | 302 | bind_image_browser_toggle : function() { |
| 270 | $("#image_browser_toggle").bind("click", function(){ | 303 | $("#image_browser_toggle").bind("click", function(){ |
| 271 | if ($("#image_browser_toggle").attr("class") == "unselected") { | 304 | if ($("#image_browser_toggle").attr("class") == "unselected") { |
| @@ -276,10 +309,110 @@ image_interface = { | |||
| 276 | $("#image_browser_toggle").attr("class", "unselected"); | 309 | $("#image_browser_toggle").attr("class", "unselected"); |
| 277 | $("#image_browser").hide(); | 310 | $("#image_browser").hide(); |
| 278 | } | 311 | } |
| 279 | 312 | ||
| 280 | return false; | 313 | return false; |
| 281 | }); | 314 | }); |
| 282 | } | 315 | } |
| 283 | } | 316 | } |
| 284 | |||
| 285 | 317 | ||
| 318 | rrule_builder = { | ||
| 319 | initialize : function() { | ||
| 320 | rrule_builder.try_populate_from_rrule($("#event_rrule").val()); | ||
| 321 | |||
| 322 | $("input[name='rrule_freq']").bind("change", function() { | ||
| 323 | if ($(this).val() === "weekly") { rrule_builder.show_weekly_options(); } | ||
| 324 | else { rrule_builder.show_monthly_options(); } | ||
| 325 | rrule_builder.sync(); | ||
| 326 | }); | ||
| 327 | $("#rrule_monthly_ordinal").bind("change", function() { | ||
| 328 | $("#rrule_ordinal_fields").toggle($(this).is(":checked")); | ||
| 329 | rrule_builder.sync(); | ||
| 330 | }); | ||
| 331 | $("#rrule_exclude_month").bind("change", function() { | ||
| 332 | $("#rrule_excluded_month").toggle($(this).is(":checked")); | ||
| 333 | rrule_builder.sync(); | ||
| 334 | }); | ||
| 335 | $("#rrule_builder input, #rrule_builder select").bind("change", rrule_builder.sync); | ||
| 336 | }, | ||
| 337 | |||
| 338 | show_weekly_options : function() { | ||
| 339 | $("#rrule_weekly_options").show(); | ||
| 340 | $("#rrule_monthly_options").hide(); | ||
| 341 | }, | ||
| 342 | show_monthly_options : function() { | ||
| 343 | $("#rrule_weekly_options").hide(); | ||
| 344 | $("#rrule_monthly_options").show(); | ||
| 345 | }, | ||
| 346 | |||
| 347 | sync : function() { | ||
| 348 | var freq = $("input[name='rrule_freq']:checked").val(); | ||
| 349 | var parts = []; | ||
| 350 | |||
| 351 | if (freq === "weekly") { | ||
| 352 | parts.push("FREQ=WEEKLY"); | ||
| 353 | if ($("#rrule_biweekly").is(":checked")) parts.push("INTERVAL=2"); | ||
| 354 | var days = []; | ||
| 355 | $("input[id^='rrule_byday_']:checked").each(function() { | ||
| 356 | days.push($(this).attr("id").replace("rrule_byday_", "")); | ||
| 357 | }); | ||
| 358 | if (days.length > 0) parts.push("BYDAY=" + days.join(",")); | ||
| 359 | } else { | ||
| 360 | parts.push("FREQ=MONTHLY"); | ||
| 361 | if ($("#rrule_monthly_ordinal").is(":checked")) { | ||
| 362 | parts.push("BYDAY=" + $("#rrule_ordinal").val() + $("#rrule_ordinal_day").val()); | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | if ($("#rrule_exclude_month").is(":checked")) { | ||
| 367 | var excluded = parseInt($("#rrule_excluded_month").val(), 10); | ||
| 368 | var months = []; | ||
| 369 | for (var m = 1; m <= 12; m++) { if (m !== excluded) months.push(m); } | ||
| 370 | parts.push("BYMONTH=" + months.join(",")); | ||
| 371 | } | ||
| 372 | |||
| 373 | $("#event_rrule").val(parts.join(";")); | ||
| 374 | }, | ||
| 375 | |||
| 376 | try_populate_from_rrule : function(rrule) { | ||
| 377 | if (!rrule) return; | ||
| 378 | var parts = {}; | ||
| 379 | rrule.split(";").forEach(function(p) { | ||
| 380 | var kv = p.split("="); | ||
| 381 | parts[kv[0]] = kv[1]; | ||
| 382 | }); | ||
| 383 | if (parts.COUNT || parts.UNTIL) return; | ||
| 384 | |||
| 385 | if (parts.FREQ === "WEEKLY") { | ||
| 386 | $("input[name='rrule_freq'][value='weekly']").prop("checked", true); | ||
| 387 | rrule_builder.show_weekly_options(); | ||
| 388 | if (parts.INTERVAL === "2") $("#rrule_biweekly").prop("checked", true); | ||
| 389 | if (parts.BYDAY) { | ||
| 390 | parts.BYDAY.split(",").forEach(function(code) { | ||
| 391 | $("#rrule_byday_" + code).prop("checked", true); | ||
| 392 | }); | ||
| 393 | } | ||
| 394 | } else if (parts.FREQ === "MONTHLY") { | ||
| 395 | $("input[name='rrule_freq'][value='monthly']").prop("checked", true); | ||
| 396 | rrule_builder.show_monthly_options(); | ||
| 397 | var match = parts.BYDAY && parts.BYDAY.match(/^(-?\d+)([A-Z]{2})$/); | ||
| 398 | if (match) { | ||
| 399 | $("#rrule_monthly_ordinal").prop("checked", true); | ||
| 400 | $("#rrule_ordinal_fields").show(); | ||
| 401 | $("#rrule_ordinal").val(match[1]); | ||
| 402 | $("#rrule_ordinal_day").val(match[2]); | ||
| 403 | } | ||
| 404 | } else { | ||
| 405 | return; | ||
| 406 | } | ||
| 407 | |||
| 408 | if (parts.BYMONTH) { | ||
| 409 | var included = parts.BYMONTH.split(",").map(function(s) { return parseInt(s, 10); }); | ||
| 410 | var missing = []; | ||
| 411 | for (var m = 1; m <= 12; m++) { if (included.indexOf(m) === -1) missing.push(m); } | ||
| 412 | if (missing.length === 1) { | ||
| 413 | $("#rrule_exclude_month").prop("checked", true); | ||
| 414 | $("#rrule_excluded_month").val(missing[0]).show(); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } | ||
| 418 | }; | ||
