Refactor front-end

This commit is contained in:
Pig Fang 2019-03-17 15:59:52 +08:00
parent 9633625e3d
commit 3b033be048
11 changed files with 34 additions and 74 deletions

View File

@ -21,10 +21,8 @@ export default {
if (errno === 0) { if (errno === 0) {
toastr.success(msg) toastr.success(msg)
$('[alt="User Image"]').each(function it() { Array.from(document.querySelectorAll('[alt="User Image"]'))
// eslint-disable-next-line no-invalid-this .forEach(el => (el.src += `?${new Date().getTime()}`))
$(this).prop('src', `${$(this).attr('src')}?${new Date().getTime()}`)
})
} else { } else {
toastr.warning(msg) toastr.warning(msg)
} }

View File

@ -301,10 +301,8 @@ export default {
{ new_nickname: nickname } { new_nickname: nickname }
) )
if (errno === 0) { if (errno === 0) {
$('.nickname').each(function it() { Array.from(document.querySelectorAll('.nickname'))
// eslint-disable-next-line no-invalid-this .forEach(el => (el.textContent = nickname))
$(this).text(nickname)
})
return swal({ type: 'success', text: msg }) return swal({ type: 'success', text: msg })
} }
return swal({ type: 'warning', text: msg }) return swal({ type: 'warning', text: msg })

View File

@ -1,4 +1,3 @@
import $ from 'jquery'
import { init } from './net' import { init } from './net'
export async function checkForUpdates() { export async function checkForUpdates() {
@ -7,12 +6,11 @@ export async function checkForUpdates() {
if (response.ok) { if (response.ok) {
const data = await response.json() const data = await response.json()
if (data.available) { if (data.available) {
const dom = ` document.querySelector(`[href="${blessing.base_url}/admin/update"]`)
<span class="pull-right-container"> .innerHTML += `
<span class="label label-primary pull-right">v${data.latest}</span> <span class="pull-right-container">
</span>` <span class="label label-primary pull-right">v${data.latest}</span>
</span>`
$(`[href="${blessing.base_url}/admin/update"]`).append(dom)
} }
} }
} }
@ -23,12 +21,11 @@ export async function checkForPluginUpdates() {
if (response.ok) { if (response.ok) {
const data = await response.json() const data = await response.json()
if (data.available) { if (data.available) {
const dom = ` document.querySelector(`[href="${blessing.base_url}/admin/plugins/market"]`)
<span class="pull-right-container"> .innerHTML += `
<span class="label label-success pull-right">${data.plugins.length}</span> <span class="pull-right-container">
</span>` <span class="label label-success pull-right">${data.plugins.length}</span>
</span>`
$(`[href="${blessing.base_url}/admin/plugins/market"]`).append(dom)
} }
} }
} }

View File

