nystudio107 /
craft-richvariables
| 1 | /** |
||
| 2 | * Rich Variables plugin for Craft CMS |
||
| 3 | * |
||
| 4 | * Rich Variables JS |
||
| 5 | * |
||
| 6 | * @author nystudio107 |
||
| 7 | * @copyright Copyright (c) 2017 nystudio107 |
||
| 8 | * @link https://nystudio107.com |
||
| 9 | * @package RichVariables |
||
| 10 | * @since 1.0.18 |
||
| 11 | */ |
||
| 12 | |||
| 13 | (function($R) |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 14 | { |
||
|
0 ignored issues
–
show
|
|||
| 15 | function setWithExpiry(key, value, ttl) { |
||
|
0 ignored issues
–
show
|
|||
| 16 | const now = new Date() |
||
| 17 | // `item` is an object which contains the original value |
||
| 18 | // as well as the time when it's supposed to expire |
||
| 19 | const item = { |
||
| 20 | value: value, |
||
| 21 | expiry: now.getTime() + ttl, |
||
| 22 | } |
||
| 23 | localStorage.setItem(key, JSON.stringify(item)) |
||
|
0 ignored issues
–
show
The variable
localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 24 | } |
||
| 25 | |||
| 26 | function getWithExpiry(key) { |
||
|
0 ignored issues
–
show
|
|||
| 27 | const itemStr = localStorage.getItem(key) |
||
|
0 ignored issues
–
show
The variable
localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 28 | // if the item doesn't exist, return null |
||
| 29 | if (!itemStr) { |
||
| 30 | return null |
||
| 31 | } |
||
| 32 | const item = JSON.parse(itemStr) |
||
| 33 | const now = new Date() |
||
| 34 | // compare the expiry time of the item with the current time |
||
| 35 | if (now.getTime() > item.expiry) { |
||
| 36 | // If the item is expired, delete the item from storage |
||
| 37 | // and return null |
||
| 38 | localStorage.removeItem(key) |
||
| 39 | return null |
||
| 40 | } |
||
| 41 | return item.value |
||
| 42 | } |
||
| 43 | |||
| 44 | $R.add('plugin', 'richvariables', { |
||
|
0 ignored issues
–
show
|
|||
| 45 | translations: { |
||
| 46 | en: { |
||
| 47 | "variables": "Variables" |
||
| 48 | } |
||
| 49 | }, |
||
| 50 | init: function(app) |
||
|
0 ignored issues
–
show
|
|||
| 51 | { |
||
|
0 ignored issues
–
show
|
|||
| 52 | this.app = app; |
||
| 53 | this.lang = app.lang; |
||
| 54 | this.inline = app.inline; |
||
| 55 | this.toolbar = app.toolbar; |
||
| 56 | this.insertion = app.insertion; |
||
| 57 | |||
| 58 | // Try to grab the menu from our local storage cache if possible |
||
| 59 | var cachedResponseVars = getWithExpiry('rich-variables-menu-cache'); |
||
| 60 | if (cachedResponseVars === null) { |
||
| 61 | // Grab the globals set Reference Tags from our controller |
||
| 62 | var request = new XMLHttpRequest(); |
||
| 63 | request.open('GET', Craft.getActionUrl('rich-variables'), false); |
||
|
0 ignored issues
–
show
The variable
Craft seems to be never declared. If this is a global, consider adding a /** global: Craft */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 64 | request.onload = function() { |
||
|
0 ignored issues
–
show
|
|||
| 65 | if (request.status >= 200 && request.status < 400) { |
||
|
0 ignored issues
–
show
Comprehensibility
Documentation
Best Practice
introduced
by
|
|||
| 66 | } else { |
||
|
0 ignored issues
–
show
Comprehensibility
Documentation
Best Practice
introduced
by
|
|||
| 67 | } |
||
| 68 | }; |
||
| 69 | request.send(); |
||
| 70 | this.request = request; |
||
| 71 | } |
||
| 72 | }, |
||
| 73 | start: function() |
||
|
0 ignored issues
–
show
|
|||
| 74 | { |
||
|
0 ignored issues
–
show
|
|||
| 75 | var dropdown = {}; |
||
| 76 | // Try to grab the menu from our local storage cache if possible |
||
| 77 | var responseVars = getWithExpiry('rich-variables-menu-cache'); |
||
| 78 | if (responseVars === null && this.request) { |
||
| 79 | responseVars = JSON.parse(this.request.responseText); |
||
| 80 | setWithExpiry('rich-variables-menu-cache', responseVars, 60 * 1000) |
||
| 81 | } |
||
| 82 | // Iterate through each menu item, adding them to our dropdown |
||
| 83 | responseVars.variablesList.forEach(function(menuItem, index) { |
||
|
0 ignored issues
–
show
|
|||
| 84 | var key = 'point' + (index + 1); |
||
| 85 | var refTag = '<ins>' + menuItem.text + '</ins>'; |
||
| 86 | dropdown[key] = { |
||
| 87 | title: menuItem.title, |
||
| 88 | api: 'plugin.richvariables.insert', |
||
| 89 | args: refTag |
||
| 90 | }; |
||
| 91 | }); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 92 | // Handle empty menu items |
||
| 93 | if (responseVars.variablesList.length === 0) { |
||
| 94 | dropdown.point1 = { |
||
| 95 | title: "No Globals Found", |
||
| 96 | func: function(buttonName) { |
||
|
0 ignored issues
–
show
|
|||
| 97 | // NOP |
||
| 98 | }, |
||
| 99 | }; |
||
| 100 | } |
||
| 101 | // Add the button and dropdown |
||
| 102 | var $button = this.toolbar.addButton('variables', { title: this.lang.get('variables') }); |
||
| 103 | $button.setDropdown(dropdown); |
||
| 104 | if (responseVars.useIconForMenu) { |
||
| 105 | $button.setIcon('<img src="' + responseVars.menuIconUrl + '" height="16" width="16" style="margin-top: -2px;">'); |
||
| 106 | } |
||
| 107 | }, |
||
| 108 | insert: function(refTag) |
||
|
0 ignored issues
–
show
|
|||
| 109 | { |
||
|
0 ignored issues
–
show
|
|||
| 110 | this.insertion.insertRaw(refTag); |
||
| 111 | } |
||
| 112 | }); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 113 | })(Redactor); |
||
|
0 ignored issues
–
show
The variable
Redactor seems to be never declared. If this is a global, consider adding a /** global: Redactor */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. Loading history...
|
|||
| 114 |