Skip to content

Commit 4155c9f

Browse files
tporadowskidregad
authored andcommitted
Make text areas configurable
Names of text areas which should have snippets available can now be configured via `plugin_Snippets_textarea_names` configuration option (default: "bugnote_text"), i.e. "bugnote_text, steps_to_reproduce". It makes including other fields easier than hard-coding each new field into code like proposed in issue #3.
1 parent 0534f50 commit 4155c9f

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ Once selection is made, the Snippet's text will be inserted in the field at the
5959
current position. If text is currently selected, the Snippet will replace the
6060
selection.
6161

62-
Note that currently only the *Bug Note* field is configured to use Snippets.
62+
By default only the *Bug Note* field is configured to use Snippets.
6363
Other text fields (*Description*, *Steps To Reproduce* as well as *Additional
64-
Information*) can be setup to use Snippets with minimal configuration effort
65-
(see [this example](https://github.com/mantisbt-plugins/snippets/issues/3)).
66-
64+
Information*) can be setup to use Snippets via configuration option `plugin_Snippets_textarea_names`
65+
where you can list names of fields you are interested in, i.e. `bugnote_text, steps_to_reproduce, body` (`body`
66+
refers to text area on *Send reminder* page).
6767

6868
## Support
6969

Snippets/Snippets.API.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,22 @@ function xmlhttprequest_plugin_snippets() {
3535
$snippets = Snippet::load_by_type_user(0, $user_id, $use_global);
3636
$snippets = Snippet::clean($snippets, "form", $bug_id);
3737

38+
# split names of textareas found in "plugin_Snippets_textarea_names" option and
39+
# make an array of "textarea[name='FIELD_NAME']" strings
40+
$textareaSelectors = array_map(function($name) {
41+
return "textarea[name='$name']";
42+
}, preg_split("/[,;\s]+/", plugin_config_get("textarea_names", "bugnote_text"))
43+
);
44+
3845
$data = array(
3946
"snippets" => SnippetsPlugin::$_version,
47+
# return configured jQuery selectors for textareas in "selector" field
48+
"selector" => implode(",", $textareaSelectors)
4049
);
4150

42-
# arrange the available snippets into the data array
51+
# arrange the available snippets into the data array and return it in "texts" field
4352
foreach($snippets as $snippet) {
44-
$data["bugnote_text"][$snippet->id] = $snippet;
53+
$data["texts"][$snippet->id] = $snippet;
4554
}
4655

4756
$json = json_encode($data);

Snippets/Snippets.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Licensed under the MIT license
66

77
class SnippetsPlugin extends MantisPlugin {
8-
public static $_version = "0.5";
8+
public static $_version = "0.6";
99

1010
public function register() {
1111
$this->name = plugin_lang_get("name");
@@ -29,6 +29,7 @@ public function config() {
2929
"edit_global_threshold" => ADMINISTRATOR,
3030
"use_global_threshold" => REPORTER,
3131
"edit_own_threshold" => REPORTER,
32+
"textarea_names" => "bugnote_text",
3233
);
3334
}
3435

@@ -89,5 +90,4 @@ public function schema() {
8990
")),
9091
);
9192
}
92-
}
93-
93+
}

Snippets/files/snippets.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ jQuery(document).ready(function($) {
4242
* then insert select boxes into the DOM for each supported textarea.
4343
*/
4444
function SnippetsInit() {
45-
var textareas = $("textarea[name='bugnote_text']");
46-
4745
function SnippetsUI(data) {
4846
var textarrays = data;
4947

50-
textareas.each(function(index) {
48+
$(data.selector).each(function(index) {
5149
var textarea_name = $(this).attr("name");
5250
var textarea = $(this);
5351

5452
try {
5553

56-
snippets = textarrays[textarea_name];
54+
snippets = textarrays["texts"];
5755
if (snippets != null) {
5856
label = $("<label>" + SnippetsLang("label") + " </label>");
5957

@@ -88,7 +86,8 @@ jQuery(document).ready(function($) {
8886
});
8987
}
9088

91-
if (textareas.length > 0) {
89+
//if we have any textareas then fetch snippets
90+
if ($("textarea").length > 0) {
9291
var bug_id = 0;
9392

9493
$("form[name='bugnoteadd'] input[name='bug_id']").each(function() {

0 commit comments

Comments
 (0)