@ -18,7 +18,7 @@ Vue.mixin({
}, },
}) })
$(document).ready(() => { document.addEventListener('loadend', () => {
$('input').iCheck({ $('input').iCheck({
radioClass: 'iradio_square-blue', radioClass: 'iradio_square-blue',
checkboxClass: 'icheckbox_square-blue', checkboxClass: 'icheckbox_square-blue',
@ -26,6 +26,7 @@ $(document).ready(() => {
$('[data-toggle="tooltip"]').tooltip() $('[data-toggle="tooltip"]').tooltip()
}) })
;(() => { ;(() => {
const list = [ const list = [
{ {

View File

@ -1,4 +1,3 @@
import $ from 'jquery'
import { post } from './net' import { post } from './net'
import { swal } from './notify' import { swal } from './notify'
import { trans } from './i18n' import { trans } from './i18n'
@ -20,4 +19,8 @@ export async function logout() {
swal({ type: 'success', text: msg }) swal({ type: 'success', text: msg })
} }
$('#logout-button').click(logout) const button = document.querySelector('#logout-button')
/* istanbul ignore next, not all pages contains this button. */
if (button) {
button.addEventListener('click', logout)
}

View File

@ -4,21 +4,6 @@ import Swal from 'sweetalert2'
import toastr from 'toastr' import toastr from 'toastr'
import { trans } from './i18n' import { trans } from './i18n'
/**
* Show message to div#msg with level
*
* @param {string} msg
* @param {string} type
* @return {void}
*/
export function showMsg(msg, type = 'info') {
$('#msg')
.removeClass()
.addClass('callout')
.addClass(`callout-${type}`)
.html(msg)
}
/** /**
* Show modal if error occured when sending an ajax request. * Show modal if error occured when sending an ajax request.
* *
@ -64,10 +49,11 @@ export function showModal(msg, title = 'Message', type = 'default', options = {}
</div> </div>
</div>` </div>`
$(dom).on('hidden.bs.modal', /* istanbul ignore next */ function modal() { $(dom)
.on('hidden.bs.modal', /* istanbul ignore next */ function modal() {
// eslint-disable-next-line no-invalid-this // eslint-disable-next-line no-invalid-this
destroyOnClose && $(this).remove() destroyOnClose && $(this).remove()
}) })
.modal(options) .modal(options)
} }
@ -85,4 +71,4 @@ export function swal(options) {
window.toastr = toastr window.toastr = toastr
window.swal = swal window.swal = swal
blessing.notify = { showMsg, showModal } blessing.notify = { showModal }

View File

@ -25,7 +25,6 @@ declare global {
} }
notify: { notify: {
showMsg(message: string, type?: string): void
showModal( showModal(
message: string, message: string,
title?: string, title?: string,

View File

@ -98,19 +98,11 @@ test('set as avatar', async () => {
swal swal
.mockResolvedValueOnce({ dismiss: 'cancel' }) .mockResolvedValueOnce({ dismiss: 'cancel' })
.mockResolvedValue({}) .mockResolvedValue({})
window.$ = jest.fn(() => ({
each(fn) {
fn()
},
prop() {},
attr() {
return ''
},
}))
const wrapper = mount(ClosetItem, { propsData: factory() }) const wrapper = mount(ClosetItem, { propsData: factory() })
const button = wrapper.findAll('.dropdown-menu > li').at(2) const button = wrapper.findAll('.dropdown-menu > li').at(2)
.find('a') .find('a')
document.body.innerHTML += '<img alt="User Image" src="a">'
button.trigger('click') button.trigger('click')
await wrapper.vm.$nextTick() await wrapper.vm.$nextTick()
@ -123,7 +115,7 @@ test('set as avatar', async () => {
await flushPromises() await flushPromises()
await wrapper.vm.$nextTick() await wrapper.vm.$nextTick()
expect(Vue.prototype.$http.post).toBeCalledWith('/user/profile/avatar', { tid: 1 }) expect(Vue.prototype.$http.post).toBeCalledWith('/user/profile/avatar', { tid: 1 })
expect(window.$).toBeCalledWith('[alt="User Image"]') expect(document.querySelector('img').src).toMatch(/\d+$/)
}) })
test('no avatar option if texture is cape', () => { test('no avatar option if texture is cape', () => {

View File

@ -96,14 +96,9 @@ test('change nickname', async () => {
swal.mockResolvedValueOnce({}) swal.mockResolvedValueOnce({})
.mockResolvedValueOnce({ dismiss: 1 }) .mockResolvedValueOnce({ dismiss: 1 })
.mockResolvedValue({}) .mockResolvedValue({})
window.$ = jest.fn(() => ({
each(fn) {
fn()
},
text() {},
}))
const wrapper = mount(Profile) const wrapper = mount(Profile)
const button = wrapper.find('[data-test=changeNickName]') const button = wrapper.find('[data-test=changeNickName]')
document.body.innerHTML += '<span class="nickname"></span>'
button.trigger('click') button.trigger('click')
expect(Vue.prototype.$http.post).not.toBeCalled() expect(Vue.prototype.$http.post).not.toBeCalled()
@ -130,6 +125,7 @@ test('change nickname', async () => {
button.trigger('click') button.trigger('click')
await flushPromises() await flushPromises()
expect(swal).toBeCalledWith({ type: 'success', text: 'o' }) expect(swal).toBeCalledWith({ type: 'success', text: 'o' })
expect(document.querySelector('.nickname').textContent).toBe('nickname')
}) })
test('change email', async () => { test('change email', async () => {

View File

@ -9,16 +9,6 @@ jest.mock('sweetalert2', () => ({
fire() {}, fire() {},
})) }))
test('show message', () => {
document.body.innerHTML = '<div id=msg class="callout-x"></div>'
notify.showMsg('hi')
const element = $('#msg')
expect(element.hasClass('callout')).toBeTrue()
expect(element.hasClass('callout-info')).toBeTrue()
expect(element.html()).toBe('hi')
})
test('show AJAX error', () => { test('show AJAX error', () => {
$.fn.modal = function () { $.fn.modal = function () {
document.body.innerHTML = this.html() document.body.innerHTML = this.html()

View File

@ -96,10 +96,10 @@
@include('common.dependencies.script') @include('common.dependencies.script')
<script> <script>
$(document).ready(() => { document.addEventListener('loadend', () => {
checkForUpdates(); checkForUpdates()
checkForPluginUpdates(); checkForPluginUpdates()
}); })
</script> </script>
@yield('script') @yield('script')