2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/tiddler.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: tiddlermethod
|
|
|
|
|
2013-04-30 21:37:02 +00:00
|
|
|
Extension methods for the $tw.Tiddler object (constructor and methods required at boot time are in boot/boot.js)
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.hasTag = function(tag) {
|
|
|
|
return this.fields.tags && this.fields.tags.indexOf(tag) !== -1;
|
|
|
|
};
|
|
|
|
|
2013-04-30 21:37:02 +00:00
|
|
|
exports.isPlugin = function() {
|
2013-08-23 17:37:54 +00:00
|
|
|
return this.fields.type === "application/json" && this.hasField("plugin-type");
|
2014-06-23 07:09:59 +00:00
|
|
|
};
|
2013-04-29 11:05:58 +00:00
|
|
|
|
2014-04-28 14:16:31 +00:00
|
|
|
exports.isDraft = function() {
|
|
|
|
return this.hasField("draft.of");
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.getFieldString = function(field) {
|
|
|
|
var value = this.fields[field];
|
|
|
|
// Check for a missing field
|
2012-11-18 13:14:28 +00:00
|
|
|
if(value === undefined || value === null) {
|
2012-10-21 15:50:37 +00:00
|
|
|
return "";
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-08-03 14:09:48 +00:00
|
|
|
// Parse the field with the associated module (if any)
|
|
|
|
var fieldModule = $tw.Tiddler.fieldModules[field];
|
2013-06-13 08:16:26 +00:00
|
|
|
if(fieldModule && fieldModule.stringify) {
|
2012-08-03 14:09:48 +00:00
|
|
|
return fieldModule.stringify.call(this,value);
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-11 14:12:10 +00:00
|
|
|
/*
|
|
|
|
Get all the fields as a name:value block. Options:
|
|
|
|
exclude: an array of field names to exclude
|
|
|
|
*/
|
|
|
|
exports.getFieldStringBlock = function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var exclude = options.exclude || [];
|
|
|
|
var fields = [];
|
|
|
|
for(var field in this.fields) {
|
|
|
|
if($tw.utils.hop(this.fields,field)) {
|
|
|
|
if(exclude.indexOf(field) === -1) {
|
|
|
|
fields.push(field + ": " + this.getFieldString(field));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields.join("\n");
|
|
|
|
};
|
|
|
|
|
2014-06-23 07:09:59 +00:00
|
|
|
/*
|
|
|
|
Compare two tiddlers for equality
|
|
|
|
tiddler: the tiddler to compare
|
|
|
|
excludeFields: array of field names to exclude from the comparison
|
|
|
|
*/
|
|
|
|
exports.isEqual = function(tiddler,excludeFields) {
|
|
|
|
excludeFields = excludeFields || [];
|
|
|
|
var self = this,
|
|
|
|
differences = []; // Fields that have differences
|
|
|
|
// Add to the differences array
|
|
|
|
function addDifference(fieldName) {
|
|
|
|
// Check for this field being excluded
|
|
|
|
if(excludeFields.indexOf(fieldName) === -1) {
|
|
|
|
// Save the field as a difference
|
|
|
|
$tw.utils.pushTop(differences,fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Returns true if the two values of this field are equal
|
|
|
|
function isFieldValueEqual(fieldName) {
|
|
|
|
var valueA = self.fields[fieldName],
|
|
|
|
valueB = tiddler.fields[fieldName];
|
|
|
|
// Check for identical string values
|
|
|
|
if(typeof(valueA) === "string" && typeof(valueB) === "string" && valueA === valueB) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Check for identical array values
|
|
|
|
if($tw.utils.isArray(valueA) && $tw.utils.isArray(valueB) && $tw.utils.isArrayEqual(valueA,valueB)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Otherwise the fields must be different
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Compare our fields
|
|
|
|
for(var fieldName in this.fields) {
|
|
|
|
if(!isFieldValueEqual(fieldName)) {
|
|
|
|
addDifference(fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There's a difference for every field in the other tiddler that we don't have
|
|
|
|
for(fieldName in tiddler.fields) {
|
|
|
|
if(!(fieldName in this.fields)) {
|
|
|
|
addDifference(fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return whether there were any differences
|
|
|
|
return differences.length === 0;
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
})();
|