Skip to content

Commit

Permalink
presets
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuletpol committed Dec 8, 2024
1 parent 9cc56c9 commit 6054359
Show file tree
Hide file tree
Showing 22 changed files with 44 additions and 92 deletions.
11 changes: 7 additions & 4 deletions amd/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,13 @@ const bindingFactory = function($e) {
return {
getValue: () => {
let ret = '';
const clazz = elem.attr('class') ?? '';
const match = new RegExp(classExpr).exec(clazz);
if (match?.[1] && typeof (match[1]) === "string") {
ret = match[1];
const classes = (elem.attr('class') ?? '').split(' ');
for (const clazz of classes) {
const match = new RegExp(classExpr).exec(clazz);
if (match?.[1] && typeof (match[1]) === "string") {
ret = match[1];
break;
}
}
return performCasting(ret, castTo);
},
Expand Down
5 changes: 2 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ The type `Parameter` consists of these fields
- **vimeoId**: Extracts the vimeo id from an URL.
- **serveGDrive**: It converts the share link of a Google Drive file into an URL that can be download.
- **removeHTML**: Returns only the text content.
- **escapeHTML**: It replaces the symbols &, <, >, ", and ' by the corresponding HTML entities.
- **encodeHTML**: Similar to `escapeHTML` but for all HTML entities.
- **escapeQuotes**: Replaces all " instances by '.
- **encodeHTML**: It replaces the symbols &, <, >, etc. by the corresponding HTML entities.
- **escapeQuotes**: Replaces all " instances into '.

Transform functions can be combined into a pipe, e.g. `trim | toUpperCase`, first trims the spaces and then converts the string to upper case.
4 changes: 2 additions & 2 deletions presets/bs-alert.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"value": false,
"bind": {
"get": "(e) => e.find(\"button.btn-close.close\").length > 0",
"set": "(e, v) => {\n if (v && !e.find(\"button.btn-close.close\")[0]) {\n var btn = '<button type=\"button\" class=\"btn-close close\" data-dismiss=\"alert\" data-bs-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>';\n e.prepend(btn);\n } else {\n e.find(\"button.btn-close.close\").remove();\n }\n}\n"
"set": "(e, v) => {\n if (v && !e.find(\"button.btn-close.close\")[0]) {\n var btn = '<button type=\"button\" class=\"btn-close close\" data-dismiss=\"alert\" data-bs-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>';\n e.prepend(btn);\n e.addClass('alert-dismissible');\n } else {\n e.removeClass('alert-dismissible');\n e.find(\"button.btn-close.close\").remove();\n }\n}\n"
}
}
],
"autocomplete": "severity",
"author": "Josep Mulet <pep.mulet@gmail.com>",
"version": "1.1.0"
"version": "1.2.0"
}
1 change: 0 additions & 1 deletion presets/ib-dedication-time.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"type": "textarea"
}
],
"stars": 2,
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
}
1 change: 0 additions & 1 deletion presets/ib-iframe.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"type": "textarea"
}
],
"stars": 2,
"scope": "^page-mod-(book|page|assign|quiz)-",
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
Expand Down
5 changes: 2 additions & 3 deletions presets/ib-image-background.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
"name": "Text onto image background",
"category": "image",
"instructions": "It allows you to create a header with an image that fills the background of a container.\n",
"template": "<p><br></p> \n<!--ini imagebackground-->\n<div class=\"twh-background-img\" style=\"background-image: url('{{url}}'); padding:10px; min-height:40px; background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; background-position: 50% 50%;\">\n<p>What is the probability of winning at gambling games?</p>\n<p><em>Probability and Statistics</em></p>\n</div>\n<!--fi imagebackground-->\n<p><br></p>\n",
"template": "<p><br></p> \n<!--ini imagebackground-->\n<div class=\"twh-background-img\" style=\"background-image: url('{{url}}'); padding:10px; min-height:40px; background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; background-position: 50% 50%;\">\n<h3><b>What is the probability of winning at gambling games?</b></h3>\n<p><em>Probability and Statistics</em></p>\n</div>\n<!--fi imagebackground-->\n<p><br></p>\n",
"selectors": "div.twh-background-img",
"parameters": [
{
"name": "url",
"value": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Aurora_borealis_over_Eielson_Air_Force_Base%2C_Alaska.jpg/1920px-Aurora_borealis_over_Eielson_Air_Force_Base%2C_Alaska.jpg",
"value": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Snow_Scene_at_Shipka_Pass_1.JPG/1024px-Snow_Scene_at_Shipka_Pass_1.JPG",
"title": "Image URL",
"type": "image",
"tip": "You can also upload an image to your course and link it here.",
"bind": "styleRegex(\"background-image:url\\\\(['\\\"]?([^'\\\")]*)['\\\"]?\\\\)\")\n"
}
],
"stars": 2,
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
}
1 change: 0 additions & 1 deletion presets/ib-quote.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"bind": "hasClass('notranslate')"
}
],
"stars": 1,
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
}
20 changes: 0 additions & 20 deletions presets/ib-subsub-seccio.json

