我怎样才能将 Confluence 插件与 jQuery 代码结合起来?
How can I do to combine Confluence plugin with jQuery code?
我尝试使用以下 js 文件(文本 1)创建一个 Confluence 插件,但 Confluence 控制台显示以下错误(文本 2),请问有人可以帮我解决这个问题吗?
我想知道 jQuery 代码是否应该以 follow:
开头
AJS.toInit(function() {
AJS.$(document).ready(function() {
【正文1】
(function ($) {
$.fn.jaliswall = function (options) {
this.each(function () {
var defaults = {
item: '.wall-item',
columnClass: '.wall-column',
resize: false
}
var prm = $.extend(defaults, options);
var container = $(this);
var items = container.find(prm.item);
var elemsDatas = [];
var columns = [];
var nbCols = getNbCols();
init();
function init() {
nbCols = getNbCols();
recordAndRemove();
print();
if (prm.resize) {
$(window).resize(function () {
if (nbCols != getNbCols()) {
nbCols = getNbCols();
setColPos();
print();
}
});
}
}
function getNbCols() {
var instanceForCompute = false;
if (container.find(prm.columnClass).length == 0) {
instanceForCompute = true;
container.append("<div class='" + parseSelector(prm.columnClass) + "'></div>");
}
var colWidth = container.find(prm.columnClass).outerWidth(true);
var wallWidth = container.innerWidth();
if (instanceForCompute) container.find(prm.columnClass).remove();
return Math.round(wallWidth / colWidth);
}
// save items content and params and removes originals items
function recordAndRemove() {
items.each(function (index) {
var item = $(this);
elemsDatas.push({
content: item.html(),
class: item.attr('class'),
href: item.attr('href'),
id: item.attr('id'),
colid: index % nbCols
});
item.remove();
});
}
//determines in which column an item should be
function setColPos() {
for (var i in elemsDatas) {
elemsDatas[i].colid = i % nbCols;
}
}
// to get a class name without the selector
function parseSelector(selector) {
return selector.slice(1, selector.length);
}
//write and append html
function print() {
var tree = '';
//creates columns
for (var i = 0; i < nbCols; i++) {
tree += "<div class='" + parseSelector(prm.columnClass) + "'></div>";
}
container.html(tree);
// creates items
for (var i in elemsDatas) {
var html = '';
var content = (elemsDatas[i].content != undefined) ? elemsDatas[i].content : '';
var href = (elemsDatas[i].href != href) ? elemsDatas[i].href : '';
var classe = (elemsDatas[i].class != undefined) ? elemsDatas[i].class : '';
var id = (elemsDatas[i].id != undefined) ? elemsDatas[i].id : '';
if (elemsDatas[i].href != undefined) {
html += "<a " + getAttr(href, 'href') + " " + getAttr(classe, 'class') + " " + getAttr(id, 'id') + ">" + content + "</a>";
} else {
html += "<div " + getAttr(classe, 'class') + " " + getAttr(id, 'id') + ">" + content + "</a>";
}
container.children(prm.columnClass).eq(i % nbCols).append(html);
}
}
//creates a well-formed attribute
function getAttr(attr, type) {
return (attr != undefined) ? type + "='" + attr + "'" : '';
}
});
return this;
}
})(jQuery);
【正文2】
[INFO] Compiling javascript using YUI
[ERROR] invalid property id
class: item.attr('class'),
[ERROR] syntax error
class: item.attr('class'),
[ERROR] syntax error
href: item.attr('href'),
[ERROR] syntax error
id: item.attr('id'),
[ERROR] syntax error
colid: index % nbCols
[ERROR] syntax error
});
[ERROR] missing name after . operator
var classe = (elemsDatas[i].class != undefined) ? elemsDatas[i].class : '';
[ERROR] Compilation produced 7 syntax errors.
问题是 'class' 是一个保留字,YUI JavaScript 压缩器抱怨它(这是正确的)。您可以在此处的代码中修复此问题:
var item = $(this);
elemsDatas.push({
content: item.html(),
class: item.attr('class'),
href: item.attr('href'),
id: item.attr('id'),
colid: index % nbCols
});
通过将 class: item.attr('class')
更改为 clazz: item.addr('class')
或类似的东西——尽管这样您也必须调整访问此 属性 的所有其他引用。
我相信如果你真的愿意的话,你也可以使用 'class': item.addr('class')
,但这会导致以后访问 属性 变得不那么方便。
我尝试使用以下 js 文件(文本 1)创建一个 Confluence 插件,但 Confluence 控制台显示以下错误(文本 2),请问有人可以帮我解决这个问题吗? 我想知道 jQuery 代码是否应该以 follow:
开头AJS.toInit(function() {
AJS.$(document).ready(function() {
【正文1】
(function ($) {
$.fn.jaliswall = function (options) {
this.each(function () {
var defaults = {
item: '.wall-item',
columnClass: '.wall-column',
resize: false
}
var prm = $.extend(defaults, options);
var container = $(this);
var items = container.find(prm.item);
var elemsDatas = [];
var columns = [];
var nbCols = getNbCols();
init();
function init() {
nbCols = getNbCols();
recordAndRemove();
print();
if (prm.resize) {
$(window).resize(function () {
if (nbCols != getNbCols()) {
nbCols = getNbCols();
setColPos();
print();
}
});
}
}
function getNbCols() {
var instanceForCompute = false;
if (container.find(prm.columnClass).length == 0) {
instanceForCompute = true;
container.append("<div class='" + parseSelector(prm.columnClass) + "'></div>");
}
var colWidth = container.find(prm.columnClass).outerWidth(true);
var wallWidth = container.innerWidth();
if (instanceForCompute) container.find(prm.columnClass).remove();
return Math.round(wallWidth / colWidth);
}
// save items content and params and removes originals items
function recordAndRemove() {
items.each(function (index) {
var item = $(this);
elemsDatas.push({
content: item.html(),
class: item.attr('class'),
href: item.attr('href'),
id: item.attr('id'),
colid: index % nbCols
});
item.remove();
});
}
//determines in which column an item should be
function setColPos() {
for (var i in elemsDatas) {
elemsDatas[i].colid = i % nbCols;
}
}
// to get a class name without the selector
function parseSelector(selector) {
return selector.slice(1, selector.length);
}
//write and append html
function print() {
var tree = '';
//creates columns
for (var i = 0; i < nbCols; i++) {
tree += "<div class='" + parseSelector(prm.columnClass) + "'></div>";
}
container.html(tree);
// creates items
for (var i in elemsDatas) {
var html = '';
var content = (elemsDatas[i].content != undefined) ? elemsDatas[i].content : '';
var href = (elemsDatas[i].href != href) ? elemsDatas[i].href : '';
var classe = (elemsDatas[i].class != undefined) ? elemsDatas[i].class : '';
var id = (elemsDatas[i].id != undefined) ? elemsDatas[i].id : '';
if (elemsDatas[i].href != undefined) {
html += "<a " + getAttr(href, 'href') + " " + getAttr(classe, 'class') + " " + getAttr(id, 'id') + ">" + content + "</a>";
} else {
html += "<div " + getAttr(classe, 'class') + " " + getAttr(id, 'id') + ">" + content + "</a>";
}
container.children(prm.columnClass).eq(i % nbCols).append(html);
}
}
//creates a well-formed attribute
function getAttr(attr, type) {
return (attr != undefined) ? type + "='" + attr + "'" : '';
}
});
return this;
}
})(jQuery);
【正文2】
[INFO] Compiling javascript using YUI
[ERROR] invalid property id
class: item.attr('class'),
[ERROR] syntax error
class: item.attr('class'),
[ERROR] syntax error
href: item.attr('href'),
[ERROR] syntax error
id: item.attr('id'),
[ERROR] syntax error
colid: index % nbCols
[ERROR] syntax error
});
[ERROR] missing name after . operator
var classe = (elemsDatas[i].class != undefined) ? elemsDatas[i].class : '';
[ERROR] Compilation produced 7 syntax errors.
问题是 'class' 是一个保留字,YUI JavaScript 压缩器抱怨它(这是正确的)。您可以在此处的代码中修复此问题:
var item = $(this);
elemsDatas.push({
content: item.html(),
class: item.attr('class'),
href: item.attr('href'),
id: item.attr('id'),
colid: index % nbCols
});
通过将 class: item.attr('class')
更改为 clazz: item.addr('class')
或类似的东西——尽管这样您也必须调整访问此 属性 的所有其他引用。
我相信如果你真的愿意的话,你也可以使用 'class': item.addr('class')
,但这会导致以后访问 属性 变得不那么方便。