2015-08-04 16:44:07 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/wiki-bulkops.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: wikimethod
|
|
|
|
|
|
|
|
Bulk tiddler operations such as rename.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Rename a tiddler, and relink any tags or lists that reference it.
|
|
|
|
*/
|
2016-12-15 17:12:57 +00:00
|
|
|
function renameTiddler(fromTitle,toTitle,options) {
|
2015-08-04 16:44:07 +00:00
|
|
|
var self = this;
|
|
|
|
fromTitle = (fromTitle || "").trim();
|
|
|
|
toTitle = (toTitle || "").trim();
|
2016-12-15 17:12:57 +00:00
|
|
|
options = options || {};
|
2015-08-04 16:44:07 +00:00
|
|
|
if(fromTitle && toTitle && fromTitle !== toTitle) {
|
|
|
|
// Rename the tiddler itself
|
|
|
|
var tiddler = this.getTiddler(fromTitle);
|
|
|
|
this.addTiddler(new $tw.Tiddler(tiddler,{title: toTitle},this.getModificationFields()));
|
|
|
|
this.deleteTiddler(fromTitle);
|
|
|
|
// Rename any tags or lists that reference it
|
2016-12-15 17:12:57 +00:00
|
|
|
relinkTiddler(fromTitle,toTitle,options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Relink any tags or lists that reference a given tiddler
|
|
|
|
*/
|
|
|
|
function relinkTiddler(fromTitle,toTitle,options) {
|
|
|
|
var self = this;
|
|
|
|
fromTitle = (fromTitle || "").trim();
|
|
|
|
toTitle = (toTitle || "").trim();
|
|
|
|
options = options || {};
|
|
|
|
if(fromTitle && toTitle && fromTitle !== toTitle) {
|
2015-08-04 16:44:07 +00:00
|
|
|
this.each(function(tiddler,title) {
|
2016-12-15 17:12:57 +00:00
|
|
|
var type = tiddler.fields.type || "";
|
|
|
|
// Don't touch plugins or JavaScript modules
|
|
|
|
if(!tiddler.fields["plugin-type"] && type !== "application/javascript") {
|
|
|
|
var tags = (tiddler.fields.tags || []).slice(0),
|
|
|
|
list = (tiddler.fields.list || []).slice(0),
|
|
|
|
isModified = false;
|
|
|
|
if(!options.dontRenameInTags) {
|
|
|
|
// Rename tags
|
|
|
|
$tw.utils.each(tags,function (title,index) {
|
|
|
|
if(title === fromTitle) {
|
|
|
|
console.log("Renaming tag '" + tags[index] + "' to '" + toTitle + "' of tiddler '" + tiddler.fields.title + "'");
|
|
|
|
tags[index] = toTitle;
|
|
|
|
isModified = true;
|
|
|
|
}
|
|
|
|
});
|
2015-08-04 16:44:07 +00:00
|
|
|
}
|
2016-12-15 17:12:57 +00:00
|
|
|
if(!options.dontRenameInLists) {
|
|
|
|
// Rename lists
|
|
|
|
$tw.utils.each(list,function (title,index) {
|
|
|
|
if(title === fromTitle) {
|
|
|
|
console.log("Renaming list item '" + list[index] + "' to '" + toTitle + "' of tiddler '" + tiddler.fields.title + "'");
|
|
|
|
list[index] = toTitle;
|
|
|
|
isModified = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(isModified) {
|
|
|
|
self.addTiddler(new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields()));
|
2015-08-04 16:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-12-15 17:12:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.renameTiddler = renameTiddler;
|
|
|
|
exports.relinkTiddler = relinkTiddler;
|
2015-08-04 16:44:07 +00:00
|
|
|
|
|
|
|
})();
|