-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
129 changed files
with
8,390 additions
and
5,971 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,21 +8,29 @@ var $osf = require('js/osfHelpers'); | |
|
||
var MESSAGE_TIMEOUT = 5000; | ||
|
||
var DEFAULT_FORWARD_BOOL = true; | ||
|
||
/** | ||
* Knockout view model for the Forward node settings widget. | ||
*/ | ||
var ViewModel = function(url, nodeId) { | ||
var ViewModel = function(node, enabled, url, label) { | ||
|
||
var self = this; | ||
|
||
// Forward configuration | ||
self.url = ko.observable().extend({ | ||
var forwardUrl = $osf.apiV2Url('nodes/' + node.id + '/addons/forward/'); | ||
|
||
self.enabled = ko.observable(enabled); | ||
self.label = koHelpers.sanitizedObservable(label); | ||
self.url = ko.observable(url).extend({ | ||
ensureHttp: true, | ||
url: true, | ||
required: true | ||
}); | ||
|
||
// Flashed messages | ||
self.message = ko.observable(''); | ||
self.messageClass = ko.observable('text-info'); | ||
|
||
self.pendingRequest = ko.observable(false); | ||
|
||
ko.validation.addAnonymousRule( | ||
self.url, | ||
koHelpers.makeRegexValidator( | ||
|
@@ -31,50 +39,54 @@ var ViewModel = function(url, nodeId) { | |
false | ||
) | ||
); | ||
self.label = koHelpers.sanitizedObservable(); | ||
|
||
// Flashed messages | ||
self.message = ko.observable(''); | ||
self.messageClass = ko.observable('text-info'); | ||
|
||
self.validators = ko.validatedObservable({ | ||
url: self.url, | ||
}); | ||
|
||
|
||
/** | ||
* Update the view model from data returned from the server. | ||
*/ | ||
self.updateFromData = function(data) { | ||
self.url(data.url); | ||
self.label(data.label); | ||
}; | ||
|
||
self.fetchFromServer = function() { | ||
$.ajax({ | ||
type: 'GET', | ||
url: url, | ||
dataType: 'json' | ||
}).done(function(response) { | ||
self.updateFromData(response); | ||
}).fail(function(xhr, textStatus, error) { | ||
self.changeMessage('Could not retrieve Forward settings at ' + | ||
'this time. Please refresh ' + | ||
'the page. If the problem persists, email ' + | ||
'<a href="mailto:[email protected]">[email protected]</a>.', | ||
'text-danger'); | ||
Raven.captureMessage('Could not GET get Forward addon settings.', { | ||
extra: { | ||
url: url, | ||
textStatus: textStatus, | ||
error: error | ||
self.enabled.subscribe(function(newValue) { | ||
self.pendingRequest(true); | ||
if (!newValue) { | ||
$osf.ajaxJSON( | ||
'delete', | ||
forwardUrl, | ||
{'isCors': true} | ||
).done(function(response) { | ||
self.pendingRequest(false); | ||
}).fail(function(xhr, status, error) { | ||
$osf.growl('Error', 'Unable to disable redirect link.'); | ||
Raven.captureMessage('Error disabling redirect link.', { | ||
extra: { | ||
url: forwardUrl, status: status, error: error | ||
} | ||
}); | ||
}); | ||
} else { | ||
$osf.ajaxJSON( | ||
'post', | ||
forwardUrl, | ||
{ | ||
'data': { | ||
'data': { | ||
'id': 'forward', | ||
'type': 'node_addons', | ||
'attributes': {} | ||
} | ||
}, | ||
'isCors': true | ||
} | ||
).done(function(response) { | ||
self.pendingRequest(false); | ||
}).fail(function(xhr, status, error) { | ||
$osf.growl('Error', 'Unable to enable redirect link.'); | ||
Raven.captureMessage('Error enabling redirect link.', { | ||
extra: { | ||
url: forwardUrl, status: status, error: error | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
// Initial fetch from server | ||
self.fetchFromServer(); | ||
} | ||
}); | ||
|
||
function onSubmitSuccess() { | ||
if (self.url() == null) { | ||
|
@@ -103,24 +115,45 @@ var ViewModel = function(url, nodeId) { | |
} | ||
else { | ||
self.changeMessage( | ||
'Could not change settings. Please try again later.', | ||
'Could not change redirect link settings. Please try again later.', | ||
'text-danger' | ||
); | ||
Raven.captureMessage('Error updating redirect link.', { | ||
extra: { | ||
url: forwardUrl, status: status, error: error | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Submit new settings. | ||
*/ | ||
self.submitSettings = function() { | ||
$osf.putJSON( | ||
url, | ||
ko.toJS(self) | ||
).done( | ||
onSubmitSuccess | ||
).fail( | ||
onSubmitError | ||
); | ||
self.pendingRequest(true); | ||
$osf.ajaxJSON( | ||
'put', | ||
forwardUrl, | ||
{ | ||
'data': { | ||
'data': { | ||
'id': 'forward', | ||
'type': 'node_addons', | ||
'attributes': { | ||
'url': self.url(), | ||
'label': self.label() | ||
} | ||
} | ||
}, | ||
'isCors': true | ||
} | ||
).done(function(response) { | ||
onSubmitSuccess() | ||
self.pendingRequest(false); | ||
}).fail(function(response) { | ||
onSubmitError() | ||
self.pendingRequest(false); | ||
}); | ||
}; | ||
|
||
/** Change the flashed message. */ | ||
|
@@ -140,9 +173,9 @@ var ViewModel = function(url, nodeId) { | |
}; | ||
|
||
// Public API | ||
function ForwardConfig(selector, url, nodeId) { | ||
function ForwardConfig(selector, node, enabled, url, label) { | ||
var self = this; | ||
self.viewModel = new ViewModel(url, nodeId); | ||
self.viewModel = new ViewModel(node, enabled, url, label); | ||
$osf.applyBindings(self.viewModel, selector); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
var $osf = require('js/osfHelpers'); | ||
var ForwardConfig = require('./forwardConfig.js'); | ||
|
||
var url = window.contextVars.node.urls.api + 'forward/config/'; | ||
// #forwardScope will only be in the DOM if the addon is properly configured | ||
if ($('#forwardScope')[0]) { | ||
new ForwardConfig('#forwardScope', url, window.contextVars.node.id); | ||
} | ||
var ctx = window.contextVars; | ||
|
||
var enabled = false; | ||
var url = ''; | ||
var label = ''; | ||
|
||
$(document).ready(function() { | ||
$osf.ajaxJSON( | ||
'GET', | ||
$osf.apiV2Url('nodes/' + ctx.node.id + '/addons/'), | ||
{'isCors': true} | ||
).done(function(response){ | ||
response.data.forEach(function(addon) { | ||
if (addon.id === 'forward') { | ||
enabled = true; | ||
url = addon.attributes.url; | ||
label = addon.attributes.label; | ||
} | ||
}); | ||
new ForwardConfig('#configureForward', ctx.node, enabled, url, label); | ||
}).fail(function(response){ | ||
$osf.growl('Error:', 'Unable to retrieve settings.'); | ||
Raven.captureMessage('Error occurred retrieving node addons'); | ||
}); | ||
|
||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.