[IMP] split new page and existing page sections in link dialog + look fixes

* select sections on click within section
* validation in case no value provided for selected section on save

bzr revid: xmo@openerp.com-20130913091532-i037z59odb4j52b7
This commit is contained in:
Xavier Morel 2013-09-13 11:15:32 +02:00
parent 96d0662732
commit c52335de5a
2 changed files with 66 additions and 54 deletions

View File

@ -400,14 +400,23 @@
template: 'website.editor.dialog.link',
events: _.extend({}, website.editor.Dialog.prototype.events, {
'change .url-source': function (e) { this.changed($(e.target)); },
'click div.existing a': 'select_page',
'mousedown': function (e) {
var $target = $(e.target).closest('.list-group-item');
if (!$target.length || $target.hasClass('active')) {
// clicked outside groups, or clicked in active groups
return;
}
$target
.addClass('active')
.siblings().removeClass('active')
.addBack().removeClass('has-error');
}
}),
init: function (editor) {
this._super(editor);
// url -> name mapping for existing pages
this.pages = Object.create(null);
// name -> url mapping for the same
this.pages_by_name = Object.create(null);
},
start: function () {
var element;
@ -466,25 +475,25 @@
},
save: function () {
var self = this, _super = this._super.bind(this);
var $e = this.$('.url-source').filter(function () { return !!this.value; });
var $e = this.$('.list-group-item.active .url-source');
var val = $e.val();
if (!val) {
$e.closest('.form-group').addClass('has-error');
return;
}
var val = $e.val(), done = $.when();
var done = $.when();
if ($e.hasClass('email-address')) {
this.make_link('mailto:' + val, false, val);
} else if ($e.hasClass('existing')) {
self.make_link(val, false, this.pages[val]);
} else if ($e.hasClass('pages')) {
// ``val`` is the *name* of the page
var url = this.pages_by_name[val];
if (!url) {
// Create the page, get the URL back
done = $.get(_.str.sprintf(
// Create the page, get the URL back
done = $.get(_.str.sprintf(
'/pagenew/%s?noredirect', encodeURIComponent(val)))
.then(function (response) {
url = response;
});
}
done.then(function () {
self.make_link(url, false, val);
});
.then(function (response) {
self.make_link(response, false, val);
});
} else {
this.make_link(val, this.$('input.window-new').prop('checked'));
}
@ -499,7 +508,7 @@
if (match = /(mailto):(.+)/.exec(href)) {
$control = this.$('input.email-address').val(match[2]);
} else if(href in this.pages) {
$control = this.$('input.pages').val(this.pages[href]);
$control = this.$('select.existing').val(href);
}
if (!$control) {
$control = this.$('input.url').val(href);
@ -511,22 +520,8 @@
'checked', this.element.getAttribute('target') === '_blank');
},
changed: function ($e) {
$e.closest('li.list-group-item').addClass('active')
.siblings().removeClass('active');
this.$('.url-source').not($e).val('');
},
/**
* Selected an existing page in dropdown
*/
select_page: function (e) {
e.preventDefault();
e.stopPropagation();
var $target = $(e.target);
this.$('input.pages').val($target.text()).change();
// No #dropdown('close'), and using #dropdown('toggle') sur
// #closest('.dropdown') makes the dropdown not work correctly
$target.closest('.open').removeClass('open');
},
/**
* CKEDITOR.plugins.link.getSelectedLink ignores the editor's root,
* if the editor is set directly on a link it will thus not work.
@ -556,12 +551,12 @@
},
fill_pages: function (results) {
var self = this;
var $pages = this.$('div.existing ul').empty();
var pages = this.$('select.existing')[0];
_(results).each(function (result) {
self.pages[result.url] = result.name;
self.pages_by_name[result.name] = result.url;
var $link = $('<a>').attr('href', result.url).text(result.name);
$('<li>').append($link).appendTo($pages);
pages.options[pages.options.length] =
new Option(result.name, result.url);
});
},
});

View File

@ -35,33 +35,50 @@
<t t-call="website.editor.dialog">
<t t-set="title">Link to</t>
<form>
<ul class="list-group form-group">
<li class="list-group-item active">
<h3 class="list-group-item-heading">Page on your website</h3>
<div class="input-group">
<input type="text" class="form-control pages url-source"
placeholder="Create new page"/>
<div class="input-group-btn existing dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Existing page <span class="caret"></span></button>
<ul class="dropdown-menu pull-right"/>
</div>
</div>
<ul class="list-group">
<li class="list-group-item form-group active">
<h3 class="list-group-item-heading">
<label for="link-existing" class="control-label">
Existing page
</label>
</h3>
<select class="existing form-control url-source"
id="link-existing">
<option/>
</select>
</li>
<li class="list-group-item">
<li class="list-group-item form-group">
<h3 class="list-group-item-heading">
<label for="link-new" class="control-label">
New page
</label>
</h3>
<input type="text" class="form-control pages url-source"
id="link-new" placeholder="Create new page"/>
</li>
<li class="list-group-item form-group clearfix">
<h3 class="list-group-item-heading">
<label for="link-external" class="control-label">
External Page
</label>
</h3>
<input type="text" class="form-control url url-source"
id="link-external" placeholder="http://openerp.com"/>
<div class="pull-right">
<label>
<input type="checkbox" class="window-new"/>
Open in new window
</label>
</div>
<h3 class="list-group-item-heading">External Page</h3>
<input type="text" class="form-control url url-source"
placeholder="http://openerp.com"/>
</li>
<li class="list-group-item">
<h3 class="list-group-item-heading">Email Address</h3>
<li class="list-group-item form-group">
<h3 class="list-group-item-heading">
<label for="link-email" class="control-label">
Email Address
</label>
</h3>
<input type="email" class="form-control email-address url-source"
placeholder="you@yourwebsite.com"/>
id="link-email" placeholder="you@yourwebsite.com"/>
</li>
</ul>
</form>