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
|
|
|
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
|
2017-02-09 15:42:55 +00:00
|
|
|
var oldTiddler = this.getTiddler(fromTitle),
|
|
|
|
newTiddler = new $tw.Tiddler(oldTiddler,{title: toTitle},this.getModificationFields());
|
|
|
|
newTiddler = $tw.hooks.invokeHook("th-renaming-tiddler",newTiddler,oldTiddler);
|
|
|
|
this.addTiddler(newTiddler);
|
2015-08-04 16:44:07 +00:00
|
|
|
this.deleteTiddler(fromTitle);
|
|
|
|
// Rename any tags or lists that reference it
|
2017-02-08 20:04:36 +00:00
|
|
|
this.relinkTiddler(fromTitle,toTitle,options)
|
2016-12-15 17:12:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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") {
|
2019-07-30 16:10:53 +00:00
|
|
|
var tags = tiddler.fields.tags ? tiddler.fields.tags.slice(0) : undefined,
|
|
|
|
list = tiddler.fields.list ? tiddler.fields.list.slice(0) : undefined,
|
2022-01-14 16:57:30 +00:00
|
|
|
isModified = false,
|
|
|
|
processList = function(listField) {
|
|
|
|
if(listField && listField.indexOf(fromTitle) !== -1) {
|
|
|
|
// Remove any existing instances of the toTitle
|
|
|
|
var p = listField.indexOf(toTitle);
|
|
|
|
while(p !== -1) {
|
|
|
|
listField.splice(p,1);
|
|
|
|
p = listField.indexOf(toTitle);
|
|
|
|
}
|
|
|
|
// Replace the fromTitle with toTitle
|
|
|
|
$tw.utils.each(listField,function (title,index) {
|
|
|
|
if(title === fromTitle) {
|
|
|
|
listField[index] = toTitle;
|
|
|
|
isModified = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-12-15 17:12:57 +00:00
|
|
|
if(!options.dontRenameInTags) {
|
|
|
|
// Rename tags
|
2022-01-14 16:57:30 +00:00
|
|
|
processList(tags);
|
2015-08-04 16:44:07 +00:00
|
|
|
}
|
2016-12-15 17:12:57 +00:00
|
|
|
if(!options.dontRenameInLists) {
|
|
|
|
// Rename lists
|
2022-01-14 16:57:30 +00:00
|
|
|
processList(list);
|
2016-12-15 17:12:57 +00:00
|
|
|
}
|
|
|
|
if(isModified) {
|
2017-02-09 15:42:55 +00:00
|
|
|
var newTiddler = new $tw.Tiddler(tiddler,{tags: tags, list: list},self.getModificationFields())
|
|
|
|
newTiddler = $tw.hooks.invokeHook("th-relinking-tiddler",newTiddler,tiddler);
|
|
|
|
self.addTiddler(newTiddler);
|
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
|
|
|
|
|
|
|
})();
|