Extension pattern

This commit is contained in:
benweet 2013-05-26 23:59:17 +01:00
parent a4c46085e4
commit 1f0fea1428
5 changed files with 305 additions and 0 deletions

View File

@ -0,0 +1,65 @@
define( [ "jquery", "underscore" ], function($) {
var documentSelector = {
extensionId: "documentSelector",
extensionName: "Document selector",
settingsBloc: [
'<p>Builds the "Open document" dropdown menu.</p>'
].join("")
};
var fileSystemDescriptor = undefined;
documentSelector.onFileSystemLoaded = function(fileSystemDescriptorParameter) {
fileSystemDescriptor = fileSystemDescriptorParameter;
};
var fileDesc = undefined;
var updateSelector = function() {
var sortedDescriptor = _.sortBy(fileSystemDescriptor, function(fileDesc) {
return fileDesc.title.toLowerCase();
});
function composeTitle(fileDesc) {
var result = [];
var syncAttributesList = _.values(fileDesc.syncLocations);
var publishAttributesList = _.values(fileDesc.publishLocations);
var attributesList = syncAttributesList.concat(publishAttributesList);
_.chain(attributesList).sortBy(function(attributes) {
return attributes.provider;
}).each(function(attributes) {
result.push('<i class="icon-' + attributes.provider + '"></i>');
});
result.push(" ");
result.push(fileDesc.title);
return result.join("");
}
$("#file-selector li:not(.stick)").empty();
_.each(sortedDescriptor, function(fileDescToPrint) {
var a = $("<a>").html(composeTitle(fileDescToPrint.index));
var li = $("<li>").append(a);
if (fileDescToPrint === fileDesc) {
li.addClass("disabled");
} else {
a.prop("href", "#").click(function() {
fileManager.selectFile(fileDescToPrint);
});
}
$("#file-selector").append(li);
});
};
documentSelector.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
updateSelector();
};
documentSelector.onTitleChanged = updateSelector;
documentSelector.onSyncExportSuccess = updateSelector;
documentSelector.onSyncRemoved = updateSelector;
documentSelector.onNewPublishSuccess = updateSelector;
documentSelector.onPublishRemoved = updateSelector;
return documentSelector;
});

View File

@ -0,0 +1,62 @@
define( [ "jquery", "underscore" ], function($) {
var documentTitle = {
extensionId: "documentTitle",
extensionName: "Document title",
settingsBloc: [
'<p>Responsible for showing the document title in the navigation bar.</p>'
].join("")
};
var layout = undefined;
documentTitle.onLayoutCreated = function(layoutParameter) {
layout = layoutParameter;
};
var fileDesc = undefined;
var updateTitle = function(fileDescParameter) {
if(fileDescParameter !== undefined && fileDescParameter !== fileDesc) {
return;
}
function composeTitle(fileDesc) {
var result = [];
var syncAttributesList = _.values(fileDesc.syncLocations);
var publishAttributesList = _.values(fileDesc.publishLocations);
var attributesList = syncAttributesList.concat(publishAttributesList);
_.chain(attributesList).sortBy(function(attributes) {
return attributes.provider;
}).each(function(attributes) {
result.push('<i class="icon-' + attributes.provider + '"></i>');
});
result.push(" ");
result.push(fileDesc.title);
return result.join("");
}
var title = fileDesc.title;
document.title = "StackEdit - " + title;
$("#file-title").html(composeTitle(fileDesc));
$(".file-title").text(title);
$("#file-title-input").val(title);
if(layout !== undefined) {
// Use defer to make sure UI has been updated
_.defer(layout.resizeAll);
}
};
documentTitle.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
updateTitle(fileDescParameter);
};
documentTitle.onTitleChanged = updateTitle;
documentTitle.onSyncExportSuccess = updateTitle;
documentTitle.onSyncRemoved = updateTitle;
documentTitle.onNewPublishSuccess = updateTitle;
documentTitle.onPublishRemoved = updateTitle;
return documentTitle;
});

View File

