Skip to content

Commit bcb67ad

Browse files
committed
Minor update
- Change codebox from a helper to a partial - Should have no user visible effect but will be much easier to maintain
1 parent 2b45f1a commit bcb67ad

27 files changed

+200
-217
lines changed

src/assets/js/collapse.js

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,93 @@ $(document).ready(function() {
1515
collapseSelector(genericCssClass, defaultValue);
1616
});
1717

18-
$('code.codebox').each(function(index) {
19-
var content = $(this).attr('data-content');
18+
$('div.codebox').each(function(index) {
19+
const codeboxElem = $(this);
2020

21-
var elem = $(this);
22-
23-
$.ajax({url:content, dataType:'text'})
21+
$.ajax({url:$(codeboxElem).attr('data-content'), dataType:'text'})
2422
.done(function(data) {
25-
elem.text(data);
26-
elem.removeClass('prettyprinted');
23+
const thisCodeElem = $(codeboxElem).find('code');
24+
$(thisCodeElem).text(data);
25+
$(thisCodeElem).removeClass('prettyprinted');
2726
if (prettyPrint) {
2827
prettyPrint();
2928
}
30-
})
29+
})
30+
31+
$(codeboxElem).find('.codeboxDownloadButton').on('click', function() {
32+
const contentUrl = $(codeboxElem).attr('data-content');
33+
34+
var a = document.createElement('a');
35+
a.href = contentUrl;
36+
a.download = contentUrl.split('/').pop();
37+
document.body.appendChild(a);
38+
a.click();
39+
document.body.removeChild(a);
40+
});
41+
42+
$(codeboxElem).find('.codeboxCopyButton').on('click', function() {
43+
const thisCodeElem = $(codeboxElem).find('code');
44+
45+
console.log('copy', thisCodeElem);
46+
47+
var t = document.createElement('textarea');
48+
document.body.appendChild(t);
49+
$(t).text($(thisCodeElem).text());
50+
t.select();
51+
document.execCommand("copy");
52+
document.body.removeChild(t);
53+
});
54+
55+
$(codeboxElem).find('.codeboxWebIdeButton').on('click', function() {
56+
var a = document.createElement('a');
57+
a.href = 'https://go.particle.io/shared_apps/' + $(this).attr('data-appid');
58+
a.target = '_blank';
59+
document.body.appendChild(a);
60+
a.click();
61+
document.body.removeChild(a);
62+
});
63+
64+
65+
$(codeboxElem).find('.codeboxFlashDeviceButton').on('click', function() {
66+
const thisCodeElem = $(codeboxElem).find('code');
67+
68+
const device = $(codeboxElem).find('select.codeboxFlashDeviceSelect').val();
69+
if (!device || device == 'select') {
70+
return;
71+
}
72+
73+
if (!apiHelper.flashConfirmed) {
74+
const warning = 'Flashing firmware to a device replaces the existing user firmware binary on the device. This can only be undone by locating and flashing the previous firmware on the device.';
75+
76+
if (!confirm(warning)) {
77+
return;
78+
}
79+
80+
apiHelper.flashConfirmed = true;
81+
}
82+
83+
apiHelper.flashDevice(device, $(thisCodeElem).text(), codeboxElem);
3184
});
85+
86+
$(codeboxElem).find('.codeboxUploadSchemaButton').on('click', function() {
87+
const thisCodeElem = $(codeboxElem).find('code');
88+
89+
const product = $(codeboxElem).find('select.codeboxConfigSchemaProductSelect').val();
90+
91+
const warning = 'Uploading a configuration schema replaces the existing configuration schema for all users of this product! An incorrect schema can cause errors opening your product in the console. A backup will be saved in your Downloads directory but you should still exercise caution.';
92+
93+
if (!confirm(warning)) {
94+
return;
95+
}
96+
97+
const schema = $(thisCodeElem).text();
98+
99+
apiHelper.uploadSchemaCodebox(schema, product, 'default', function() {
100+
101+
});
102+
});
103+
104+
});
32105
});
33106

