// Generated by CoffeeScript 1.4.0 (function() { var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define(['app.lib/dssView', 'utils', 'ace-editable', 'lib/bootstrap-typeahead'], function(DssView, utils) { var EditableView; EditableView = (function(_super) { __extends(EditableView, _super); function EditableView() { this.setupImageEditable = __bind(this.setupImageEditable, this); return EditableView.__super__.constructor.apply(this, arguments); } EditableView.prototype.events = { "change input": "changed", "change textarea": "changed" }; EditableView.prototype.changeSelect = function(evt) { var changed, obj, objInst, value; changed = evt.currentTarget; if (id) { value = $(evt.currentTarget).val(); obj = "{\"" + changed.id + "\":\"" + value.replace(/\n/g, "
") + "\"}"; objInst = JSON.parse(obj); return this.model.set(objInst); } }; EditableView.prototype.changed = function(evt) { var changed, obj, objInst, value; return; changed = evt.currentTarget; if (changed.id) { value = void 0; obj = void 0; if ($(changed).is(":checkbox")) { value = $(changed).is(":checked"); obj = "{\"" + changed.id + "\":" + value + "}"; } else { value = $(changed).val(); obj = "{\"" + changed.id + "\":\"" + value.replace(/\n/g, "
") + "\"}"; } objInst = JSON.parse(obj); return this.model.set(objInst); } }; EditableView.prototype._bakeForm = function(el, lookups) { var labels, mapped, model; model = this.model; labels = void 0; mapped = void 0; $(".typeahead", el).typeahead({ source: function(query, process) { return $.get("/ajax/lookup/" + lookups + "/", { query: query }, (function(data) { labels = []; mapped = {}; $.each(data, function(i, item) { mapped[item[1]] = item; return labels.push(item[1]); }); return process(labels); }), "json"); }, updater: function(item) { this.$element.val(mapped[item][0]); model.set(this.$element.attr("id"), mapped[item][0]); return item; } }); $(".datepicker", el).datepicker({ format: "dd/mm/yyyy" }); $(".timepicker", el).timepicker(); return $("textarea.tinymce", this.el).tinymce({ script_url: "/static/js/libs/tiny_mce/tiny_mce.js", mode: "textareas", theme: "advanced", theme_advanced_toolbar_location: "top", theme_advanced_toolbar_align: "left", theme_advanced_buttons1: "fullscreen,media,tablecontrols,separator,link,unlink,anchor,separator,preview,separator,bold,italic,underline,strikethrough,separator,bullist,numlist,outdent,indent,separator,undo,redo,separator,image,cleanup,help,separator,code", theme_advanced_buttons2: "", theme_advanced_buttons3: "", auto_cleanup_word: true, plugins: "media, table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,print,contextmenu,fullscreen,preview,searchreplace", plugin_insertdate_dateFormat: "%m/%d/%Y", plugin_insertdate_timeFormat: "%H:%M:%S", extended_valid_elements: "a[name|href|target=_blank|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]", fullscreen_settings: { theme_advanced_path_location: "top", theme_advanced_buttons1: "fullscreen,media, separator,preview,separator,cut,copy,paste,separator,undo,redo,separator,search,replace,separator,code,separator,cleanup,separator,bold,italic,underline,strikethrough,separator,forecolor,backcolor,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,help", theme_advanced_buttons2: "removeformat,styleselect,formatselect,fontselect,fontsizeselect,separator,bullist,numlist,outdent,indent,separator,link,unlink,anchor", theme_advanced_buttons3: "sub,sup,separator,image,insertdate,inserttime,separator,tablecontrols,separator,hr,advhr,visualaid,separator,charmap,emotions,iespell,flash,separator,print" } }); }; EditableView.prototype._saveChanges = function() { var args, error, _results; args = arguments; if (!this.model.isValid()) { if (this.model.errors) { _results = []; for (error in this.model.errors) { $("#group-" + error, this.el).addClass("error"); _results.push($("#error-" + error, this.el).text(this.model.errors[error])); } return _results; } } else { return this.model.save(null, { patch: args[0].patch, success: args[0].success, error: args[0].error }); } }; EditableView.prototype.setupImageEditable = function(options) { var _this = this; $.fn.editable.defaults.mode = 'inline'; try { if (/msie\s*(8|7|6)/.test(navigator.userAgent.toLowerCase())) { Image.prototype.appendChild = function(el) { return true; }; } return options.el.editable({ type: "image", name: options.el.attr('id'), value: null, showbuttons: options.showbuttons === void 0 ? true : options.showbuttons, image: { btn_choose: options.chooseMessage ? options.chooseMessage : "Change Avatar", droppable: true, name: options.el.attr('id'), max_size: 2621440, on_error: function(code) { if (code === 1) { return utils.showError("File is not an image!", "Please choose a jpg|gif|png image!"); } else if (code === 2) { return utils.showError("File too big!", "Image size should not exceed 2.5Mb!"); } else { } } }, url: function(params) { return _this.uploadImage({ el: options.el, url: options.url, success: function(data) { console.log("Image updated: " + data.url); options.el.attr("src", data.url); return utils.showMessage("Avatar succesfully updated"); } }); } }); } catch (e) { return console.log(e); } }; EditableView.prototype.uploadImage = function(options) { var $form, deferred, fd, file_input, files, iframe_id; $form = options.el.next().find(".editableform:eq(0)"); file_input = $form.find("input[type=file]:eq(0)"); if (!("FormData" in window)) { deferred = new $.Deferred; iframe_id = "temporary-iframe-" + (new Date()).getTime() + "-" + (parseInt(Math.random() * 1000)); $form.after(""); $form.append(""); $form.next().data("deferrer", deferred); $form.attr({ method: "POST", enctype: "multipart/form-data", target: iframe_id, action: options.url }); $form.get(0).submit(); setTimeout((function() { var iframe; iframe = document.getElementById(iframe_id); if (iframe != null) { iframe.src = "about:blank"; $(iframe).remove(); return deferred.reject({ status: "fail", message: "Timeout!" }); } }), 60000); } else { fd = null; try { fd = new FormData($form.get(0)); } catch (e) { fd = new FormData(); $.each($form.serializeArray(), function(index, item) { return fd.append(item.name, item.value); }); $form.find("input[type=file]").each(function() { if (this.files.length > 0) { return fd.append(this.getAttribute("name"), this.files[0]); } }); } if (file_input.data("ace_input_method") === "drop") { files = file_input.data("ace_input_files"); if (files && files.length > 0) { fd.append(file_input.attr("name"), files[0]); } } deferred = $.ajax({ url: options.url, type: "POST", processData: false, contentType: false, dataType: "json", data: fd, xhr: function() { var req; req = $.ajaxSettings.xhr(); return req; }, beforeSend: function() { return { success: function() {} }; } }); } deferred.done(function(res) { if (res.status === "OK") { return options.success(res); } else { return utils.showError(res.message); } }).fail(function(res) { return utils.showError("Failure"); }); return deferred.promise(); }; return EditableView; })(DssView); return EditableView; }); }).call(this);