Stackedit/public/res/extensions/buttonStat.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

2013-06-10 21:22:32 +00:00
define([
"jquery",
"underscore",
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-10 21:22:32 +00:00
"text!html/buttonStat.html",
2013-06-22 23:48:57 +00:00
"text!html/buttonStatSettingsBlock.html",
], function($, _, utils, Extension, buttonStatHTML, buttonStatSettingsBlockHTML) {
2013-06-10 21:22:32 +00:00
2013-07-29 00:03:19 +00:00
var buttonStat = new Extension("buttonStat", 'Button "Statistics"', true, true);
2013-06-22 23:48:57 +00:00
buttonStat.settingsBlock = buttonStatSettingsBlockHTML;
buttonStat.defaultConfig = {
name1: "Characters",
value1: "\\S",
name2: "Words",
value2: "\\S+",
name3: "Paragraphs",
value3: "\\S.*",
2013-06-10 21:22:32 +00:00
};
buttonStat.onLoadSettings = function() {
_.each([
1,
2,
3
], function(index) {
utils.setInputValue("#input-stat-name" + index, buttonStat.config["name" + index]);
utils.setInputValue("#input-stat-value" + index, buttonStat.config["value" + index]);
});
};
buttonStat.onSaveSettings = function(newConfig, event) {
_.each([
1,
2,
3
], function(index) {
newConfig["name" + index] = utils.getInputTextValue("#input-stat-name" + index, event);
newConfig["value" + index] = utils.getInputRegExpValue("#input-stat-value" + index, event);
});
};
2013-06-19 20:33:46 +00:00
buttonStat.onCreatePreviewButton = function() {
2013-07-30 08:46:36 +00:00
return _.template(buttonStatHTML, buttonStat.config);
2013-06-10 21:22:32 +00:00
};
2013-07-28 17:14:42 +00:00
2013-11-07 23:10:38 +00:00
var previewContentsElt;
var value1Elt;
var value2Elt;
var value3Elt;
2013-07-28 17:14:42 +00:00
buttonStat.onReady = function() {
previewContentsElt = document.getElementById('preview-contents');
value1Elt = document.getElementById('span-stat-value1');
value2Elt = document.getElementById('span-stat-value2');
value3Elt = document.getElementById('span-stat-value3');
};
2013-06-10 21:22:32 +00:00
buttonStat.onPreviewFinished = function() {
2013-07-28 17:14:42 +00:00
var previewContentsEltClone = previewContentsElt.cloneNode(true);
var scriptEltList = previewContentsEltClone.getElementsByTagName('script');
for(var i = scriptEltList.length-1; i >= 0; i--) {
var scriptElt = scriptEltList[i];
scriptElt.parentNode.removeChild(scriptElt);
}
var text = previewContentsEltClone.textContent;
value1Elt.textContent = (text.match(new RegExp(buttonStat.config.value1, "g")) || []).length;
value2Elt.textContent = (text.match(new RegExp(buttonStat.config.value2, "g")) || []).length;
value3Elt.textContent = (text.match(new RegExp(buttonStat.config.value3, "g")) || []).length;
2013-06-10 21:22:32 +00:00
};
return buttonStat;
});