34107
function collapseToggle(id) {
@@ -104,72 +177,4 @@ function hideOverlay() {
104177
$('#imageOverlay').hide();
105178
}
106179

107-
function codeboxDownload(url) {
108-
var a = document.createElement('a');
109-
a.href = url;
110-
a.download = url.split('/').pop();
111-
document.body.appendChild(a);
112-
a.click();
113-
document.body.removeChild(a);
114-
}
115-
116-
function codeboxCopy(id) {
117-
var t = document.createElement('textarea');
118-
document.body.appendChild(t);
119-
$(t).text($('#' + id).text());
120-
t.select();
121-
document.execCommand("copy");
122-
document.body.removeChild(t);
123-
}
124-
125-
function codeboxOpenWebIDE(appid) {
126-
var a = document.createElement('a');
127-
a.href = 'https://go.particle.io/shared_apps/' + appid;
128-
a.target = '_blank';
129-
document.body.appendChild(a);
130-
a.click();
131-
document.body.removeChild(a);
132-
}
133-
134-
function codeboxFlash(id) {
135-
const codebox = $('#' + id).closest('div.codebox');
136-
137-
const device = $(codebox).find('select.codeboxFlashDeviceSelect').val();
138-
if (!device || device == 'select') {
139-
return;
140-
}
141-
142-
if (!apiHelper.flashConfirmed) {
143-
const warning = 'Flashing firmware to a device replaces the existing user firmware binary on the device. This can only be undone by locating and flashing the previous firmware on the device.';
144-
145-
if (!confirm(warning)) {
146-
return;
147-
}
148-
149-
apiHelper.flashConfirmed = true;
150-
}
151-
152-
const code = $('#' + id).text();
153-
154-
apiHelper.flashDevice(device, code, codebox);
155-
}
156-
157-
158-
function codeboxUploadSchema(id) {
159-
const codebox = $('#' + id).closest('div.codebox');
160-
161-
const product = $(codebox).find('select.codeboxConfigSchemaProductSelect').val();
162-
163-
const warning = 'Uploading a configuration schema replaces the existing configuration schema for all users of this product! An incorrect schema can cause errors opening your product in the console. A backup will be saved in your Downloads directory but you should still exercise caution.';
164-
165-
if (!confirm(warning)) {
166-
return;
167-
}
168-
169-
const schema = $('#' + id).text();
170-
171-
apiHelper.uploadSchemaCodebox(schema, product, 'default', function() {
172-
173-
});
174180

175-
}

src/content/datasheets/app-notes/an002-device-powerdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The idea works like this:
2020

2121
Full Code:
2222

23-
{{codebox content="/assets/files/app-notes/AN002/firmware/powerdown.cpp" format="cpp" height="500"}}
23+
{{> codebox content="/assets/files/app-notes/AN002/firmware/powerdown.cpp" format="cpp" height="500"}}
2424

2525

2626
Author: Rick

src/content/datasheets/app-notes/an010-finite-state-machines.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Author: Rick
2727

2828
This is the simplest example of a state machine, which we'll walk through:
2929

30-
{{codebox content="/assets/files/app-notes/AN010/01-Simple/01-Simple.cpp" format="cpp" height="500"}}
30+
{{> codebox content="/assets/files/app-notes/AN010/01-Simple/01-Simple.cpp" format="cpp" height="500"}}
3131

3232

3333
This is just the standard stuff to set the modes:
@@ -234,15 +234,15 @@ Upon waking up from sleep, we go into `STATE_WAIT_CONNECTED` state. We'll almost
234234

235235
This example just shows what the code would look like if we didn't use a state machine. For a simple example like this it may look cleaner, but as your code gets more complex it can get unwieldy quickly!
236236

237-
{{codebox content="/assets/files/app-notes/AN010/02-Linear/02-Linear.cpp" format="cpp" height="500"}}
237+
{{> codebox content="/assets/files/app-notes/AN010/02-Linear/02-Linear.cpp" format="cpp" height="500"}}
238238

239239
One example of the subtle gotchas that can occur: Say you decide to enable the `ApplicationWatchdog`. In each of the two inner delay loops you'd also have to add a call to `checkin()` otherwise the device could end up resetting if it was having trouble connecting. That's not necessary in the state machine examples because the code returns from `loop()` frequently.
240240

241241
## 03-If-Statement
242242

243243
This is basically the same as the **01-Simple** example except if uses an `if` statement instead of switch.
244244

245-
{{codebox content="/assets/files/app-notes/AN010/03-If-Statement/03-If-Statement.cpp" format="cpp" height="500"}}
245+
{{> codebox content="/assets/files/app-notes/AN010/03-If-Statement/03-If-Statement.cpp" format="cpp" height="500"}}
246246

247247

248248
```
@@ -270,7 +270,7 @@ It's mostly just a matter of preference.
270270

271271
While this example is pretty simple, you can imagine if you have a complex program, putting everything in `loop()` with a `switch` or `if` statement can get unwieldy!
272272

273-
{{codebox content="/assets/files/app-notes/AN010/04-Case-Function/04-Case-Function.cpp" format="cpp" height="500"}}
273+
{{> codebox content="/assets/files/app-notes/AN010/04-Case-Function/04-Case-Function.cpp" format="cpp" height="500"}}
274274

275275
One common solution to this is to separate every state out into a separate function.
276276

@@ -316,7 +316,7 @@ void stateWaitConnected() {
316316

317317
One annoyance of the **04-Case-Function** example is that every time you add a new state you need to add an enum value, a case in the switch statement, and a function.
318318

319-
{{codebox content="/assets/files/app-notes/AN010/05-Function-Pointer/05-Function-Pointer.cpp" format="cpp" height="500"}}
319+
{{> codebox content="/assets/files/app-notes/AN010/05-Function-Pointer/05-Function-Pointer.cpp" format="cpp" height="500"}}
320320

321321
One solution to this is to just dispense with the enum and use function pointers. This is used instead of the `State` variable in the previous examples.
322322

@@ -368,7 +368,7 @@ void loop() {
368368
369369
We have a new header file `MainStateMachine.h`. Here's what's in it:
370370
371-
{{codebox content="/assets/files/app-notes/AN010/06-Class/MainStateMachine.h" format="cpp" height="500"}}
371+
{{> codebox content="/assets/files/app-notes/AN010/06-Class/MainStateMachine.h" format="cpp" height="500"}}
372372
373373
You normally declare the `MainStateMachine` as a global variable in your main source file. You should avoid doing much in the constructor, as there are limitations on what is safe at [global object construction time](https://docs.particle.io/reference/device-os/firmware/#global-object-constructors).
374374
@@ -425,7 +425,7 @@ std::function<void(MainStateMachine&)> stateHandler = 0;
425425
426426
### MainStateMachine.cpp
427427
428-
{{codebox content="/assets/files/app-notes/AN010/06-Class/MainStateMachine.cpp" format="cpp" height="500"}}
428+
{{> codebox content="/assets/files/app-notes/AN010/06-Class/MainStateMachine.cpp" format="cpp" height="500"}}
429429
430430
The **MainStateHander.cpp** file has as few interesting features.
431431

src/content/datasheets/app-notes/an012-tracker-1wire.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ The documentation for the library can be found [here](https://github.com/rickkas
164164

165165
### Customize main.cpp
166166

167-
{{codebox content="/assets/files/app-notes/AN012/firmware/main.cpp" format="cpp" height="500"}}
167+
{{> codebox content="/assets/files/app-notes/AN012/firmware/main.cpp" format="cpp" height="500"}}
168168

169169

170170
### Digging In

src/content/datasheets/app-notes/an013-tracker-gpio.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ The documentation for the library can be found [here](https://github.com/rickkas
214214

215215
### Customize main.cpp
216216

217-
{{codebox content="/assets/files/app-notes/AN013/firmware/main.cpp" format="cpp" height="500"}}
217+
{{> codebox content="/assets/files/app-notes/AN013/firmware/main.cpp" format="cpp" height="500"}}

src/content/datasheets/app-notes/an016-tracker-keypad-lcd.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ From the command palette in Workbench, **Particle: Install Library** then enter
201201

202202
If you prefer to edit project.properties directly, add these:
203203

204-
{{codebox content="/assets/files/app-notes/AN016/firmware/AN016.dep" height="120"}}
204+
{{> codebox content="/assets/files/app-notes/AN016/firmware/AN016.dep" height="120"}}
205205

206206
### The full source
207207

208-
{{codebox content="/assets/files/app-notes/AN016/firmware/main.cpp" format="cpp" height="500"}}
208+
{{> codebox content="/assets/files/app-notes/AN016/firmware/main.cpp" format="cpp" height="500"}}
209209

210210

211211
### Explanation

src/content/datasheets/app-notes/an017-tracker-can.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ CAN support requires Tracker Edge v10 or later, which requires 2.0.0-rc.3 or lat
9595

9696
## Full Source
9797

98-
{{codebox content="/assets/files/app-notes/AN017/firmware/main.cpp" format="cpp" height="400"}}
98+
{{> codebox content="/assets/files/app-notes/AN017/firmware/main.cpp" format="cpp" height="400"}}
9999

100100

101101
## Digging In
@@ -433,15 +433,15 @@ The configuration data that is sent between the device and cloud is described by
433433

434434
These are the fields that will be added. This not only describe the data, but match the configuration data in the user firmware, and are also used to describe the console user interface.
435435

436-
{{codebox content="/assets/files/tracker/engine-schema-fragment.json" format="json" height="400"}}
436+
{{> codebox content="/assets/files/tracker/engine-schema-fragment.json" format="json" height="400"}}
437437
438438
---
439439

440440
The easiest way to set the schema is using the control here.
441441

442442
{{> sso}}
443443
444-
{{codebox content="/assets/files/tracker/engine-schema.json" format="json" height="300" configSchema="true"}}
444+
{{> codebox content="/assets/files/tracker/engine-schema.json" format="json" height="300" configSchema="true"}}
445445
446446

447447
You can also set the schema manually using curl:

src/content/datasheets/app-notes/an018-tracker-level.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ From the command palette in Workbench, **Particle: Install Library** then enter
203203

204204
If you prefer to edit project.properties directly, add these:
205205

206-
{{codebox content="/assets/files/app-notes/AN018/firmware/AN018.dep" height="100"}}
206+
{{> codebox content="/assets/files/app-notes/AN018/firmware/AN018.dep" height="100"}}
207207

208208

209209
### The Source
210210

211-
{{codebox content="/assets/files/app-notes/AN018/firmware/main.cpp" format="cpp" height="500"}}
211+
{{> codebox content="/assets/files/app-notes/AN018/firmware/main.cpp" format="cpp" height="500"}}
212212

213213

214214
### Digging in

src/content/datasheets/app-notes/an019-tracker-prototype.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ From the command palette in Workbench, **Particle: Install Library** then enter
9393

9494
If you prefer to edit project.properties directly, add:
9595

96-
{{codebox content="/assets/files/app-notes/AN019/firmware/AN019.dep" height="120"}}
96+
{{> codebox content="/assets/files/app-notes/AN019/firmware/AN019.dep" height="120"}}
9797

9898

9999
### The Source
100100

101101
This is the modified main.cpp to implement thermocouple support:
102102

103-
{{codebox content="/assets/files/app-notes/AN019/firmware/main.cpp" format="cpp" height="500"}}
103+
{{> codebox content="/assets/files/app-notes/AN019/firmware/main.cpp" format="cpp" height="500"}}
104104

105105
### Digging In
106106

src/content/datasheets/app-notes/an020-tracker-4-20ma.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ Make sure you've used the [**Mark As Development Device**](https://docs.particle
149149
From the command palette in Workbench, **Particle: Install Library** then enter **Sensor_4_20mA_RK**.
150150
If you prefer to edit project.properties directly, add this:
151151

152-
{{codebox content="/assets/files/app-notes/AN020/firmware/AN020.dep" height="100"}}
152+
{{> codebox content="/assets/files/app-notes/AN020/firmware/AN020.dep" height="100"}}
153153

154154
### The full source
155155

156-
{{codebox content="/assets/files/app-notes/AN020/firmware/main.cpp" format="cpp" height="500"}}
156+
{{> codebox content="/assets/files/app-notes/AN020/firmware/main.cpp" format="cpp" height="500"}}
157157

158158
## Digging In
159159

0 commit comments

Comments
 (0)