refactor(dom) Images elements: 2 -> 1

This commit is contained in:
coderaiser 2014-04-18 09:27:17 -04:00
parent 24269434e1
commit c7524ea2b2
2 changed files with 40 additions and 57 deletions

View file

@ -87,9 +87,6 @@ body {
}
.error::before {
position : relative;
right : 15px;
bottom : 1px;
font-family : 'Fontello';
font-size : 14px;
color : rgb(222, 41, 41);

View file

@ -14,18 +14,32 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
ImagesProto = function() {
var ImagesProto = function() {
var Classes = 'icon',
ID = 'js-image-',
Images = {
'image-loading' : null,
'image-error' : null
};
function getImage(name, classes) {
var element = DOM.anyload({
var LOADING = 'loading',
LoadingImage,
HIDDEN = 'hidden',
ERROR = 'error';
function init() {
if (!LoadingImage) {
LoadingImage = LOADING;
if (DOM.isSVG())
LoadingImage += '-svg';
else
LoadingImage += '-gif';
}
}
function getElement() {
var element;
init();
element = DOM.anyload({
name : 'span',
className : classes,
id : ID + name,
id : 'js-status-image',
className : 'icon',
attr : 'data-progress',
not_append : true
});
@ -35,43 +49,25 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
/* Функция создаёт картинку загрузки */
this.loading = function() {
var classes, isSVG,
name = 'loading',
id = ID + name,
element = Images[id],
parent = element && element.parentElement;
var element = getElement();
if (!element || !parent) {
classes = Classes + ' ' + name;
isSVG = DOM.isSVG();
if (isSVG)
classes += ' ' + name + '-svg';
else
classes += ' ' + name + '-gif';
Images[id] =
element = getImage(name, classes);
}
DOM.addClass(element, LOADING)
.addClass(element, LoadingImage)
.removeClass(element, ERROR)
.removeClass(element, HIDDEN);
return element;
};
/* Функция создаёт картинку ошибки загрузки */
this.error = function() {
var classes,
name = 'error',
id = ID + name,
element = Images[id],
parent = element && element.parentElement;
if (!element || !parent) {
classes = Classes + ' ' + name;
Images[id] =
element = getImage(name, classes);
}
var element = getElement();
DOM.addClass(element, ERROR)
.removeClass(element, LOADING)
.removeClass(element, LoadingImage)
.removeClass(element, HIDDEN);
return element;
};
},
@ -83,11 +79,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
this.showLoad = function(position) {
var top = position && position.top,
current,
loadingImage = Images.loading(),
errorImage = Images.error(),
parent = loadingImage.parentElement;
DOM.hide(errorImage);
image = Images.loading(),
parent = image.parentElement;
if (top)
current = DOM.getRefreshButton().parentElement;
@ -97,11 +90,11 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
}
if (!parent || (parent && parent !== current))
current.appendChild(loadingImage);
current.appendChild(image);
DOM.show(loadingImage);
DOM.show(image);
return loadingImage;
return image;
};
/**
@ -116,7 +109,6 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
*/
this.showError = function(jqXHR) {
var func,
lLoadingImage = Images.loading(),
lErrorImage = Images.error(),
lResponse = '',
lStatusText = '',
@ -147,12 +139,6 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
DOM.show(lErrorImage);
lErrorImage.title = lText;
var lParent = lLoadingImage.parentElement;
if (lParent)
lParent.appendChild(lErrorImage);
DOM.hide(lLoadingImage);
if (lText) {
Util.log(lText);
func = Dialog.alert.bind(null, lText);