Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions local_testing/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Local testing

Viewing `ExampleApp/local_testing/test.html` in your browser will allow you to test without using the [App Creator](https://www.cloudflare.com/apps/developer/app-creator )

* Please modify **`window.INSTALL_OPTIONS`** in `ExampleApp/local_testing/local.js` to edit the default **`INSTALL_OPTIONS`** that will be tested. There should be a default value for every option in `install.json`
43 changes: 43 additions & 0 deletions local_testing/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
window.INSTALL = window.INSTALL || {}
window.INSTALL_OPTIONS = {
// TODO: edit the default testing options to include every option
// install.json
"location": {
"selector": "body",
"method": "before"
},
"message":
"<p>Welcome to Cloudflare Apps! This is our example app.</p><p>Download this app every time you want to make a new project.</p>"
}
// Below is declaring the INSTALL.createElement function which is part of the
// App creator API
INSTALL.createElement = function(location, element){
element = element || document.createElement('div')
if (!location){
//default to append to the document body if location not given
elementMethodSwitch(element, document.body, 'append')
return element
}
var locations = document.querySelectorAll(location.selector)
locations.forEach((el) => {
el.appendChild(element)
el = elementMethodSwitch(element, el, location.method)
})
return element
}
function elementMethodSwitch(el, parent, method){
//implements the specified method by adding el to the parent
var grandpa = parent.parentElement
switch (method){
case 'append':
return parent.appendChild(el)
case 'prepend':
return parent.prepend(el)
case 'before':
return grandpa.insertBefore(el, parent)
case 'after':
return grandpa.insertBefore(el, parent.nextSibling)
case 'replace':
return grandpa.replaceChild(el, parent)
}
}
11 changes: 11 additions & 0 deletions local_testing/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="../source/app.css">
<script src="local.js"></script>
<script src="../source/app.js"></script>
</head>
<body>
<p>Example Body Text </p>
</body>
</html>
1 change: 0 additions & 1 deletion source/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
window.INSTALL_SCOPE = {
setOptions: function setOptions (nextOptions) {
options = nextOptions

updateElement()
}
}
Expand Down
34 changes: 34 additions & 0 deletions source/source/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function () {
'use strict'

if (!window.addEventListener) return // Check for IE9+

var options = INSTALL_OPTIONS
var element

// updateElement runs every time the options are updated.
// Most of your code will end up inside this function.
function updateElement () {
element = INSTALL.createElement(options.location, element)

// Set the app attribute to your app's dash-delimited alias.
element.setAttribute('app', 'example')
element.innerHTML = options.message
}

// INSTALL_SCOPE is an object that is used to handle option changes without refreshing the page.
window.INSTALL_SCOPE = {
setOptions: function setOptions (nextOptions) {
options = nextOptions

updateElement()
}
}

// This code ensures that the app doesn't run before the page is loaded.
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', updateElement)
} else {
updateElement()
}
}())