mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-06 14:05:46 +01:00
moving widgets and variables code out of ajaxify
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var ajaxify = {};
|
var ajaxify = ajaxify || {};
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
|
||||||
@@ -13,6 +13,7 @@ var ajaxify = {};
|
|||||||
apiXHR = null;
|
apiXHR = null;
|
||||||
|
|
||||||
var events = [];
|
var events = [];
|
||||||
|
|
||||||
ajaxify.register_events = function (new_page_events) {
|
ajaxify.register_events = function (new_page_events) {
|
||||||
for (var i = 0, ii = events.length; i < ii; i++) {
|
for (var i = 0, ii = events.length; i < ii; i++) {
|
||||||
socket.removeAllListeners(events[i]); // optimize this to user removeListener(event, listener) instead.
|
socket.removeAllListeners(events[i]); // optimize this to user removeListener(event, listener) instead.
|
||||||
@@ -95,7 +96,7 @@ var ajaxify = {};
|
|||||||
|
|
||||||
app.processPage();
|
app.processPage();
|
||||||
|
|
||||||
ajaxify.renderWidgets(tpl_url, url, function(err) {
|
ajaxify.widgets.render(tpl_url, url, function(err) {
|
||||||
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
|
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
|
||||||
ajaxify.initialLoad = false;
|
ajaxify.initialLoad = false;
|
||||||
|
|
||||||
@@ -234,97 +235,6 @@ var ajaxify = {};
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ajaxify.variables = (function() {
|
|
||||||
var parsedVariables = {};
|
|
||||||
|
|
||||||
return {
|
|
||||||
set: function(key, value) {
|
|
||||||
parsedVariables[key] = value;
|
|
||||||
},
|
|
||||||
get: function(key) {
|
|
||||||
return parsedVariables[key];
|
|
||||||
},
|
|
||||||
flush: function() {
|
|
||||||
parsedVariables = {};
|
|
||||||
},
|
|
||||||
parse: function() {
|
|
||||||
$('#content [template-variable]').each(function(index, element) {
|
|
||||||
var value = null;
|
|
||||||
|
|
||||||
switch ($(element).attr('template-type')) {
|
|
||||||
case 'boolean':
|
|
||||||
value = ($(element).val() === 'true' || $(element).val() === '1') ? true : false;
|
|
||||||
break;
|
|
||||||
case 'int':
|
|
||||||
case 'integer':
|
|
||||||
value = parseInt($(element).val());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
value = $(element).val();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ajaxify.variables.set($(element).attr('template-variable'), value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
ajaxify.repositionNoWidgets = function() {
|
|
||||||
$('body [no-widget-class]').each(function() {
|
|
||||||
var $this = $(this);
|
|
||||||
$this.removeClass();
|
|
||||||
$this.addClass($this.attr('no-widget-class'));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
ajaxify.renderWidgets = function(tpl_url, url, callback) {
|
|
||||||
var widgetLocations = [], numLocations;
|
|
||||||
|
|
||||||
$('#content [widget-area]').each(function() {
|
|
||||||
widgetLocations.push($(this).attr('widget-area'));
|
|
||||||
});
|
|
||||||
|
|
||||||
numLocations = widgetLocations.length;
|
|
||||||
|
|
||||||
if (!numLocations) {
|
|
||||||
ajaxify.repositionNoWidgets();
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderWidgets(location) {
|
|
||||||
var area = $('#content [widget-area="' + location + '"]');
|
|
||||||
|
|
||||||
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
|
|
||||||
if (area.html()) {
|
|
||||||
area.html(templates.parse(area.html(), {widgets: renderedWidgets}))
|
|
||||||
.removeClass('hidden');
|
|
||||||
|
|
||||||
if (!renderedWidgets.length) {
|
|
||||||
ajaxify.repositionNoWidgets();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#content [widget-area] img:not(.user-img)').addClass('img-responsive');
|
|
||||||
checkCallback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkCallback() {
|
|
||||||
numLocations--;
|
|
||||||
if (numLocations < 0 && callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i in widgetLocations) {
|
|
||||||
if (widgetLocations.hasOwnProperty(i)) {
|
|
||||||
renderWidgets(widgetLocations[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
checkCallback();
|
|
||||||
};
|
|
||||||
|
|
||||||
$('document').ready(function () {
|
$('document').ready(function () {
|
||||||
if (!window.history || !window.history.pushState) {
|
if (!window.history || !window.history.pushState) {
|
||||||
return; // no ajaxification for old browsers
|
return; // no ajaxification for old browsers
|
||||||
|
|||||||
@@ -682,7 +682,7 @@ var socket,
|
|||||||
ajaxify.variables.parse();
|
ajaxify.variables.parse();
|
||||||
app.processPage();
|
app.processPage();
|
||||||
|
|
||||||
ajaxify.renderWidgets(tpl_url, url);
|
ajaxify.widgets.render(tpl_url, url);
|
||||||
|
|
||||||
ajaxify.loadScript(tpl_url, function() {
|
ajaxify.loadScript(tpl_url, function() {
|
||||||
$(window).trigger('action:ajaxify.end', {
|
$(window).trigger('action:ajaxify.end', {
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
|||||||
var noTopicsWarning = $('#category-no-topics');
|
var noTopicsWarning = $('#category-no-topics');
|
||||||
if (noTopicsWarning.length) {
|
if (noTopicsWarning.length) {
|
||||||
noTopicsWarning.remove();
|
noTopicsWarning.remove();
|
||||||
ajaxify.renderWidgets('category', window.location.pathname.slice(1));
|
ajaxify.widgets.render('category', window.location.pathname.slice(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numTopics > 0) {
|
if (numTopics > 0) {
|
||||||
|
|||||||
41
public/src/variables.js
Normal file
41
public/src/variables.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
"use strict";
|
||||||
|
/*global ajaxify*/
|
||||||
|
|
||||||
|
(function(ajaxify) {
|
||||||
|
var parsedVariables = {};
|
||||||
|
|
||||||
|
ajaxify.variables = {};
|
||||||
|
|
||||||
|
ajaxify.variables.set = function(key, value) {
|
||||||
|
parsedVariables[key] = value;
|
||||||
|
};
|
||||||
|
|
||||||
|
ajaxify.variables.get = function(key) {
|
||||||
|
return parsedVariables[key];
|
||||||
|
};
|
||||||
|
|
||||||
|
ajaxify.variables.flush = function() {
|
||||||
|
parsedVariables = {};
|
||||||
|
};
|
||||||
|
|
||||||
|
ajaxify.variables.parse = function() {
|
||||||
|
$('#content [template-variable]').each(function(index, element) {
|
||||||
|
var value = null;
|
||||||
|
|
||||||
|
switch ($(element).attr('template-type')) {
|
||||||
|
case 'boolean':
|
||||||
|
value = ($(element).val() === 'true' || $(element).val() === '1') ? true : false;
|
||||||
|
break;
|
||||||
|
case 'int':
|
||||||
|
case 'integer':
|
||||||
|
value = parseInt($(element).val());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
value = $(element).val();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ajaxify.variables.set($(element).attr('template-variable'), value);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}(ajaxify || {}));
|
||||||
59
public/src/widgets.js
Normal file
59
public/src/widgets.js
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
"use strict";
|
||||||
|
/*global ajaxify, socket, templates*/
|
||||||
|
|
||||||
|
(function(ajaxify) {
|
||||||
|
ajaxify.widgets.reposition = function() {
|
||||||
|
$('body [no-widget-class]').each(function() {
|
||||||
|
var $this = $(this);
|
||||||
|
$this.removeClass();
|
||||||
|
$this.addClass($this.attr('no-widget-class'));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
ajaxify.widgets.render = function(tpl_url, url, callback) {
|
||||||
|
var widgetLocations = [], numLocations;
|
||||||
|
|
||||||
|
$('#content [widget-area]').each(function() {
|
||||||
|
widgetLocations.push($(this).attr('widget-area'));
|
||||||
|
});
|
||||||
|
|
||||||
|
numLocations = widgetLocations.length;
|
||||||
|
|
||||||
|
if (!numLocations) {
|
||||||
|
ajaxify.widgets.reposition();
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderWidgets(location) {
|
||||||
|
var area = $('#content [widget-area="' + location + '"]');
|
||||||
|
|
||||||
|
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
|
||||||
|
if (area.html()) {
|
||||||
|
area.html(templates.parse(area.html(), {widgets: renderedWidgets}))
|
||||||
|
.removeClass('hidden');
|
||||||
|
|
||||||
|
if (!renderedWidgets.length) {
|
||||||
|
ajaxify.widgets.reposition();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#content [widget-area] img:not(.user-img)').addClass('img-responsive');
|
||||||
|
checkCallback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkCallback() {
|
||||||
|
numLocations--;
|
||||||
|
if (numLocations < 0 && callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i in widgetLocations) {
|
||||||
|
if (widgetLocations.hasOwnProperty(i)) {
|
||||||
|
renderWidgets(widgetLocations[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCallback();
|
||||||
|
};
|
||||||
|
}(ajaxify || {}));
|
||||||
@@ -243,6 +243,8 @@ var fs = require('fs'),
|
|||||||
'src/app.js',
|
'src/app.js',
|
||||||
'src/templates.js',
|
'src/templates.js',
|
||||||
'src/ajaxify.js',
|
'src/ajaxify.js',
|
||||||
|
'src/variables.js',
|
||||||
|
'src/widgets.js',
|
||||||
'src/translator.js',
|
'src/translator.js',
|
||||||
'src/overrides.js',
|
'src/overrides.js',
|
||||||
'src/utils.js'
|
'src/utils.js'
|
||||||
|
|||||||
Reference in New Issue
Block a user