var EditMedia = function(core, target, data, returnCB, targetId){ var events = new EventManager, template = new Template, data = null, element = null, elements = null, mediaLoc = !targetId ? "media/byUser/" : "appMedia/byId/", reqId = !targetId ? core.userID : targetId; this.exit = exit; new ApiManager({ 'media' : { 'source' : mediaLoc + 'all' } }, reqId, init); function Data(d){ this.selectedImg = new Media; this.media = getMedia(d.media); this.hasMedia = !!this.media && this.media.length > 0; } function Media(id, l, w, h, d){ this.id = !!id ? id : null; this.loc = !!l ? l : ""; this.width = !!w ? w : 0; this.height = !!h ? h : 0; this.data = !!d ? d : ""; } Media.prototype.remove = function(e){ preventDefault(e); if(!this.id){ core.notifications.setError("Error attempting to delete image, I'm sorry!") return; } apiRequest('DELETE', !targetId ? 'media/byId/image' : 'appMedia/byId/image', this.id, null, deleteCb); }; function Template(){ var x = 'X', rem = 'Remove', anchr = '' + x + rem + '', mdia = '{{# media }}' + anchr + '{{/ media }}', sel = 'Select Pic', up = 'Upload Pic', mCntr = '
' + sel + up + '
'; var h = '

Edit Media

', a = '

Add Media:

' + mCntr + '
', m = '
' + mdia + '
'; // If jewish { // return errors.New("Not kosher"); // } this.index = '
' + h + a + m + '
'; this.media = m; } function init(err, d){ if(!!err){ core.notifications.setError("I'm sorry, but there was an error loading this page!") return; } data = new Data(d); element = getElementFromString(render(template.index, data)); elements = new Elements(element, { 'select' : { 'target' : new ImageUpload(element, selectUpdate) }, 'selectBtn' : { 'selector' : '.selectBtn' }, 'uploadImg' : { 'selector' : '.uploadImg' }, 'uploadBtn' : { 'selector' : '.uploadBtn' }, 'viewMedia' : { 'selector' : '.viewMedia' }, }); bindViewMedia(); events.add(elements.selectBtn, 'click', elements.select.open); events.add(elements.uploadBtn, 'click', uploadAction); returnCB(); target.appendChild(element); } function getMedia(d){ if(!d) return []; var t = d.length, c = t, a = []; while(c){ var m = d[t-c--] a.push(new Media(m.id, m.loc, m.width, m.height, null)); } return a; } function isValidImg(img){ return img.data.length > 0 && !!img.width && !!img.height && img.width <= 1024 && img.height <= 1024; } function setSelect(e){ elements.uploadImg.src = data.selectedImg.data = e.data; data.selectedImg.width = e.width; data.selectedImg.height = e.height; elements.uploadBtn.classList.remove('noDisplay'); } function unsetSelect(){ elements.uploadImg.src = data.selectedImg.data = ""; data.selectedImg.width = 0; data.selectedImg.height = 0; elements.uploadBtn.classList.add('noDisplay'); } function selectUpdate(e){ if(isValidImg(e)){ setSelect(e); } else { core.notifications.setError("Sorry, but your selected image is too large!") unsetSelect(); } } function uploadAction(e){ preventDefault(e); if(!isValidImg(data.selectedImg)) return null; apiRequest('POST', mediaLoc + 'image', reqId, [data.selectedImg], uploadCb); unsetSelect(); } function uploadCb(e,s){ if(s !== 200 || !e[0].success){ var msg = !!e[0] && !!e[0].error ? e[0].error : "There was an error while uploading, I'm sorry!" core.notifications.setError(msg); return; } data.media.push(new Media(e[0].id, e[0].loc, 0, 0, null)); refreshViewMedia(); } function deleteCb(e,s){ if(s !== 200){ core.notifications.setError("There was an error while deleting, I'm sorry!"); return; } var m = data.media, t = m.length, c = t; while(c){ var i = t-c--; if(m[i].id === e.id){ m.splice(i, 1); refreshViewMedia(); break; } } } function refreshViewMedia(){ events.removeByTag('viewMedia'); removeChild(element, elements.viewMedia); data.hasMedia = !!data.media && data.media.length > 0; elements.viewMedia = getElementFromString(render(template.media, data)); element.appendChild(elements.viewMedia); bindViewMedia(); } function bindViewMedia(){ var tgs = element.querySelectorAll('media'), t = tgs.length, c = t; while(c){ var i = t-c--; events.add(tgs[i].querySelector("a"), "click", function(i){ return function(){ data.media[i].remove(); } }(i), "viewMedia") } } function exit(){ if(events) events.reset(); if(element) removeChild(target, element); if(elements) elements.exit(); housekeeping(); } function housekeeping(){ events = template = data = element = elements = null; } };