This file was deleted.

32 changes: 0 additions & 32 deletions presets/ib-temps-dedicacio.json

This file was deleted.

6 changes: 3 additions & 3 deletions presets/ib-video-gdrive.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"parameters": [
{
"name": "title",
"value": "Title",
"title": "Title"
"value": "Write a title",
"title": "Descriptive Title",
"type": "textfield"
},
{
"name": "iframe_code",
Expand All @@ -24,7 +25,6 @@
"min": 100
}
],
"stars": 1,
"scope": "^page-mod-(book|page|assign|quizz)-",
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
Expand Down
3 changes: 1 addition & 2 deletions presets/ib-vimeo.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"name": "title",
"value": "Enter a title",
"value": "Write a title",
"title": "Descriptive Title",
"type": "textfield"
},
Expand All @@ -33,7 +33,6 @@
"min": 0
}
],
"stars": 1,
"scope": "^page-mod-(book|page|assign|quizz)-",
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
Expand Down
6 changes: 3 additions & 3 deletions presets/ib-youtube.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
},
{
"name": "title",
"value": "Title",
"title": "Descriptive Title"
"value": "Write a title",
"title": "Descriptive Title",
"type": "textfield"
},
{
"name": "startSeconds",
Expand All @@ -38,7 +39,6 @@
}
],
"scope": "^page-mod-(book|page|assign|quizz)-",
"stars": 3,
"author": "Josep Mulet Pol <pep.mulet@gmail.com>",
"version": "1.1.0"
}
4 changes: 3 additions & 1 deletion repository/bs-alert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ parameters:
if (v && !e.find("button.btn-close.close")[0]) {
var btn = '<button type="button" class="btn-close close" data-dismiss="alert" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
e.prepend(btn);
e.addClass('alert-dismissible');
} else {
e.removeClass('alert-dismissible');
e.find("button.btn-close.close").remove();
}
}
autocomplete: severity
author: Josep Mulet <pep.mulet@gmail.com>
version: 1.1.0
version: 1.2.0
1 change: 0 additions & 1 deletion repository/ib-dedication-time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ parameters:
title: Description
tip: This message will appear as a tooltip
type: textarea
stars: 2
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
1 change: 0 additions & 1 deletion repository/ib-iframe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ parameters:
value: '<b>Sample</b>'
title: HTML Code
type: textarea
stars: 2
scope: ^page-mod-(book|page|assign|quiz)-
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
5 changes: 2 additions & 3 deletions repository/ib-image-background.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ template: |
<p><br></p>
<!--ini imagebackground-->
<div class="twh-background-img" style="background-image: url('{{url}}'); padding:10px; min-height:40px; background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; background-position: 50% 50%;">
<p>What is the probability of winning at gambling games?</p>
<h3><b>What is the probability of winning at gambling games?</b></h3>
<p><em>Probability and Statistics</em></p>
</div>
<!--fi imagebackground-->
<p><br></p>
selectors: div.twh-background-img
parameters:
- name: url
value: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Aurora_borealis_over_Eielson_Air_Force_Base%2C_Alaska.jpg/1920px-Aurora_borealis_over_Eielson_Air_Force_Base%2C_Alaska.jpg
value: https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Snow_Scene_at_Shipka_Pass_1.JPG/1024px-Snow_Scene_at_Shipka_Pass_1.JPG
title: Image URL
type: image
tip: You can also upload an image to your course and link it here.
bind: |
styleRegex("background-image:url\\(['\"]?([^'\")]*)['\"]?\\)")
stars: 2
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
1 change: 0 additions & 1 deletion repository/ib-quote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ parameters:
value: true
title: Prevent translation by Google
bind: hasClass('notranslate')
stars: 1
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
6 changes: 3 additions & 3 deletions repository/ib-video-gdrive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ template: |
<p><br></p>
parameters:
- name: title
value: Title
title: Title
value: Write a title
title: Descriptive Title
type: textfield
- name: iframe_code
value: <iframe src="https://drive.google.com/file/d/1C-xFq_cPF9kr1Xq9XINixAkU46AHAVUE/preview" width="640" height="480" allow="autoplay" fullscreen="1"></iframe>
title: Iframe code
Expand All @@ -28,7 +29,6 @@ parameters:
value: 500
title: Maximum width in px
min: 100
stars: 1
scope: ^page-mod-(book|page|assign|quizz)-
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
3 changes: 1 addition & 2 deletions repository/ib-vimeo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ parameters:
type: textfield
transform: vimeoId
- name: title
value: Enter a title
value: Write a title
title: Descriptive Title
type: textfield
- name: maxWidth
Expand All @@ -37,7 +37,6 @@ parameters:
title: Start Playback at Second
tip: Set to 0 to start from the beginning of the video.
min: 0
stars: 1
scope: ^page-mod-(book|page|assign|quizz)-
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
4 changes: 2 additions & 2 deletions repository/ib-youtube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ parameters:
title: Maximum Width in px
min: 100
- name: title
value: Title
value: Write a title
title: Descriptive Title
type: textfield
- name: startSeconds
value: 0
title: Start Playback at Second
Expand All @@ -40,6 +41,5 @@ parameters:
min: 0
tip: Keep this at 0 if you want the video to play until the end
scope: ^page-mod-(book|page|assign|quizz)-
stars: 3
author: Josep Mulet Pol <pep.mulet@gmail.com>
version: 1.1.0
2 changes: 1 addition & 1 deletion tests/jsunit/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// jest.config.js
module.exports = {
silent: true,
silent: false,
transformIgnorePatterns: ['node_modules/(?!(sucrase)/)'],
transform: {
'^.+\\.(js|jsx|ts|tsx|mjs)$': ['babel-jest',
Expand Down
14 changes: 12 additions & 2 deletions tests/jsunit/specs/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('utils module tests', () => {
["classRegex('locked-([0-9]*)-some(.*)')", `<span class="etc"></span>`, ''],
["classRegex('locked-([0-9]*)-some(.*)')", `<span class="locked--some etc"></span>`, ''],
["classRegex('locked-([0-9]*)-some(.*)')", `<span class="locked-123-somes etc"></span>`, '123'],

["hasAttr('data-locked')", `<span data-locked></span>`, true],
["hasAttr('data-locked')", `<span data-locked="false"></span>`, true],
["hasAttr('data-locked')", `<span data-open="false"></span>`, false],
Expand All @@ -309,7 +309,7 @@ describe('utils module tests', () => {
["attrRegex('role=channel(.*)')", `<span role="channel"></span>`, ''],
["attrRegex('role=locked-([0-9]*)-abc')", `<span role="locked--abc"></span>`, ''],
["attrRegex('role=locked-([0-9]*)-abc')", `<span role="locked-123-abc"></span>`, '123'],

["hasStyle('width')", `<span style="width: 100px;"></span>`, true],
["hasStyle('height')", `<span style="width: 100px;"></span>`, false],
["hasStyle('color:red')", `<span style="color: red;"></span>`, true],
Expand Down Expand Up @@ -341,6 +341,16 @@ describe('utils module tests', () => {
expect(binding?.getValue()).toBe(result);
});

test('Testing class regex', () => {
let [bindDef, elemDef, result] = ["classRegex('alert-(.*)')", `<div class="m-2 alert alert-secondary fade show" role="alert"><div class="alert-content"><p>Lorem ipsum.</p></div></div>`, 'secondary'];
let $e = jQuery(elemDef);
expect($e.length).toBe(1);
// Binding on the same element
let binding = U.createBinding(bindDef, $e);
expect(binding).not.toBeNull();
expect(binding?.getValue()).toBe(result);
});


test.each([
["hasClass('editable')", `<span class="a editable"></span>`, true, `<span class="a editable"></span>`],
Expand Down

0 comments on commit 6054359

Please sign in to comment.