h5-cli
Version:
hello
154 lines (140 loc) • 6.54 kB
JavaScript
"use strict";
//define(function (require, exports, module) {
define("mods/condition/index", function (require, exports, module) {
var VModule = require("component/VModule");
var page = VModule.render({
init: function init(isEdit, content, photos) {
var isSupperUpload = this.util.isInYuantuApp();
this.isInit = 1;
if (isEdit) {
this.conditionCache = this.cache.getCacheModule("yuantu-condition");
this.state = {
loading: false,
success: true,
isEdit: isEdit,
maxPhotosNumber: 9,
isSupperUpload: isSupperUpload,
photos: this.conditionCache.get("photos").value || [],
content: this.conditionCache.get("content").value || []
};
} else {
this.state = {
loading: false,
success: true,
isEdit: isEdit,
isSupperUpload: isSupperUpload,
photos: photos || [],
maxPhotosNumber: 9,
content: content || ""
};
}
this.module = this.initModule(this.state, '#J_ConditionModule');
this.photoList = $('#J_ConditionModule');
this.regEvent();
},
regEvent: function regEvent() {
var util = this.util;
var self = this;
this.photoList.delegate(".J_AddPhotoBtn", "click", function () {
// self.addPhoto("http://image.yuantutech.com/user/0b0e7a27918afa6b74d649eea58d7495-612-612.jpg")
util.brige("callPhotoUpload", {}, function (result) {
if (result && result.ret == "SUCCESS") {
try {
var photoResult = JSON.parse(result.data);
if (photoResult.success && photoResult.data && photoResult.data.name) {
self.addPhoto(self.buildPhotoUrl(photoResult.data));
} else {
util.alert("\u56fe\u7247\u4e0a\u4f20\u5931\u8d25\uff0c\u8bf7\u91cd\u8bd5");
}
} catch (e) {
util.alert("\u8fd4\u56de\u6570\u636e\u9519\u8bef");
}
} else {
util.alert("\u56fe\u7247\u4e0a\u4f20\u5931\u8d25");
}
}, function (data) {
util.alert(data.msg || "\u8c03\u7528\u56fe\u7247\u4e0a\u4f20\u7ec4\u4ef6\u5931\u8d25");
});
});
this.photoList.delegate("textarea", "input", function () {
self.conditionCache.set("content", $(this).val());
self.setState({ content: $(this).val() });
// alert($(this).val())
});
this.photoList.delegate(".del", "click", function (e) {
var url = $(this).data("url");
if (url) {
self.removePhoto(url);
}
});
//\u9884\u89c8\u5927\u56fe
this.photoList.delegate(".J_PhotoItem", "click", function (e) {
// console.log(e.target.classList)
//\u963b\u6b62 .del \u5192\u6ce1\u4e0a\u6765
if ($(e.target).hasClass('J_PhotoItem')) {
// if(e.target.classList && e.target.classList.value.indexOf("J_PhotoItem") != -1){
var url = $(this).data("url");
self.util.brige("imageBrowser", {
current: url,
urls: self.getPhotos().split(",")
}, function () {}, function () {
self.util.alert("\u65e0\u6cd5\u67e5\u770b\u5927\u56fe\uff0c\u8bf7\u5347\u7ea7\u5230\u6700\u65b0\u5ba2\u6237\u7aef");
});
}
});
},
getSmallPhotoUrl: function getSmallPhotoUrl(url) {
var suffix = url.match(/\.(\w+)$/);
suffix = suffix ? suffix[0] : "";
if (suffix) {
suffix = "_120x120" + suffix;
}
return url.replace("image.yuantutech.com", "112.124.118.39/image") + suffix;
},
buildPhotoUrl: function buildPhotoUrl(data) {
return "http://image.yuantutech.com/" + data.path + data.name;
},
getContent: function getContent() {
return this.isInit ? this.state.content : "";
},
getPhotos: function getPhotos() {
return this.isInit ? this.state.photos.join() : "";
},
removePhoto: function removePhoto(url) {
var index = this.state.photos.indexOf(url);
if (index != -1) {
this.state.photos.splice(index, 1);
this.conditionCache.set("photos", this.state.photos);
this.setState(this.state);
}
},
addPhoto: function addPhoto(url) {
this.state.photos.push(url);
this.setState(this.state);
this.conditionCache.set("photos", this.state.photos);
},
renderPhotoList: function renderPhotoList(state) {
var _this = this;
var photos = state.photos;
var isEdit = state.isEdit;
if (photos.length == 0 && !isEdit) {
return "<div class=\"tip\">\u6CA1\u6709\u56FE\u7247\u63CF\u8FF0</div>";
} else {
return photos.map(function (url) {
return "<div class=\"photo J_PhotoItem\" data-url=\"" + url + "\" style=\"background-image:url(" + _this.getSmallPhotoUrl(url) + ")\">\n\t\t\t\t\t\t\t" + _this.util.is(isEdit, "<span class=\"del\" data-url=\"" + url + "\"></span>") + "\n\t\t\t\t\t\t</div>";
}).join("");
}
},
renderNoSupper: function renderNoSupper() {
return "<div class=\"ui-tips center\">\n \u4E0B\u8F7D<a href=\"http://nocache-s.yuantutech.com/tms/fb/app-download.html?target=_blank\" target=\"_blank\">\u6167\u533B\u5BA2\u6237\u7AEF</a>\uFF0C\u53EF\u6DFB\u52A0/\u67E5\u770B\u75C5\u60C5\u56FE\u7247\u63CF\u8FF0\n </div>\n ";
},
render: function render(state) {
return "\n\t\t\t\t<div class=\"page condition-module\">\n\t\t\t <div class=\"ui-form ui-border-tb\">\n\t\t\t <div class=\"ui-form-item ui-form-item-textarea ui-form-item-show ui-border-b\">\n\t\t\t <label>\u75C5\u60C5\u63CF\u8FF0\uFF1A</label>\n\t\t\t " + (state.isEdit ? "<textarea placeholder=\"\u8BF7\u8BE6\u7EC6\u63CF\u8FF0\u60A8\u7684\u75C5\u60C5\uFF0C\u75C7\u72B6\uFF0C\u6CBB\u7597\u7ECF\u8FC7\u4EE5\u53CA\u60F3\u8981\u83B7\u5F97\u7684\u5E2E\u52A9\" maxlength=\"300\" class=\"edit\">" + state.content + "</textarea>" : "<div class=\"condition-text\">" + (state.content || "<span class=\"tip\">\u6CA1\u6709\u75C5\u60C5\u63CF\u8FF0</span>") + "</div>") + "\n\t\t\t </div>\n\t\t\t </div>\n\t\t\t <div class=\"ui-form ui-border-tb\">\n\t\t\t <div class=\"ui-form-item ui-form-photo ui-form-item-show ui-border-b\">\n\t\t\t <label>\u56FE\u7247\u63CF\u8FF0\uFF1A</label>\n\t\t\t <div class=\"photo-small-list\">\n\t\t\t \t" + (state.isSupperUpload ? this.renderPhotoList(state) : this.renderNoSupper()) + "\n\t\t\t \t" + (state.isEdit && state.isSupperUpload && state.photos.length < state.maxPhotosNumber ? "<div class=\"photo-add J_AddPhotoBtn\"></div>" : "") + "\n\t\t\t </div>\n\t\t\t </div>\n\t\t\t </div>\n\t\t\t </div>\n\t\t\t";
},
renderError: function renderError() {
return "";
}
});
// page.init();
module.exports = page;
});