@ -0,0 +1,65 @@
define( [ "jquery", "underscore" ], function($) {
var managePublication = {
extensionId: "managePublication",
extensionName: "Manage Publication",
settingsBloc: [
'<p>Populates the "Manage publication" dialog box.</p>'
].join("")
};
var fileManager = undefined;
manageSynchronization.onFileManagerCreated = function(fileManagerParameter) {
fileManager = fileManagerParameter;
};
var fileDesc = undefined;
var lineTemplate = [
'<div class="input-prepend input-append">',
'<span class="add-on" title="<%= provider.providerName %>">',
'<i class="icon-<%= provider.providerId %>"></i>',
'</span>',
'<input class="span5" type="text" value="<%= publishDesc %>" disabled />',
'</div>'].join("");
var removeButtonTemplate = '<a class="btn" title="Remove this location"><i class="icon-trash"></i></a>';
var refreshDialog = function(fileDescParameter) {
if(fileDescParameter !== undefined && fileDescParameter !== fileDesc) {
return;
}
var publishAttributesList = _.values(fileDesc.publishLocations);
$(".msg-no-publish, .msg-publish-list").addClass("hide");
var publishList = $("#manage-publish-list").empty();
if (publishAttributesList.length > 0) {
$(".msg-publish-list").removeClass("hide");
} else {
$(".msg-no-publish").removeClass("hide");
}
_.each(publishAttributesList, function(publishAttributes) {
publishAttributes = _.extend({}, publishAttributes);
if(publishAttributes.password) {
publishAttributes.password = "********";
}
var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, "");
var lineElement = $(_.template(lineTemplate, {
provider: providerMap[publishAttributes.provider],
publishDesc: publishDesc
}));
lineElement.append($(removeButtonTemplate).click(function() {
fileManager.removePublish(publishIndex);
}));
publishList.append(lineElement);
});
};
managePublication.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
refreshDialog(fileDescParameter);
};
managePublication.onNewPublishSuccess = refreshDialog;
managePublication.onPublishRemoved = refreshDialog;
return managePublication;
});

View File

@ -0,0 +1,61 @@
define( [ "jquery", "underscore" ], function($) {
var manageSynchronization = {
extensionId: "manageSynchronization",
extensionName: "Manage Synchronization",
settingsBloc: [
'<p>Populates the "Manage synchronization" dialog box.</p>'
].join("")
};
var fileManager = undefined;
manageSynchronization.onFileManagerCreated = function(fileManagerParameter) {
fileManager = fileManagerParameter;
};
var fileDesc = undefined;
var lineTemplate = [
'<div class="input-prepend input-append">',
'<span class="add-on" title="<%= provider.providerName %>">',
'<i class="icon-<%= provider.providerId %>"></i>',
'</span>',
'<input class="span5" type="text" value="<%= syncDesc %>" disabled />',
'</div>'].join("");
var removeButtonTemplate = '<a class="btn" title="Remove this location"><i class="icon-trash"></i></a>';
var refreshDialog = function(fileDescParameter) {
if(fileDescParameter !== undefined && fileDescParameter !== fileDesc) {
return;
}
var syncAttributesList = _.values(fileDesc.syncLocations);
$(".msg-no-sync, .msg-sync-list").addClass("hide");
var syncList = $("#manage-sync-list").empty();
if (syncAttributesList.length > 0) {
$(".msg-sync-list").removeClass("hide");
} else {
$(".msg-no-sync").removeClass("hide");
}
_.each(syncAttributesList, function(syncAttributes) {
var syncDesc = syncAttributes.id || syncAttributes.path;
var lineElement = $(_.template(lineTemplate, {
provider: providerMap[syncAttributes.provider],
syncDesc: syncDesc
}));
lineElement.append($(removeButtonTemplate).click(function() {
fileManager.removeSync(syncAttributes);
}));
syncList.append(lineElement);
});
};
manageSynchronization.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
refreshDialog(fileDescParameter);
};
manageSynchronization.onSyncExportSuccess = refreshDialog;
manageSynchronization.onSyncRemoved = refreshDialog;
return manageSynchronization;
});

View File

@ -0,0 +1,52 @@
define( [ "jquery", "underscore" ], function($) {
var sharingButton = {
extensionId: "sharingButton",
extensionName: "Sharing button",
optional: true,
settingsBloc: [
'<p>Adds a "Share document" button in the navigation bar.</p>'
].join("")
};
var fileDesc = undefined;
var lineTemplate = [
'<div class="input-prepend">',
'<a href="<%= link %>" class="add-on" title="Sharing location"><i class="icon-link"></i></a>',
'<input class="span2" type="text" value="<%= link %>" readonly />',
'</div>'
].join("");
var refreshDocumentSharing = function(fileDescParameter) {
if(fileDescParameter !== undefined && fileDescParameter !== fileDesc) {
return;
}
var linkList = $("#link-container .link-list").empty();
$("#link-container .no-link").show();
var attributesList = _.values(fileDesc.publishLocations);
_.each(attributesList, function(attributes) {
if(attributes.sharingLink) {
var lineElement = $(_.template(lineTemplate, {
link: attributes.sharingLink
}));
lineElement.click(function(event) {
event.stopPropagation();
});
linkList.append(lineElement);
$("#link-container .no-link").hide();
}
});
};
sharingButton.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
refreshDocumentSharing(fileDescParameter);
};
sharingButton.onNewPublishSuccess = refreshDocumentSharing;
sharingButton.onPublishRemoved = refreshDocumentSharing;
return sharingButton;
});