Starting to add scripts

This commit is contained in:
Eduard Urbach 2016-12-03 00:23:05 +09:00
parent a45613be84
commit c2d9817ec5
14 changed files with 20 additions and 388 deletions

View File

@ -24,6 +24,10 @@ func main() {
// CSS // CSS
app.SetStyle(components.CSS()) app.SetStyle(components.CSS())
// HTTPS
app.Security.Certificate, _ = ioutil.ReadFile("security/fullchain.pem")
app.Security.Key, _ = ioutil.ReadFile("security/privkey.pem")
// Layout // Layout
app.Layout = func(ctx *aero.Context, content string) string { app.Layout = func(ctx *aero.Context, content string) string {
return components.Layout(content) return components.Layout(content)

1
pages/anime/anime.js Normal file
View File

@ -0,0 +1 @@
console.log("Hello Typescript");

View File

@ -6,7 +6,7 @@ profile-boot-duration = 2s
position relative position relative
left calc(content-padding * -1) left calc(content-padding * -1)
top calc(content-padding * -1) top calc(content-padding-top * -1)
min-width calc(100% + content-padding * 2) min-width calc(100% + content-padding * 2)
padding calc(content-padding * 2) padding calc(content-padding * 2)

2
run.sh
View File

@ -1,2 +0,0 @@
#!/bin/bash
pack && go build && ./notify.moe

View File

@ -1,12 +0,0 @@
document.addEventListener('keydown', e => {
// Alt + A = Staff info
if(e.keyCode === 65 && e.altKey) {
let staffInfo = $('staff-info')
if(staffInfo.style.display !== 'block') {
staffInfo.style.display = 'block'
} else {
staffInfo.style.display = 'none'
}
}
})

View File

@ -1,21 +0,0 @@
function find(id: string) {
return document.getElementById(id)
}
function get(url: string, body?: Object): Promise<string> {
return new Promise(function(resolve, reject) {
resolve("")
})
}
function post(url: string, body?: Object): Promise<string> {
return new Promise(function(resolve, reject) {
resolve("")
})
}
export {
find,
get,
post
}

View File

@ -1,31 +0,0 @@
$.updateAvatars = function() {
let images = document.querySelectorAll('.user-image')
for(let i = 0; i < images.length; ++i) {
let img = <HTMLImageElement> images[i]
if(img.naturalWidth === 0) {
img.onload = function() {
this.style.opacity = '1.0'
}
img.onerror = function() {
this.src = '/images/elements/no-gravatar.svg'
this.style.opacity = '1.0'
}
} else {
img.style.opacity = '1.0'
}
}
// Tooltips
// let links = document.querySelectorAll('.user')
//
// for(let i = 0; i < links.length; ++i) {
// let link = links[i]
//
// link.classList.add('tooltip')
// link.setAttribute('data-tooltip', link.title)
// link.title = ''
// }
}

1
scripts/init.ts Normal file
View File

@ -0,0 +1 @@
console.log("Hello World")

View File

@ -1,10 +0,0 @@
// Fix Facebook login hash in URL
if(window.location.hash && window.location.hash === '#_=_') {
window.history.pushState('', document.title, window.location.pathname)
}
// Fade out loading animation
document.addEventListener('DOMContentLoaded', function(event) {
$.loadingAnimation.classList.add('fade-out')
$.updateAvatars()
})

View File

@ -1,46 +0,0 @@
import * as $ from './aqua'
function like(type, id) {
$.post(`/api/${type}/like/` + id)
$.find('like-' + id).style.display = 'none'
$.find('unlike-' + id).style.display = 'inline-block'
let likes = $.find('likes-' + id)
likes.innerHTML = (parseInt(likes.textContent) + 1).toString()
}
function unlike(type, id) {
$.post(`/api/${type}/unlike/` + id)
$.find('like-' + id).style.display = 'inline-block'
$.find('unlike-' + id).style.display = 'none'
let likes = $.find('likes-' + id)
likes.innerHTML = (parseInt(likes.textContent) - 1).toString()
}
function edit(id) {
$.find('source-' + id).style.display = 'block'
$.find('save-' + id).style.display = 'block'
$.find('render-' + id).style.display = 'none'
$.find('toolbar-' + id).style.display = 'none'
}
function cancelEdit(id) {
$.find('source-' + id).style.display = 'none'
$.find('save-' + id).style.display = 'none'
$.find('render-' + id).style.display = ''
$.find('toolbar-' + id).style.display = ''
}
function saveEdit(type, id) {
let source = <HTMLInputElement> $.find('source-' + id)
let text = source.value
$.post(`/api/${type}/edit/` + id, {
id,
text
}).then(response => {
$.find('render-' + id).innerHTML = response
cancelEdit(id)
})
}

View File

@ -1,204 +0,0 @@
var isPushEnabled = false
declare var ServiceWorkerRegistration: any
declare var Notification: any
interface Navigator {
serviceWorker: any
}
function subscribeOnServer(subscription) {
console.log('Send subscription to server...')
console.log(subscription)
$.post('/api/notifications/subscribe', subscription).then(function(response) {
console.log(response)
})
}
function unsubscribeOnServer(subscription) {
console.log('Send unsubscription to server...')
console.log(subscription)
$.post('/api/notifications/unsubscribe', subscription).then(function(response) {
console.log(response)
})
}
function sendTestNotification() {
console.log('Sending test notification...')
$.get('/api/notifications/test').then(function(response) {
// ...
})
}
function subscribe() {
// Disable the button so it can't be changed while
// we process the permission request
let pushButton = <HTMLButtonElement> document.querySelector('.push-button')
if(pushButton)
pushButton.disabled = true
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true
// applicationServerKey: new TextEncoder('binary').encode('BLxjquZGLvRnYGkV_xlkuAIilZzHJLDdGUZAmvq4pqev5uQHBmYRxJqfbFQFQn2kYfe5SRBwiNfuiakHn-3KR_k')
}).then(function(subscription) {
// The subscription was successful
isPushEnabled = true
if(pushButton) {
pushButton.textContent = 'Disable Notifications'
pushButton.disabled = false
}
let testButton = <HTMLButtonElement> document.querySelector('.test-notification')
if(testButton)
testButton.disabled = false
// TODO: Send the subscription.endpoint to your server
// and save it to send a push message at a later date
return subscribeOnServer(subscription)
}).catch(function(e) {
if(Notification.permission === 'denied') {
// The user denied the notification permission which
// means we failed to subscribe and the user will need
// to manually change the notification permission to
// subscribe to push messages
console.warn('Permission for Notifications was denied')
if(pushButton)
pushButton.disabled = true
} else {
// A problem occurred with the subscription common reasons
// include network errors, and lacking gcm_sender_id and/or
// gcm_user_visible_only in the manifest.
console.error('Unable to subscribe to push.', e)
if(pushButton) {
pushButton.disabled = false
pushButton.textContent = 'Enable Notifications'
}
}
})
})
}
function unsubscribe() {
let pushButton = <HTMLButtonElement> document.querySelector('.push-button')
if(pushButton)
pushButton.disabled = true
let testButton = <HTMLButtonElement> document.querySelector('.test-notification')
if(testButton)
testButton.disabled = true
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// To unsubscribe from push messaging, you need get the
// subscription object, which you can call unsubscribe() on.
serviceWorkerRegistration.pushManager.getSubscription().then(function(pushSubscription) {
// Check we have a subscription to unsubscribe
if(!pushSubscription) {
// No subscription object, so set the state
// to allow the user to subscribe to push
isPushEnabled = false
if(pushButton) {
pushButton.disabled = false
pushButton.textContent = 'Enable Notifications'
}
return
}
unsubscribeOnServer(pushSubscription)
// We have a subscription, so call unsubscribe on it
pushSubscription.unsubscribe().then(function(successful) {
if(pushButton) {
pushButton.disabled = false
pushButton.textContent = 'Enable Notifications'
}
isPushEnabled = false
}).catch(function(e) {
// We failed to unsubscribe, this can lead to
// an unusual state, so may be best to remove
// the users data from your data store and
// inform the user that you have done so
console.log('Unsubscription error: ', e)
if(pushButton) {
pushButton.disabled = false
pushButton.textContent = 'Enable Notifications'
}
})
}).catch(function(e) {
console.error('Error thrown while unsubscribing from push messaging.', e)
})
})
}
// Once the service worker is registered set the initial state
function initialiseState(registration) {
console.log('Initialise state...')
console.log('Scope:', registration.scope)
// Are Notifications supported in the service worker?
if(!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications aren\'t supported.')
return
}
// Check the current Notification permission.
// If its denied, it's a permanent block until the
// user changes the permission
if(Notification.permission === 'denied') {
console.warn('The user has blocked notifications.')
return
}
// Check if push messaging is supported
if(!('PushManager' in window)) {
console.warn('Push messaging isn\'t supported.')
return
}
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
console.log('Get subscription...')
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) {
console.log('Enable Push UI...')
// Enable any UI which subscribes / unsubscribes from
// push messages.
let pushButton = <HTMLButtonElement> document.querySelector('.push-button')
if(pushButton)
pushButton.disabled = false
if(!subscription) {
// We aren't subscribed to push, so set UI
// to allow the user to enable push
return
}
// Keep your server in sync with the latest subscriptionId
subscribeOnServer(subscription)
// Set your UI to show they have subscribed for
// push messages
if(pushButton)
pushButton.textContent = 'Disable Notifications'
isPushEnabled = true
// Enable test notifications
let testButton = <HTMLButtonElement> document.querySelector('.test-notification')
if(testButton)
testButton.disabled = false
}).catch(function(err) {
console.warn('Error during getSubscription()', err)
})
})
}

