|
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) |
|
|
|
|
|
|
14
|
|
|
{ |
|
|
|
|
|
|
15
|
|
|
function setWithExpiry(key, value, ttl) { |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
function getWithExpiry(key) { |
|
|
|
|
|
|
27
|
|
|
const itemStr = localStorage.getItem(key) |
|
|
|
|
|
|
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', { |
|
|
|
|
|
|
45
|
|
|
translations: { |
|
46
|
|
|
en: { |
|
47
|
|
|
"variables": "Variables" |
|
48
|
|
|
} |
|
49
|
|
|
}, |
|
50
|
|
|
init: function(app) |
|
|
|
|
|
|
51
|
|
|
{ |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
64
|
|
|
request.onload = function() { |
|
|
|
|
|
|
65
|
|
|
if (request.status >= 200 && request.status < 400) { |
|
|
|
|
|
|
66
|
|
|
} else { |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
}; |
|
69
|
|
|
request.send(); |
|
70
|
|
|
this.request = request; |
|
71
|
|
|
} |
|
72
|
|
|
}, |
|
73
|
|
|
start: function() |
|
|
|
|
|
|
74
|
|
|
{ |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
}); |
|
|
|
|
|
|
92
|
|
|
// Handle empty menu items |
|
93
|
|
|
if (responseVars.variablesList.length === 0) { |
|
94
|
|
|
dropdown.point1 = { |
|
95
|
|
|
title: "No Globals Found", |
|
96
|
|
|
func: function(buttonName) { |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
109
|
|
|
{ |
|
|
|
|
|
|
110
|
|
|
this.insertion.insertRaw(refTag); |
|
111
|
|
|
} |
|
112
|
|
|
}); |
|
|
|
|
|
|
113
|
|
|
})(Redactor); |
|
|
|
|
|
|
114
|
|
|
|