Stackedit/public/res/extensions/buttonStat.js

86 lines
3.0 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",
2014-05-27 13:33:51 +00:00
"text!html/buttonStatSettingsBlock.html"
2013-06-22 23:48:57 +00:00
], 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",
2014-05-27 13:33:51 +00:00
value3: "\\S.*"
};
var eventMgr;
buttonStat.onEventMgrCreated = function(eventMgrParam) {
eventMgr = eventMgrParam;
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-11-07 23:10:38 +00:00
var previewContentsElt;
var valueElt, value1Elt, value2Elt, value3Elt;
2013-07-28 17:14:42 +00:00
buttonStat.onReady = function() {
previewContentsElt = document.getElementById('preview-contents');
valueElt = document.querySelector('.stat-button .value');
value1Elt = document.querySelector('.stat-button-dropdown .value1');
value2Elt = document.querySelector('.stat-button-dropdown .value2');
value3Elt = document.querySelector('.stat-button-dropdown .value3');
};
var regex1, regex2, regex3;
buttonStat.onInit = function() {
regex1 = new RegExp(buttonStat.config.value1, "g");
regex2 = new RegExp(buttonStat.config.value2, "g");
regex3 = new RegExp(buttonStat.config.value3, "g");
2013-07-28 17:14:42 +00:00
};
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;
valueElt.textContent = value1Elt.textContent = (text.match(regex1) || []).length;
value2Elt.textContent = (text.match(regex2) || []).length;
value3Elt.textContent = (text.match(regex3) || []).length;
2014-05-27 13:33:51 +00:00
eventMgr.onExtensionButtonResize();
2013-06-10 21:22:32 +00:00
};
return buttonStat;
});