View File

@ -1,61 +0,0 @@
function makeSaveable(apiEndpoint: string, postSaveCallback?: (string, any) => void) {
$.save = function(e) {
var item = e.target;
if($.saving)
return;
$.saving = true;
var key = item.id;
var value = item.value ? item.value : '';
var old = item.dataset.old ? item.dataset.old : '';
item.classList.add('saving');
$.content.style.cursor = 'wait';
$.post(apiEndpoint, {
function: 'save',
key: key,
value: value,
old: old
}).then(function(response) {
if(postSaveCallback)
postSaveCallback(key, response);
}).catch(function(error) {
console.error(error);
}).then(function() {
$.get('/_' + location.pathname).then(function(newPageCode) {
var focusedElementId = document.activeElement.id;
var focusedElementValue = (<HTMLInputElement> document.activeElement).value;
$.setContent(newPageCode);
// Re-focus previously selected element
if(focusedElementId) {
var focusedElement = $(focusedElementId);
if(focusedElement) {
focusedElement.value = focusedElementValue;
if(focusedElement.select)
focusedElement.select();
else if(focusedElement.focus)
focusedElement.focus();
}
}
$.content.style.cursor = 'auto';
$.saving = false;
});
});
};
var myNodeList = document.querySelectorAll('.save-on-change');
for(var i = 0; i < myNodeList.length; ++i) {
var element = <HTMLInputElement> myNodeList[i];
element.onchange = $.save;
element.dataset['old'] = element.value;
}
}

View File

@ -30,5 +30,8 @@ strong
em em
font-style italic font-style italic
img
backface-visibility hidden
.spacer .spacer
flex 1 flex 1

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "./"
},
"compileOnSave": true,
"buildOnSave": false
}