/** * Copyright (c) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you * may not use this file except in compliance with the License. You may * obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ (function($) { var _isModalOpen = false; var _isReadOnly = true; var _allTestsSet = new Set(); var _allBranches = []; var _allDevices = []; var _writableSummary = 'Known test failures are acknowledged below for specific branch and \ device configurations, and corresponding test breakage alerts will be silenced. Click an \ entry to edit or see more information about the test failure.' var _readOnlySummary = 'Known test failures are acknowledged below for specific branch and \ device configurations, and corresponding test breakage alerts will be silenced. Click an \ entry to see more information about the test failure. To add, edit, or remove a test \ acknowledgment, contact a VTS Dashboard administrator.' $.widget('custom.sizedAutocomplete', $.ui.autocomplete, { options: { parent: '' }, _resizeMenu: function() { this.menu.element.outerWidth($(this.options.parent).width()); } }); /** * Remove an acknowledgment from the list. * @param ack (jQuery object) The object for acknowledgment. * @param key (String) The value to display next to the label. */ function removeAcknowledgment(ack, key) { if (ack.hasClass('disabled')) { return; } ack.addClass('disabled'); $.ajax({ url: '/api/test_acknowledgments/' + key, type: 'DELETE' }).always(function() { ack.removeClass('disabled'); }).then(function() { ack.slideUp(150, function() { ack.remove(); }); }); } /** * Callback for when a chip is removed from a chiplist. * @param text (String) The value stored in the chip. * @param allChipsSet (Set) The set of all chip values. * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips. */ function chipRemoveCallback(text, allChipsSet, allIndicator) { allChipsSet.delete(text); if (allChipsSet.size == 0) { allIndicator.show(); } } /** * Add chips to the chip UI. * @param allChipsSet (Set) The set of all chip values. * @param container (jQuery object) The object in which to insert the chips. * @param chipList (list) The list of chip values to insert. * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips. */ function addChips(allChipsSet, container, chipList, allIndicator) { if (chipList && chipList.length > 0) { chipList.forEach(function(text) { if (allChipsSet.has(text)) return; var chip = $('' + text + ''); if (!_isReadOnly) { var icon = $('clear').appendTo(chip); icon.click(function() { chipRemoveCallback(text, allChipsSet, allIndicator); }); } chip.appendTo(container); allChipsSet.add(text); }); allIndicator.hide(); } } /** * Create a chip input UI. * @param container (jQuery object) The object in which to insert the input box. * @param placeholder (String) The placeholder text to display in the input. * @param allChipsSet (Set) The set of all chip values. * @param chipContainer (jQuery object) The object in which to insert new chips from the input. * @param allIndicator (jQuery object) The object for "All" indicator adjacent to the chips. * @returns The chip input jQuery object. */ function addChipInput(container, placeholder, allChipsSet, chipContainer, allIndicator) { var input = $(''); input.attr('placeholder', placeholder); input.keyup(function(e) { if (e.keyCode === 13 && input.val().trim()) { addChips(allChipsSet, chipContainer, [input.val()], allIndicator); input.val(''); } }); var addButton = $('add'); addButton.click(function() { if (input.val().trim()) { addChips(allChipsSet, chipContainer, [input.val()], allIndicator); input.val(''); addButton.hide(); } }); addButton.hide(); input.focus(function() { addButton.show(); }); input.focusout(function() { if (!input.val().trim()) { addButton.hide(); } }); var holder = $('
').appendTo(container); input.appendTo(holder); addButton.appendTo(holder); return input; } /** * Callback to save changes to the acknowledgment. * @param ack (jQuery object) The object for acknowledgment. * @param modal (jQuery object) The jQueryUI modal object which invoked the callback. * @param key (String) The key associated with the acknowledgment. * @param test (String) The test name in the acknowledgment. * @param branchSet (Set) The set of all branches in the acknowledgment. * @param deviceSet (Set) The set of all devoces in the acknowledgment. * @param testCaseSet (Set) The set of all test cases in the acknowledgment. * @param note (String) The note in the acknowledgment. */ function saveCallback(ack, modal, key, test, branchSet, deviceSet, testCaseSet, note) { var allEmpty = true; var firstUnemptyInput = null; var vals = modal.find('.modal-section>.input-container>input').each(function(_, input) { if (!!$(input).val()) { allEmpty = false; if (!firstUnemptyInput) firstUnemptyInput = $(input); } }); if (!allEmpty) { firstUnemptyInput.focus(); return false; } var branches = Array.from(branchSet); branches.sort(); var devices = Array.from(deviceSet); devices.sort(); var testCaseNames = Array.from(testCaseSet); testCaseNames.sort(); var data = { 'key' : key, 'testName' : test, 'branches' : branches, 'devices' : devices, 'testCaseNames' : testCaseNames, 'note': note }; $.post('/api/test_acknowledgments', JSON.stringify(data)).done(function(newKey) { var newAck = createAcknowledgment(newKey, test, branches, devices, testCaseNames, note); if (key == null) { ack.replaceWith(newAck.hide()); newAck.slideDown(150); } else { ack.replaceWith(newAck); } }).always(function() { modal.modal({ complete: function() { _isModalOpen = false; } }); modal.modal('close'); }); } /** * Callback to save changes to the acknowledgment. * @param ack (jQuery object) The object for the acknowledgment. * @param key (String) The key associated with the acknowledgment. * @param test (String) The test name in the acknowledgment. * @param branches (list) The list of all branches in the acknowledgment. * @param devices (Set) The list of all devoces in the acknowledgment. * @param testCases (Set) The list of all test cases in the acknowledgment. * @param note (String) The note in the acknowledgment. */ function showModal(ack, key, test, branches, devices, testCases, note) { if (_isModalOpen) { return; } _isModalOpen = true; var wrapper = $('#modal'); wrapper.empty(); wrapper.modal(); var content = $(''); var row = $('
').appendTo(content); row.append('
Test: ' + test + '
'); var branchSet = new Set(); var branchContainer = $('').appendTo(row); var branchHeader = $('
').appendTo(branchContainer); branchHeader.append('Branches:'); var allBranchesLabel = $(' All').appendTo(branchHeader); var branchChips = $('
').appendTo(branchContainer); addChips(branchSet, branchChips, branches, allBranchesLabel); if (!_isReadOnly) { var branchInput = addChipInput( branchContainer, 'Specify a branch...', branchSet, branchChips, allBranchesLabel); branchInput.sizedAutocomplete({ source: _allBranches, classes: { 'ui-autocomplete': 'card autocomplete-dropdown' }, parent: branchInput }); } var deviceSet = new Set(); var deviceContainer = $('').appendTo(row); var deviceHeader = $('
').appendTo(deviceContainer); deviceHeader.append('Devices:'); var allDevicesLabel = $(' All').appendTo(deviceHeader); var deviceChips = $('
').appendTo(deviceContainer); addChips(deviceSet, deviceChips, devices, allDevicesLabel); if (!_isReadOnly) { var deviceInput = addChipInput( deviceContainer, 'Specify a device...', deviceSet, deviceChips, allDevicesLabel); deviceInput.sizedAutocomplete({ source: _allDevices, classes: { 'ui-autocomplete': 'card autocomplete-dropdown' }, parent: deviceInput }); } var testCaseSet = new Set(); var testCaseContainer = $('').appendTo(row); var testCaseHeader = $('
').appendTo(testCaseContainer); testCaseHeader.append('Test Cases:'); var allTestCasesLabel = $(' All').appendTo(testCaseHeader); var testCaseChips = $('
').appendTo( testCaseContainer); addChips(testCaseSet, testCaseChips, testCases, allTestCasesLabel); var testCaseInput = null; if (!_isReadOnly) { testCaseInput = addChipInput( testCaseContainer, 'Specify a test case...', testCaseSet, testCaseChips, allTestCasesLabel); } row.append('
Note:
'); var inputField = $('
').appendTo(row); var textArea = $(''); textArea.addClass('materialize-textarea note-field'); textArea.appendTo(inputField); textArea.val(note); if (_isReadOnly) { textArea.attr('disabled', true); } content.appendTo(wrapper); var footer = $(''); if (!_isReadOnly) { var save = $('Save').appendTo(footer); save.click(function() { saveCallback(ack, wrapper, key, test, branchSet, deviceSet, testCaseSet, textArea.val()); }); } var close = $('Close').appendTo(footer); close.click(function() { wrapper.modal({ complete: function() { _isModalOpen = false; } }); wrapper.modal('close'); }) footer.appendTo(wrapper); if (!_isReadOnly) { $.get('/api/test_run?test=' + test + '×tamp=latest').done(function(data) { var allTestCases = data.reduce(function(array, column) { return array.concat(column.data); }, []); testCaseInput.sizedAutocomplete({ source: allTestCases, classes: { 'ui-autocomplete': 'card autocomplete-dropdown' }, parent: testCaseInput }); }).always(function() { wrapper.modal('open'); }); } else { wrapper.modal('open'); } } /** * Create a test acknowledgment object. * @param key (String) The key associated with the acknowledgment. * @param test (String) The test name in the acknowledgment. * @param branches (list) The list of all branches in the acknowledgment. * @param devices (Set) The list of all devoces in the acknowledgment. * @param testCases (Set) The list of all test cases in the acknowledgment. * @param note (String) The note in the acknowledgment. */ function createAcknowledgment(key, test, branches, devices, testCases, note) { var wrapper = $('
'); var details = $('
').appendTo(wrapper); details.addClass(_isReadOnly ? 's12' : 's11') var testDiv = $('
' + test + '
').appendTo(details); var infoBtn = $('').appendTo(testDiv); infoBtn.append('info_outline'); details.click(function() { showModal(wrapper, key, test, branches, devices, testCases, note); }); var branchesSummary = 'All'; if (!!branches && branches.length == 1) { branchesSummary = branches[0]; } else if (!!branches && branches.length > 1) { branchesSummary = branches[0]; branchesSummary += ' (+' + (branches.length - 1) + ')'; } $('
Branches: ' + branchesSummary + '
').appendTo(details); var devicesSummary = 'All'; if (!!devices && devices.length == 1) { devicesSummary = devices[0]; } else if (!!devices && devices.length > 1) { devicesSummary = devices[0]; devicesSummary += ' (+' + (devices.length - 1) + ')'; } $('
Devices: ' + devicesSummary + '
').appendTo(details); var testCaseSummary = 'All'; if (!!testCases && testCases.length == 1) { testCaseSummary = testCases[0]; } else if (!!testCases && testCases.length > 1) { testCaseSummary = testCases[0]; testCaseSummary += ' (+' + (testCases.length - 1) + ')'; } details.append('
Test Cases: ' + testCaseSummary + '
'); if (!_isReadOnly) { var btnContainer = $('
'); var clear = $(''); clear.append('clear'); clear.attr('title', 'Remove'); clear.click(function() { removeAcknowledgment(wrapper, key); }); clear.appendTo(btnContainer); btnContainer.appendTo(wrapper); } return wrapper; } /** * Create a test acknowledgments UI. * @param allTests (list) The list of all test names. * @param allBranches (list) The list of all branches. * @param allDevices (list) The list of all device names. * @param testAcknowledgments (list) JSON-serialized TestAcknowledgmentEntity object list. * @param readOnly (boolean) True if the acknowledgments are read-only, false if mutable. */ $.fn.testAcknowledgments = function( allTests, allBranches, allDevices, testAcknowledgments, readOnly) { var self = $(this); _allTestsSet = new Set(allTests); _allBranches = allBranches; _allDevices = allDevices; _isReadOnly = readOnly; var searchRow = $('
'); var headerRow = $('
'); var acks = $('
'); if (!_isReadOnly) { var inputWrapper = $('
'); var input = $('').appendTo(inputWrapper); inputWrapper.append(''); inputWrapper.appendTo(searchRow); input.sizedAutocomplete({ source: allTests, classes: { 'ui-autocomplete': 'card autocomplete-dropdown' }, parent: input }); var btnWrapper = $('
'); var btn = $(''); btn.append('add'); btn.appendTo(btnWrapper); btnWrapper.appendTo(searchRow); btn.click(function() { if (!_allTestsSet.has(input.val())) return; var ack = createAcknowledgment(undefined, input.val()); ack.hide().prependTo(acks); showModal(ack, undefined, input.val()); }); searchRow.appendTo(self); } var headerCol = $('
').appendTo(headerRow); if (_isReadOnly) { headerCol.append('

' + _readOnlySummary + '

'); } else { headerCol.append('

' + _writableSummary + '

'); } headerRow.appendTo(self); testAcknowledgments.forEach(function(ack) { var wrapper = createAcknowledgment( ack.key, ack.testName, ack.branches, ack.devices, ack.testCaseNames, ack.note); wrapper.appendTo(acks); }); acks.appendTo(self); self.append(''); }; })(jQuery);