Skip to content

Instantly share code, notes, and snippets.

@rich97
Created July 25, 2012 10:41
Show Gist options
  • Save rich97/3175489 to your computer and use it in GitHub Desktop.
Save rich97/3175489 to your computer and use it in GitHub Desktop.
var DOM = (function() {
var maps = {};
maps.$progressMap = {};
maps.$previewMap = {};
function updateProgress(options) {
if (typeof options !== 'object') {
return;
}
$this = this.elements;
if (typeof options.completed === 'number') {
$this.bar.width(options.completed + '%');
}
if (typeof options.status === 'string') {
$this.item
.removeClass()
.addClass('progress-bar ' + options.status);
}
if (typeof options.message === 'string') {
$listItem = $('<li />')
.addClass('message');
if (typeof options.status === 'string') {
$listItem.addClass(options.status);
}
$this.messages
.append(
$listItem.html(options.message)
);
}
}
function removeProgress(options) {
var timeout = 0;
if (options !== undefined && options.timeout !== undefined) {
timeout = options.timeout;
}
var key = this.options.key;
var self = this;
// remove progress bar
setTimeout(function() {
self.elements.item.remove();
delete maps.$progressMap[key];
}, timeout);
}
function updatePreview(options) {
if (typeof options !== 'object') {
return;
}
$this = this.elements;
if (typeof options.error === 'string') {
$this.content.html('');
var $error = DOM.init('error');
$error.elements.content.html(options.error);
$this.removeLink.remove();
$this.item
.removeClass('success')
.addClass('error');
$this.content
.append(
$error.elements.image,
$error.elements.content
);
$error.elements.image.load(function() {
$this.loadingOverlay.hide();
});
}
if (typeof options.loaded === 'boolean') {
if (options.loaded === true) {
$this.loadingOverlay.fadeOut('slow');
} else {
$this.loadingOverlay.fadeIn('slow');
}
}
}
function removePreview(options) {
$this = this.elements;
var group = $this.item.attr('data-group') || $this.item.data('group')
, index = $this.item.attr('data-index') || $this.item.data('index');
if (maps.$previewMap[group] === undefined) {
return;
}
if (maps.$previewMap[group][index] !== undefined) {
delete maps.$previewMap[group][index];
}
$this.item.fadeOut('slow', function() {
$this.item.remove();
});
$.get(options.deleteScript);
}
var init = function(name, options) {
var elems = {
$progress: function(options) {
var key = options.key;
var $cancel = $('<a />')
.addClass('progress-cancel-all')
.attr('href', '#')
.text('Cancel All ')
.append($('<i />').addClass('icon-remove'));
var $title = $('<h3 />')
, $bar = $('<div />').addClass('progress-inner')
, $messages = $('<ul />').addClass('feedback-messages');
var $item = $('<div />').addClass('progress-bar')
.append(
$title,
$cancel,
$('<div />')
.addClass('progress-outer')
.html($bar),
$messages
);
var e = function() {
this.options = options;
this.elements = {
item: $item,
cancel: $cancel,
title: $title,
bar: $bar,
messages: $messages
};
};
e.prototype.update = updateProgress;
e.prototype.remove = removeProgress;
maps.$progressMap[key] = new e();
return maps.$progressMap[key];
},
$preview: function(options) {
if (options.elements === undefined) {
options.elements = {};
options.elements.title = $('<h4 />');
options.elements.content = $('<div />')
.addClass('pi-content');
options.elements.loadingOverlay = $('<div />')
.addClass('loading')
.html('<img src="/unew/images/loader.gif" alt="loader" />');
options.elements.removeLink = $('<a />')
.attr('href', '#')
.addClass('icon-remove')
.html('&nbsp;');
options.elements.item = $('<li />')
.addClass('preview-image')
.append(
$('<div />').addClass('pi-header').append(
options.elements.title, options.elements.removeLink
),
$('<div />').addClass('pi-content-wrapper').append(
options.elements.loadingOverlay, options.elements.content
)
);
options.elements.item
.data('group', options.key)
.data('index', options.index);
}
var e = function() {
this.elements = options.elements;
delete options.elements;
this.options = options;
};
e.prototype.update = updatePreview;
e.prototype.remove = removePreview;
if (maps.$previewMap[options.key] === undefined) {
maps.$previewMap[options.key] = {};
}
maps.$previewMap[options.key][options.index] = new e();
return maps.$previewMap[options.key][options.index];
},
$error: function() {
$image = $('<img />')
.attr('src', '/unew/images/error.png')
.attr('alt', 'error');
$content = $('<div />');
var e = function() {
this.elements = {
image: $image,
content: $content
};
};
return new e();
}
};
if (typeof elems['$' + name] === 'function') {
return elems['$' + name](options);
}
};
return {
init: init,
$progressMap: maps.$progressMap,
$previewMap: maps.$previewMap
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment