Skip to content

Commit

Permalink
client: clean up form data extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Sep 5, 2020
1 parent 1e593db commit 8ac6434
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
26 changes: 0 additions & 26 deletions assets/js/selfoss-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,32 +179,6 @@ var selfoss = {
},


/**
* returns an array of name value pairs of all form elements in given element
*
* @return void
* @param element containing the form elements
*/
getValues: function(element) {
var values = {};

$(element).find(':input').each(function(i, el) {
// get only input elements with name
if (el.hasAttribute('name')) {
let name = el.getAttribute('name').trim();
if (name.length != 0) {
values[name] = $(el).val();
if ($(el).attr('type') == 'checkbox') {
values[name] = $(el).attr('checked') ? 1 : 0;
}
}
}
});

return values;
},


loggedin: false,


Expand Down
38 changes: 25 additions & 13 deletions assets/js/selfoss-events-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as ajax from './helpers/ajax';
*/
selfoss.events.sources = function() {
// cancel source editing
$('.source-cancel').unbind('click').click(function() {
$('.source-cancel').unbind('click').click(function(event) {
event.preventDefault();

var parent = $(this).parents('.source');
if (parent.hasClass('source-new')) {
parent.fadeOut('fast', function() {
Expand All @@ -18,7 +20,9 @@ selfoss.events.sources = function() {
});

// add new source
$('.source-add').unbind('click').click(function() {
$('.source-add').unbind('click').click(function(event) {
event.preventDefault();

ajax.get('source').promise.then(response => response.text()).then((text) => {
$('.source-opml').after(text);
selfoss.events.sources();
Expand All @@ -29,8 +33,10 @@ selfoss.events.sources = function() {
});

// save source
$('.source-save').unbind('click').click(function() {
var parent = $(this).parents('.source');
$('.source-save').unbind('click').click(function(event) {
event.preventDefault();

var parent = $(this).parents('form.source');

// remove old errors
parent.find('span.error').remove();
Expand All @@ -42,15 +48,18 @@ selfoss.events.sources = function() {
// get id
let id = parent.attr('data-source-id');

// set url
const url = `source/${id}`;

// get values and params
var values = selfoss.getValues(parent);
values['tags'] = values['tags'].split(',');
var values = new FormData(parent.get(0));

// make tags into a list
let oldTags = values.get('tags').split(',');
values.delete('tags');
oldTags.map(tag => tag.trim())
.filter(tag => tag !== '')
.forEach(tag => values.append('tags[]', tag));

ajax.post(url, {
body: ajax.makeSearchParams(values),
ajax.post(`source/${id}`, {
body: new URLSearchParams(values),
failOnHttpErrors: false
}).promise
.then(ajax.rejectUnless(response => response.ok || response.status === 400))
Expand Down Expand Up @@ -95,7 +104,9 @@ selfoss.events.sources = function() {
});

// delete source
$('.source-delete').unbind('click').click(function() {
$('.source-delete').unbind('click').click(function(event) {
event.preventDefault();

var answer = confirm(selfoss.ui._('source_warn'));
if (answer == false) {
return;
Expand Down Expand Up @@ -124,7 +135,8 @@ selfoss.events.sources = function() {
});

// show params
$('.source-showparams').unbind('click').click(function() {
$('.source-showparams').unbind('click').click(function(event) {
event.preventDefault();
$(this).parent().parent().find('.source-edit-form').show();
});

Expand Down
3 changes: 3 additions & 0 deletions src/controllers/Sources/Write.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function write(Base $f3, array $params) {

// clean up title and tag data to prevent XSS
$title = htmlspecialchars($data['title']);
if (!isset($data['tags'])) {
$data['tags'] = [];
}
$tags = array_map('htmlspecialchars', $data['tags']);
$spout = $data['spout'];
$filter = $data['filter'];
Expand Down
10 changes: 5 additions & 5 deletions src/templates/source.phtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php $idAttr = isset($this->source) ? $this->source['id'] : 'new-' . rand(); ?>
<div role="form" data-source-id="<?= $idAttr ?>"
<form data-source-id="<?= $idAttr ?>"
class="source <?= isset($this->source) === false ? 'source-new' : '' ?> <?= (isset($this->source) && isset($this->source['error']) && strlen($this->source['error']) > 0) ? 'error' : '' ?>">
<div class="source-icon">
<?php if (isset($this->source) && isset($this->source['icon']) && $this->source['icon'] != '0') : ?>
Expand All @@ -9,8 +9,8 @@

<div class="source-title"><?= isset($this->source) ? $this->source['title'] : \F3::get('lang_source_new'); ?></div>

<div class="source-edit-delete"><button accesskey="e" class="source-showparams"><?= \F3::get('lang_source_edit')?>
</button> &bull; <button accesskey="d" class="source-delete"><?= \F3::get('lang_source_delete') ?></button>
<div class="source-edit-delete"><button type="button" accesskey="e" class="source-showparams"><?= \F3::get('lang_source_edit')?>
</button> &bull; <button type="button" accesskey="d" class="source-delete"><?= \F3::get('lang_source_delete') ?></button>
</div>

<div class="source-days">
Expand Down Expand Up @@ -88,9 +88,9 @@

<!-- save/delete -->
<li class="source-action">
<button class="source-save" accesskey="s"><?= \F3::get('lang_source_save')?></button> &bull; <button class="source-cancel" accesskey="c"><?= \F3::get('lang_source_cancel')?></button>
<button type="submit" class="source-save" accesskey="s"><?= \F3::get('lang_source_save')?></button> &bull; <button type="submit" class="source-cancel" accesskey="c"><?= \F3::get('lang_source_cancel')?></button>
</li>
</ul>


</div>
</form>

0 comments on commit 8ac6434

Please sign in to comment.