From 3ac701266452d3509776fe58f9e1b2b8e9f33c1e Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Tue, 24 Mar 2015 13:45:44 +0800 Subject: Bug 1096798: prototype modal show_bug view --- extensions/BugModal/web/bug_modal.js | 730 +++++++++++++++++++++++++++++++++++ 1 file changed, 730 insertions(+) create mode 100644 extensions/BugModal/web/bug_modal.js (limited to 'extensions/BugModal/web/bug_modal.js') diff --git a/extensions/BugModal/web/bug_modal.js b/extensions/BugModal/web/bug_modal.js new file mode 100644 index 000000000..c3fd84e97 --- /dev/null +++ b/extensions/BugModal/web/bug_modal.js @@ -0,0 +1,730 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. */ + +$(function() { + 'use strict'; + + // all keywords for autocompletion (lazy-loaded on edit) + var keywords = []; + + // products with descriptions (also lazy-loaded) + var products = []; + + // scroll to an element + function scroll_to(el, complete) { + var offset = el.offset(); + $('html, body') + .animate({ + scrollTop: offset.top - 20, + scrollLeft: offset.left = 20 + }, + 200, + complete + ); + } + + // expand all modules + $('#expand-all-btn') + .click(function(event) { + event.preventDefault(); + var btn = $(event.target); + if (btn.data('expanded-modules')) { + btn.data('expanded-modules').slideToggle(200, 'swing', function() { + btn.data('expanded-spinners').html('▸'); + }); + btn.data('expanded-modules', false); + btn.text('Expand All'); + } + else { + var modules = $('.module-content:hidden'); + var spinners = $([]); + modules.each(function() { + spinners.push($(this).parent('.module').find('.module-spinner')[0]); + }); + btn.data('expanded-modules', modules); + btn.data('expanded-spinners', spinners); + modules.slideToggle(200, 'swing', function() { + spinners.html('▾'); + }); + btn.text('Collapse'); + } + }); + + // expand/colapse module + $('.module-header') + .click(function(event) { + event.preventDefault(); + var target = $(event.target); + var latch = target.hasClass('module-header') ? target.children('.module-latch') : target.parent('.module-latch'); + var spinner = $(latch.children('.module-spinner')[0]); + var module = $(latch.parents('.module')[0]); + var content = $(module.children('.module-content')[0]); + content.slideToggle(200, 'swing', function() { + spinner.html(content.is(':visible') ? '▾' : '▸'); + }); + }); + + // toggle obsolete attachments + $('#attachments-obsolete-btn') + .click(function(event) { + event.preventDefault(); + $(event.target).text(($('#attachments tr:hidden').length ? 'Hide' : 'Show') + ' Obsolete Attachments'); + $('#attachments tr.attach-obsolete').toggle(); + }); + + // comment collapse/expand + $('.comment-spinner') + .click(function(event) { + event.preventDefault(); + var spinner = $(event.target); + var id = spinner.attr('id').match(/\d+$/)[0]; + // switch to full header for initially collapsed comments + if (spinner.attr('id').match(/^ccs-/)) { + $('#cc-' + id).hide(); + $('#ch-' + id).show(); + } + $('#ct-' + id).slideToggle('fast', function() { + $('#c' + id).find('.activity').toggle(); + spinner.text($('#ct-' + id + ':visible').length ? '-' : '+'); + }); + }); + + // url --> unsafe warning + $('.unsafe-url') + .click(function(event) { + event.preventDefault(); + if (confirm('This is considered an unsafe URL and could possibly be harmful. ' + + 'The full URL is:\n\n' + $(event.target).attr('title') + '\n\nContinue?')) + { + try { + window.open($(event.target).attr('title')); + } catch(ex) { + alert('Malformed URL'); + } + } + }); + + // last comment btn + $('#last-comment-btn') + .click(function(event) { + event.preventDefault(); + var id = $('.comment:last')[0].parentNode.id; + scroll_to($('#' + id)); + window.location.hash = id; + }); + + // top btn + $('#top-btn') + .click(function(event) { + event.preventDefault(); + scroll_to($('body')); + }); + + // use non-native tooltips for relative times and bug summaries + $('.rel-time, .bz_bug_link').tooltip({ + position: { my: "left top+8", at: "left bottom", collision: "flipfit" }, + show: { effect: 'none' }, + hide: { effect: 'none' } + }); + + // tooltips create a new ui-helper-hidden-accessible div each time a + // tooltip is shown. this is never removed leading to memory leak and + // bloated dom. http://bugs.jqueryui.com/ticket/10689 + $('.ui-helper-hidden-accessible').remove(); + + // product/component info + $('.spin-toggle') + .click(function(event) { + event.preventDefault(); + var latch = $($(event.target).data('latch')); + var el_for = $($(event.target).data('for')); + + if (latch.data('expanded')) { + latch.data('expanded', false).html('▸'); + el_for.hide(); + } + else { + latch.data('expanded', true).html('▾'); + el_for.show(); + } + }); + + // cc list + $('#cc-latch, #cc-summary') + .click(function(event) { + event.preventDefault(); + var latch = $('#cc-latch'); + + if (latch.data('expanded')) { + latch.data('expanded', false).html('▸'); + $('#cc-list').hide(); + } + else { + latch.data('expanded', true).html('▾'); + $('#cc-list').show(); + if (!latch.data('fetched')) { + $('#cc-list').html( + ' Loading...' + ); + bugzilla_ajax( + { + url: 'rest/bug_modal/cc/' + BUGZILLA.bug_id + }, + function(data) { + $('#cc-list').html(data.html); + latch.data('fetched', true); + } + ); + } + } + }); + + // copy summary to clipboard + if ($('#copy-summary').length) { + var zero = new ZeroClipboard($('#copy-summary')); + zero.on({ + 'error': function(event) { + console.log(event.message); + zero.destroy(); + $('#copy-summary').hide(); + + }, + 'copy': function(event) { + var clipboard = event.clipboardData; + clipboard.setData('text/plain', 'Bug ' + BUGZILLA.bug_id + ' - ' + $('#field-value-short_desc').text()); + } + }); + } + + // + // anything after this point is only executed for logged in users + // + + if (BUGZILLA.user.id === 0) return; + + // edit/save mode button + $('#mode-btn') + .click(function(event) { + event.preventDefault(); + + // hide buttons, old error messages + $('#mode-btn-readonly').hide(); + + // toggle visibility + $('.edit-hide').hide(); + $('.edit-show').show(); + + // expand specific modules + $('#module-details .module-header').each(function() { + if ($(this.parentNode).find('.module-content:visible').length === 0) { + $(this).click(); + } + }); + + // if there's no current user-story, it's a better experience if it's editable by default + if ($('#cf_user_story').val() === '') { + $('#user-story-edit-btn').click(); + } + + // "loading.." ui + $('#mode-btn-loading').show(); + $('#cancel-btn').prop('disabled', true); + $('#mode-btn').prop('disabled', true); + + // load the missing select data + bugzilla_ajax( + { + url: 'rest/bug_modal/edit/' + BUGZILLA.bug_id + }, + function(data) { + $('#mode-btn').hide(); + + // populate select menus + $.each(data.options, function(key, value) { + var el = $('#' + key); + if (!el) return; + var selected = el.val(); + el.empty(); + $(value).each(function(i, v) { + el.append($('