2013-08-29 10:29:56 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var website = openerp.website;
|
|
|
|
website.EditorBar.include({
|
|
|
|
events: _.extend({}, website.EditorBar.prototype.events, {
|
|
|
|
'click a[data-action=promote-current-page]': 'promotePage',
|
|
|
|
}),
|
|
|
|
promotePage: function () {
|
|
|
|
(new website.seo.Configurator()).appendTo($(document.body));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2013-08-29 16:39:52 +00:00
|
|
|
function cleanupKeyword (word) {
|
|
|
|
return word ? word.replace(/[,;.:]+/g, " ").replace(/ +/g, " ").trim() : "";
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
website.seo = {};
|
2013-08-29 16:39:52 +00:00
|
|
|
|
|
|
|
website.seo.Tip = openerp.Widget.extend({
|
|
|
|
template: 'website.seo_tip',
|
|
|
|
events: {
|
2013-08-30 13:19:52 +00:00
|
|
|
'closed.bs.alert': 'destroy',
|
2013-08-29 16:39:52 +00:00
|
|
|
},
|
|
|
|
init: function (parent, options) {
|
|
|
|
this._super(parent);
|
|
|
|
this.message = options.message;
|
2013-08-30 11:21:24 +00:00
|
|
|
// success, info, warning or danger
|
2013-08-29 16:39:52 +00:00
|
|
|
this.type = options.type;
|
2013-08-30 13:19:52 +00:00
|
|
|
}
|
2013-08-29 16:39:52 +00:00
|
|
|
});
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
website.seo.Keyword = openerp.Widget.extend({
|
|
|
|
template: 'website.seo_keyword',
|
|
|
|
events: {
|
|
|
|
'click a[data-action=remove-keyword]': 'destroy',
|
|
|
|
},
|
|
|
|
init: function (parent, options) {
|
|
|
|
this._super(parent);
|
|
|
|
this.keyword = options.keyword;
|
|
|
|
this.onDelete = options.onDelete;
|
|
|
|
},
|
|
|
|
destroy: function () {
|
2013-08-30 13:19:52 +00:00
|
|
|
console.log("Destroyed", this.keyword);
|
2013-08-29 10:29:56 +00:00
|
|
|
if (_.isFunction(this.onDelete)) {
|
|
|
|
this.onDelete(this.keyword);
|
|
|
|
}
|
|
|
|
this._super();
|
|
|
|
},
|
|
|
|
});
|
2013-08-29 16:39:52 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
website.seo.PageParser = openerp.Class.extend({
|
|
|
|
init: function () {
|
|
|
|
this._url = this._currentURL();
|
|
|
|
this._title = $(document.title).text();
|
|
|
|
this._headers = {};
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
_.each([ 'h1', 'h2', 'h3'], function (header) {
|
|
|
|
self._headers[header] = $(header).map(function () {
|
|
|
|
return $(this).text();
|
|
|
|
}).get();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
url: function () {
|
|
|
|
return this._url;
|
|
|
|
},
|
|
|
|
title: function () {
|
|
|
|
return this._title;
|
|
|
|
},
|
|
|
|
headers: function () {
|
|
|
|
return this._headers;
|
|
|
|
},
|
|
|
|
_currentURL: function () {
|
|
|
|
var url = window.location.href;
|
|
|
|
var hashIndex = url.indexOf('#');
|
|
|
|
return hashIndex >= 0 ? url.substring(0, hashIndex) : url;
|
|
|
|
},
|
|
|
|
keywordSuggestions: function () {
|
|
|
|
var headers = this.headers();
|
|
|
|
return _.map(_.uniq(headers.h1.concat(headers.h2)),
|
2013-08-29 16:39:52 +00:00
|
|
|
cleanupKeyword);
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
website.seo.Configurator = openerp.Widget.extend({
|
|
|
|
template: 'website.seo_configuration',
|
|
|
|
events: {
|
|
|
|
'keypress input[name=seo_page_keywords]': 'confirmKeyword',
|
|
|
|
'click button[data-action=add]': 'addKeyword',
|
|
|
|
'click a[data-action=update]': 'update',
|
2013-08-30 13:19:52 +00:00
|
|
|
'hidden.bs.modal': 'destroy'
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
maxTitleSize: 65,
|
|
|
|
maxDescriptionSize: 155,
|
|
|
|
maxNumberOfKeywords: 10,
|
|
|
|
maxWordsPerKeyword: 4,
|
|
|
|
|
|
|
|
start: function () {
|
|
|
|
var pageParser = new website.seo.PageParser();
|
|
|
|
this.$el.find('.js_seo_page_url').text(pageParser.url());
|
|
|
|
this.$el.find('input[name=seo_page_title]').val(pageParser.title());
|
2013-08-29 16:39:52 +00:00
|
|
|
this.checkBestPractices(pageParser);
|
2013-08-29 10:29:56 +00:00
|
|
|
this.$el.modal();
|
|
|
|
},
|
2013-08-29 16:39:52 +00:00
|
|
|
checkBestPractices: function (parser) {
|
|
|
|
var pageParser = parser || new website.seo.PageParser();
|
|
|
|
if (pageParser.headers()['h1'].length > 1) {
|
|
|
|
new website.seo.Tip(this, {
|
|
|
|
message: "You have more than one <h1> tag on the page.",
|
2013-08-30 11:21:24 +00:00
|
|
|
type: 'danger'
|
2013-08-29 16:39:52 +00:00
|
|
|
}).appendTo(this.$el.find('.js_seo_tips'));
|
|
|
|
}
|
|
|
|
},
|
2013-08-29 10:29:56 +00:00
|
|
|
currentPage: function () {
|
|
|
|
var url = window.location.href;
|
|
|
|
var hashIndex = url.indexOf('#');
|
|
|
|
return hashIndex > 0 ? url.substring(0, hashIndex): url;
|
|
|
|
},
|
|
|
|
keywords: function () {
|
|
|
|
return _.uniq($('.js_seo_keyword').map(function () {
|
|
|
|
return $(this).text();
|
2013-08-29 16:39:52 +00:00
|
|
|
}));
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
isExistingKeyword: function (word) {
|
|
|
|
return _.contains(this.keywords(), word);
|
|
|
|
},
|
|
|
|
isKeywordListFull: function () {
|
|
|
|
return this.keywords().length >= this.maxNumberOfKeywords;
|
|
|
|
},
|
|
|
|
confirmKeyword: function (e) {
|
|
|
|
if (e.keyCode == 13) {
|
|
|
|
this.addKeyword();
|
|
|
|
this.$el.find('input[name=seo_page_keywords]').val("");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addKeyword: function () {
|
|
|
|
var $modal = this.$el;
|
|
|
|
function enableNewKeywords () {
|
|
|
|
$modal.find('input[name=seo_page_keywords]')
|
2013-08-29 16:39:52 +00:00
|
|
|
.removeAttr('readonly').attr('placeholder', "");
|
2013-08-29 10:29:56 +00:00
|
|
|
$modal.find('button[data-action=add]')
|
|
|
|
.prop('disabled', false).removeClass('disabled');
|
|
|
|
}
|
|
|
|
function disableNewKeywords () {
|
|
|
|
$modal.find('input[name=seo_page_keywords]')
|
|
|
|
.attr('readonly', "readonly")
|
|
|
|
.attr('placeholder', "Remove a keyword first");
|
|
|
|
$modal.find('button[data-action=add]')
|
|
|
|
.prop('disabled', true).addClass('disabled');
|
|
|
|
}
|
|
|
|
var candidate = this.$el.find('input[name=seo_page_keywords]').val();
|
2013-08-29 16:39:52 +00:00
|
|
|
var word = cleanupKeyword(candidate);
|
2013-08-29 10:29:56 +00:00
|
|
|
if (word && !this.isKeywordListFull() && !this.isExistingKeyword(word)) {
|
|
|
|
new website.seo.Keyword(this, {
|
|
|
|
keyword: word,
|
|
|
|
onDelete: enableNewKeywords
|
|
|
|
}).appendTo(this.$el.find('.js_seo_keywords_list'));
|
|
|
|
}
|
|
|
|
if (this.isKeywordListFull()) {
|
|
|
|
disableNewKeywords();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: function () {
|
|
|
|
// TODO: Persist changes
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})();
|