Stackedit/js/extensions/buttonStat.js

56 lines
1.9 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-06-22 23:48:57 +00:00
var buttonStat = new Extension("buttonStat", 'Button "Statistics"', true);
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-06-10 21:22:32 +00:00
return $(_.template(buttonStatHTML, buttonStat.config));
};
buttonStat.onPreviewFinished = function() {
var text = $("#wmd-preview").clone().find("script").remove().end().text();
$("#span-stat-value1").text((text.match(new RegExp(buttonStat.config.value1, "g")) || []).length);
$("#span-stat-value2").text((text.match(new RegExp(buttonStat.config.value2, "g")) || []).length);
$("#span-stat-value3").text((text.match(new RegExp(buttonStat.config.value3, "g")) || []).length);
};
return buttonStat;
});