提交 77721978 编写于 作者: T tfennelly

Refactoring to extract the model parts into the modules in the model folder

上级 5d011731
var $ = require('jquery-detached').getJQuery();
var localStorage = require('./util/jenkinsLocalStorage.js');
var jenkinsLocalStorage = require('./util/jenkinsLocalStorage.js');
var configMetadata = require('./widgets/config/model/ConfigTableMetaData.js');
$(function() {
// Horrible ugly hack...
......@@ -16,7 +17,7 @@ $(function() {
var configTables = $('.job-config.tabbed');
if (configTables.size() > 0) {
var tabBarShowPreferenceKey = 'config:usetabs';
var tabBarShowPreference = localStorage.getGlobalItem(tabBarShowPreferenceKey, "yes");
var tabBarShowPreference = jenkinsLocalStorage.getGlobalItem(tabBarShowPreferenceKey, "yes");
var tabBarWidget = require('./widgets/config/tabbar.js');
if (tabBarShowPreference === "yes") {
......@@ -30,7 +31,7 @@ $(function() {
fireBottomStickerAdjustEvent();
});
tabBar.deactivator.click(function() {
localStorage.setGlobalItem(tabBarShowPreferenceKey, "no");
jenkinsLocalStorage.setGlobalItem(tabBarShowPreferenceKey, "no");
require('window-handle').getWindow().location.reload();
});
$('.jenkins-config-widgets .find-container input').focus(function() {
......@@ -39,9 +40,9 @@ $(function() {
if (tabBar.hasSections()) {
var tabBarLastSectionKey = 'config:' + tabBar.configForm.attr('name') + ':last-tab';
var tabBarLastSection = localStorage.getPageItem(tabBarLastSectionKey, tabBar.sections[0].id);
var tabBarLastSection = jenkinsLocalStorage.getPageItem(tabBarLastSectionKey, tabBar.sections[0].id);
tabBar.onShowSection(function() {
localStorage.setPageItem(tabBarLastSectionKey, this.id);
jenkinsLocalStorage.setPageItem(tabBarLastSectionKey, this.id);
});
tabBar.showSection(tabBarLastSection);
}
......@@ -50,9 +51,9 @@ $(function() {
configTables.each(function() {
var configTable = $(this);
var activator = tabBarWidget.addTabsActivator(configTable);
require('./widgets/config/table-metadata.js').markConfigForm(configTable);
configMetadata.markConfigTableParentForm(configTable);
activator.click(function() {
localStorage.setGlobalItem(tabBarShowPreferenceKey, "yes");
jenkinsLocalStorage.setGlobalItem(tabBarShowPreferenceKey, "yes");
require('window-handle').getWindow().location.reload();
});
});
......@@ -70,15 +71,15 @@ function addFinderToggle(configTableMetadata) {
var findContainer = $('.find-container', configTableMetadata.configWidgets);
if (findContainer.hasClass('visible')) {
findContainer.removeClass('visible');
localStorage.setGlobalItem(finderShowPreferenceKey, "no")
jenkinsLocalStorage.setGlobalItem(finderShowPreferenceKey, "no");
} else {
findContainer.addClass('visible');
$('input', findContainer).focus();
localStorage.setGlobalItem(finderShowPreferenceKey, "yes")
jenkinsLocalStorage.setGlobalItem(finderShowPreferenceKey, "yes");
}
});
if (localStorage.getGlobalItem(finderShowPreferenceKey, "yes") === 'yes') {
if (jenkinsLocalStorage.getGlobalItem(finderShowPreferenceKey, "yes") === 'yes') {
findToggle.click();
}
}
......
var jQD = require('../../../util/jquery-ext.js');
module.exports = ConfigRowSet;
/*
* =======================================================================================
* Configuration table section.
* =======================================================================================
*/
function ConfigRowSet(startRow) {
this.startRow = startRow;
this.rows = [];
this.endRow = undefined;
this.toggleWidget = undefined;
this.label = undefined;
}
/*
* Find the row-set toggle widget i.e. the input element that indicates that
* the row-set rows should be made visible or not.
*/
ConfigRowSet.prototype.findToggleWidget = function(row) {
var $ = jQD.getJQuery();
var input = $(':input.block-control', row);
if (input.size() === 1) {
this.toggleWidget = input;
this.label = input.parent().find('label').text();
input.addClass('disable-behavior');
}
};
var jQD = require('../../../util/jquery-ext.js');
var util = require('./util.js');
var ConfigRowSet = require('./ConfigRowSet.js');
module.exports = ConfigSection;
/*
* =======================================================================================
* Configuration table section.
* =======================================================================================
*/
function ConfigSection(parentCMD, title) {
this.parentCMD = parentCMD;
this.title = title;
this.id = util.toId(title);
this.rows = [];
this.rowSets = undefined;
this.activator = undefined;
}
/*
* Set the element (jquery) that activates the section (on click).
*/
ConfigSection.prototype.setActivator = function(activator) {
this.activator = activator;
var section = this;
section.activator.click(function() {
section.parentCMD.showSection(section);
});
};
ConfigSection.prototype.activate = function() {
if (this.activator) {
this.activator.click();
} else {
console.warn('No activator attached to config section object.');
}
};
ConfigSection.prototype.activeRowCount = function() {
var activeRowCount = 0;
for (var i = 0; i < this.rows.length; i++) {
if (this.rows[i].hasClass('active')) {
activeRowCount++;
}
}
return activeRowCount;
};
ConfigSection.prototype.updateRowSetVisibility = function() {
if (this.rowSets === undefined) {
// Lazily gather rowset information.
this.gatherRowSets();
}
for (var i = 0; i < this.rowSets.length; i++) {
var rowSet = this.rowSets[i];
if (rowSet.toggleWidget !== undefined) {
var isChecked = rowSet.toggleWidget.is(':checked');
for (var ii = 0; ii < rowSet.rows.length; ii++) {
if (isChecked) {
rowSet.rows[ii].show();
} else {
rowSet.rows[ii].hide();
}
}
}
}
};
ConfigSection.prototype.gatherRowSets = function() {
this.rowSets = [];
// Only tracking row-sets that are bounded by 'row-set-start' and 'row-set-end' (for now).
// Also, only capturing the rows after the 'block-control' input (checkbox, radio etc)
// and before the 'row-set-end'.
// TODO: Find out how these actually work. It seems like they can be nested into a hierarchy :(
// Also seems like you can have these "optional-block" thingies which are not wrapped
// in 'row-set-start' etc. Grrrrrr :(
var curRowSet = undefined; // jshint ignore:line
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
if (row.hasClass('row-set-start')) {
curRowSet = new ConfigRowSet(row);
curRowSet.findToggleWidget(row);
} else if (curRowSet !== undefined) {
if (row.hasClass('row-set-end')) {
curRowSet.endRow = row;
// Only capture the row-set if we find a 'row-set-end'.
// Yeah, this does not handle hierarchical stuff (see above TO-DO).
this.rowSets.push(curRowSet);
curRowSet = undefined;
} else if (curRowSet.toggleWidget === undefined) {
curRowSet.findToggleWidget(row);
} else {
// we have the toggleWidget, which means that this row is
// one of the rows after that row and is one of the rows that's
// subject to being made visible/hidden when the input is
// checked or unchecked.
curRowSet.rows.push(row);
}
}
}
};
ConfigSection.prototype.getRowSetLabels = function() {
var labels = [];
for (var i = 0; i < this.rowSets.length; i++) {
var rowSet = this.rowSets[i];
if (rowSet.label) {
labels.push(rowSet.label);
}
}
return labels;
};
ConfigSection.prototype.highlightText = function(text) {
var $ = jQD.getJQuery();
var selector = ":containsci('" + text + "')";
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
/*jshint loopfunc: true */
$('span.highlight-split', row).each(function() { // jshint ignore:line
var highlightSplit = $(this);
highlightSplit.before(highlightSplit.text());
highlightSplit.remove();
});
if (text !== '') {
var regex = new RegExp('(' + text + ')',"gi");
/*jshint loopfunc: true */
$(selector, row).find(':not(:input)').each(function() {
var $this = $(this);
$this.contents().each(function () {
// We specifically only mess with text nodes
if (this.nodeType === 3) {
var highlightedMarkup = this.wholeText.replace(regex, '<span class="highlight">$1</span>');
$(this).replaceWith('<span class="highlight-split">' + highlightedMarkup + '</span>');
}
});
});
}
}
};
......@@ -2,9 +2,11 @@
* Internal support module for config tables.
*/
var jQD = require('../../util/jquery-ext.js');
var jQD = require('../../../util/jquery-ext.js');
var ConfigSection = require('./ConfigSection.js');
var util = require('./util.js');
exports.markConfigForm = function(configTable) {
exports.markConfigTableParentForm = function(configTable) {
var form = configTable.closest('form');
form.addClass('jenkins-config');
return form;
......@@ -17,10 +19,10 @@ exports.findConfigTables = function() {
return $('form[name="config"] > table');
};
exports.decorateConfigTable = function(configTable) {
exports.fromConfigTable = function(configTable) {
var $ = jQD.getJQuery();
var sectionHeaders = $('.section-header', configTable);
var configForm = exports.markConfigForm(configTable);
var configForm = exports.markConfigTableParentForm(configTable);
// Mark the ancestor <tr>s of the section headers and add a title
sectionHeaders.each(function () {
......@@ -46,7 +48,7 @@ exports.decorateConfigTable = function(configTable) {
var curSection = new ConfigSection(configTableMetadata, 'General');
configTableMetadata.sections.push(curSection);
curSection.id = exports.toId(curSection.title);
curSection.id = util.toId(curSection.title);
topRows.each(function () {
var tr = $(this);
......@@ -63,187 +65,11 @@ exports.decorateConfigTable = function(configTable) {
var buttonsRow = $('#bottom-sticker', configTable).closest('tr');
buttonsRow.removeClass(curSection.id);
buttonsRow.addClass(exports.toId('buttons'));
buttonsRow.addClass(util.toId('buttons'));
return configTableMetadata;
};
exports.toId = function(string) {
string = string.trim();
return 'config_' + string.replace(/[\W_]+/g, '_').toLowerCase();
};
/*
* =======================================================================================
* Configuration table section.
* =======================================================================================
*/
function ConfigSection(parentCMD, title) {
this.parentCMD = parentCMD;
this.title = title;
this.id = exports.toId(title);
this.rows = [];
this.rowSets = undefined;
this.activator = undefined;
}
/*
* Set the element (jquery) that activates the section (on click).
*/
ConfigSection.prototype.setActivator = function(activator) {
this.activator = activator;
var section = this;
section.activator.click(function() {
section.parentCMD.showSection(section);
});
};
ConfigSection.prototype.activate = function() {
if (this.activator) {
this.activator.click();
} else {
console.warn('No activator attached to config section object.');
}
};
ConfigSection.prototype.activeRowCount = function() {
var activeRowCount = 0;
for (var i = 0; i < this.rows.length; i++) {
if (this.rows[i].hasClass('active')) {
activeRowCount++;
}
}
return activeRowCount;
};
ConfigSection.prototype.updateRowSetVisibility = function() {
if (this.rowSets === undefined) {
// Lazily gather rowset information.
this.gatherRowSets();
}
for (var i = 0; i < this.rowSets.length; i++) {
var rowSet = this.rowSets[i];
if (rowSet.toggleWidget !== undefined) {
var isChecked = rowSet.toggleWidget.is(':checked');
for (var ii = 0; ii < rowSet.rows.length; ii++) {
if (isChecked) {
rowSet.rows[ii].show();
} else {
rowSet.rows[ii].hide();
}
}
}
}
};
ConfigSection.prototype.gatherRowSets = function() {
this.rowSets = [];
// Only tracking row-sets that are bounded by 'row-set-start' and 'row-set-end' (for now).
// Also, only capturing the rows after the 'block-control' input (checkbox, radio etc)
// and before the 'row-set-end'.
// TODO: Find out how these actually work. It seems like they can be nested into a hierarchy :(
// Also seems like you can have these "optional-block" thingies which are not wrapped
// in 'row-set-start' etc. Grrrrrr :(
var curRowSet = undefined; // jshint ignore:line
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
if (row.hasClass('row-set-start')) {
curRowSet = new ConfigRowSet(row);
curRowSet.findToggleWidget(row);
} else if (curRowSet !== undefined) {
if (row.hasClass('row-set-end')) {
curRowSet.endRow = row;
// Only capture the row-set if we find a 'row-set-end'.
// Yeah, this does not handle hierarchical stuff (see above TO-DO).
this.rowSets.push(curRowSet);
curRowSet = undefined;
} else if (curRowSet.toggleWidget === undefined) {
curRowSet.findToggleWidget(row);
} else {
// we have the toggleWidget, which means that this row is
// one of the rows after that row and is one of the rows that's
// subject to being made visible/hidden when the input is
// checked or unchecked.
curRowSet.rows.push(row);
}
}
}
};
ConfigSection.prototype.getRowSetLabels = function() {
var labels = [];
for (var i = 0; i < this.rowSets.length; i++) {
var rowSet = this.rowSets[i];
if (rowSet.label) {
labels.push(rowSet.label);
}
}
return labels;
};
ConfigSection.prototype.highlightText = function(text) {
var $ = jQD.getJQuery();
var selector = ":containsci('" + text + "')";
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
/*jshint loopfunc: true */
$('span.highlight-split', row).each(function() { // jshint ignore:line
var highlightSplit = $(this);
highlightSplit.before(highlightSplit.text());
highlightSplit.remove();
});
if (text !== '') {
var regex = new RegExp('(' + text + ')',"gi");
/*jshint loopfunc: true */
$(selector, row).find(':not(:input)').each(function() {
var $this = $(this);
$this.contents().each(function () {
// We specifically only mess with text nodes
if (this.nodeType === 3) {
var highlightedMarkup = this.wholeText.replace(regex, '<span class="highlight">$1</span>');
$(this).replaceWith('<span class="highlight-split">' + highlightedMarkup + '</span>');
}
});
});
}
}
};
/*
* =======================================================================================
* Configuration table section.
* =======================================================================================
*/
function ConfigRowSet(startRow) {
this.startRow = startRow;
this.rows = [];
this.endRow = undefined;
this.toggleWidget = undefined;
this.label = undefined;
}
/*
* Find the row-set toggle widget i.e. the input element that indicates that
* the row-set rows should be made visible or not.
*/
ConfigRowSet.prototype.findToggleWidget = function(row) {
var $ = jQD.getJQuery();
var input = $(':input.block-control', row);
if (input.size() === 1) {
this.toggleWidget = input;
this.label = input.parent().find('label').text();
input.addClass('disable-behavior');
}
};
/*
* =======================================================================================
* ConfigTable MetaData class.
......
exports.toId = function(string) {
string = string.trim();
return 'config_' + string.replace(/[\W_]+/g, '_').toLowerCase();
};
\ No newline at end of file
var jQD = require('jquery-detached');
var tableMetadata = require('./table-metadata');
var tableMetadata = require('./model/ConfigTableMetaData.js');
exports.addTabsOnFirst = function() {
return exports.addTabs(tableMetadata.findConfigTables().first());
......@@ -18,11 +18,11 @@ exports.addTabs = function(configTable) {
if (configTableEl.size() === 0) {
throw "No config table found using selector '" + configTable + "'";
} else {
configTableMetadata = tableMetadata.decorateConfigTable(configTableEl);
configTableMetadata = tableMetadata.fromConfigTable(configTableEl);
}
} else {
// It's a config <table> element
configTableMetadata = tableMetadata.decorateConfigTable(configTable);
configTableMetadata = tableMetadata.fromConfigTable(configTable);
}
var tabBar = $('<div class="tabBar config-section-activators"></div>');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册