UNPKG

9.52 kBJavaScriptView Raw
1/*
2 This file 'index' is part of Firebird Integrated Solution 1.0
3
4 Copyright (c) 2019 Lincong
5
6 Contact:
7 Email: lincong1987@gmail.com
8
9 QQ: 159257119
10
11 See Usage at http://www.jplatformx.com/firebird
12
13 Create date: 2019-03-06 16:20
14 */
15
16//require("es5-polyfill")
17
18let Yox = require("./lib/yox");
19let jQuery = require("jquery");
20let _ = require("lodash");
21let Store = require("./lib/store");
22let Router = require("./lib/yox-router");
23
24let shortid = require("shortid");
25
26let dayjs = require("./lib/dayjs").dayjs;
27let pkg = require("../package.json");
28
29Yox.uuid = shortid;
30
31Yox.prototype.getSlot = function (name) {
32 return this.get(`$slot_${name}`);
33};
34
35Yox.prototype.hasSlot = function (name) {
36 return typeof this.getSlot(name) !== "undefined";
37};
38
39Yox.prototype.$store = function (name) {
40 return typeof this.getSlot(name) !== "undefined";
41};
42
43
44Yox.prototype.$http = function (options) {
45 return jQuery.ajax(options);
46};
47//
48// Yox.prototype.$http.get = function (url, data, callback, type) {
49//
50// return jQuery.ajax(jQuery.extend({
51// url: url,
52// type: method,
53// dataType: type,
54// data: data,
55// success: callback
56// }, jQuery.isPlainObject(url) && url));
57//
58// return jQuery.get(options);
59// };
60//
61// Yox.prototype.$http.post = function (url, data, callback, type) {
62//
63// return jQuery.ajax(jQuery.extend({
64// url: url,
65// type: method,
66// dataType: type,
67// data: data,
68// success: callback
69// }, jQuery.isPlainObject(url) && url));
70//
71// return jQuery.get(options);
72// };
73//
74// Yox.prototype.$http.payload = function (url, data, callback, type) {
75//
76// return jQuery.ajax(jQuery.extend({
77// url: url,
78// type: method,
79// dataType: type,
80// data: data,
81// success: callback
82// }, jQuery.isPlainObject(url) && url));
83//
84// return jQuery.get(options);
85// };
86//
87// Yox.prototype.$http.interceptors = [
88// function (request, next) {
89// //...
90// //请求发送前的处理逻辑
91// //...
92// next(function (response) {
93// //...
94// //请求发送后的处理逻辑
95// //...
96// //根据请求的状态,response参数会返回给successCallback或errorCallback
97// return response
98// });
99// }
100// ];
101//
102// Yox.prototype.$http.addInterceptor = function () {
103//
104// };
105//
106// Yox.prototype.$dialog = function (name) {
107//
108// };
109
110let FireBird = (function () {
111 function FireBird(options) {
112 return new Yox(options);
113 }
114
115 return FireBird;
116})();
117
118FireBird.findComponentUpward = function (parent, componentName) {
119 if (typeof componentName === 'string') {
120 componentName = [componentName]
121 } else {
122 componentName = componentName
123 }
124
125 while (parent) {
126 let {name} = parent.$options
127 if (name && _.indexOf(componentName, name) >= 0) {
128 break
129 } else {
130 parent = parent.$parent
131 }
132 }
133
134 return parent
135};
136
137FireBird.prototype = Yox.prototype;
138
139_.each(Yox, (func, name) => {
140 FireBird[name] = func;
141});
142
143
144FireBird.log = (msg) => {
145 console.log(`[FireBird info]: ${msg}`);
146};
147FireBird.warn = (msg) => {
148 console.warn(`[FireBird warn]: ${msg}`);
149};
150FireBird.error = (msg) => {
151 console.error(`[FireBird error]: ${msg}`);
152};
153
154
155FireBird.components = {};
156
157FireBird.classes = {};
158
159FireBird.namespace = function () {
160 var a = arguments, o, i = 0, j, d, arg;
161 for (; i < a.length; i++) {
162 o = this; //Reset base object per argument or it will get reused from the last
163 arg = a[i];
164 if (arg.indexOf(".") > -1) { //Skip this if no "." is present
165 d = arg.split(".");
166 for (j = (d[0] == 'FireBird') ? 1 : 0; j < d.length; j++) {
167 o[d[j]] = o[d[j]] || {};
168 o = o[d[j]];
169 }
170 } else {
171 o[arg] = o[arg] || {};
172 o = o[arg]; //Reset base object to the new object so it's returned
173 }
174 }
175 return o;
176};
177
178FireBird.create = (name, options) => {
179 return new FireBird($.extend(true, {}, FireBird.components[name], options));
180};
181
182FireBird.addMember = function (name, member) {
183 Yox.prototype[name] = member;
184 return this;
185};
186
187
188FireBird.component = (name, options) => {
189 if (typeof name === "string") {
190 if (typeof options === "function") {
191
192 } else {
193 options.$$name = name;
194 if (options.extend) {
195
196 if (typeof options.extend === "string") {
197 options.extend = FireBird.components[options.extend] || null;
198 }
199
200 options = $.extend(true, {}, options.extend, options, {
201 "$$parent": options.extend.$$name
202 });
203 }
204 }
205
206 Yox.component(name, options);
207 FireBird.log(`组件 ${name} 已注册`);
208 FireBird.components[name] = options;
209 //FireBird.defaults[name] = options;
210 FireBird.namespace(name);
211 return options;
212 }
213 if (typeof name === "object") {
214 _.each(name, (options, comp) => {
215 FireBird.component(comp, options);
216 });
217 }
218};
219
220FireBird.partials = {};
221
222FireBird.partial = (name, template) => {
223 if (typeof name === "string") {
224 Yox.partial(name, template);
225 FireBird.log(`模版 ${name} 已注册`);
226 FireBird.partials[name] = template;
227 return template;
228 }
229 if (typeof name === "object") {
230 _.each(name, (template, partial) => {
231 FireBird.partial(partial, template);
232 });
233 }
234};
235
236/**
237 * 组件 class 处理器
238 * @param cssObject
239 * @returns {string}
240 *
241 * @example
242 *
243 * FireBird.css({text: true, valid: true, invalid: false});
244 *
245 * // class="text valid"
246 */
247FireBird.css = (cssObject = {}) => {
248 let classes = [];
249 _.each(cssObject, (value, cssName) => {
250 if (value === true) {
251 classes.push(cssName);
252 }
253 });
254 return classes.join(" ");
255};
256
257
258FireBird.style = (styleObject = {}) => {
259 let style = [];
260
261 _.each(styleObject, (v, k) => {
262 switch (k) {
263 case "width":
264 case "height":
265 case "min-height":
266 case "min-width":
267 case "top":
268 case "left":
269 case "bottom":
270 case "right":
271 case "line-height":
272 if (_.isNumber(v)) {
273 style.push(`${k}: ${v}px`);
274 } else {
275 style.push(`${k}: ${v}`);
276 }
277 break;
278 default:
279 style.push(`${k}: ${v}`);
280 }
281
282 });
283
284 return style.join("; ");
285};
286
287FireBird.oneOf = (value, defaults, array = []) => {
288 if (!FireBird.array.has(array, value)) {
289 return defaults;
290 }
291 return value;
292};
293
294FireBird.filter("css", FireBird.css);
295
296FireBird.filter("style", FireBird.style);
297
298FireBird.filter("oneOf", FireBird.oneOf);
299
300FireBird.compile = Yox.compile;
301
302FireBird.filter("_defaults", function (val, def) {
303 return typeof val === "undefined" ? (typeof def === "undefined" ? "" : def) : val;
304});
305
306FireBird.filter("Function", function (val, def) {
307 return (new Function(
308 "//this data from FireBird Filter 'Function' Wrapper; \n return " + (typeof val === "undefined" ? (typeof def === "undefined" ? "" : def) : val)))();
309});
310
311FireBird.filter("JSON_parse", function (val, def) {
312 return JSON.parse(typeof val === "undefined" ? (typeof def === "undefined" ? "" : def) : val);
313});
314
315FireBird.filter("JSON_stringify", function (val, def, replacer, space) {
316 return JSON.stringify(typeof val === "undefined" ? (typeof def === "undefined" ? "" : def) : val, replacer, space);
317});
318
319FireBird.filter("dayjs_fromNow", function (withoutSuffix) {
320 return dayjs.fromNow(withoutSuffix);
321});
322FireBird.filter("dayjs_from", function (val, withoutSuffix) {
323 return dayjs.from(val, withoutSuffix);
324});
325FireBird.filter("dayjs_toNow", function (withoutSuffix) {
326 return dayjs.toNow(withoutSuffix);
327});
328FireBird.filter("dayjs_to", function (val, withoutSuffix) {
329 return dayjs.to(val, withoutSuffix);
330});
331
332FireBird.directive = Yox.directive;
333
334// 将 lodash 注入
335FireBird.filter(_);
336
337Store.install(FireBird);
338Router.install(FireBird);
339
340FireBird.component("PageApp", {
341 template: "<div></div>",
342 data() {
343 return {};
344 }
345});
346
347FireBird.App = function () {
348 console.log("FireBird.App")
349};
350
351global.FireBird = FireBird;
352
353let version = pkg.version;
354console.log(" _______________________________________________________________________");
355console.log("| |");
356console.log("| ######## #### ######## ######## ######## #### ######## ######## |");
357console.log("| ## ## ## ## ## ## ## ## ## ## ## ## |");
358console.log("| ## ## ## ## ## ## ## ## ## ## ## ## |");
359console.log("| ###### ## ######## ###### ######## ## ######## ## ## |");
360console.log("| ## ## ## ## ## ## ## ## ## ## ## ## |");
361console.log("| ## ## ## ## ## ## ## ## ## ## ## ## |");
362console.log("| ## #### ## ## ######## ######## #### ## ## ######## |");
363console.log("| |");
364console.log(`|${_.pad("", 71)}|`);
365console.log(`|${_.pad("Version: " + version, 71)}|`);
366console.log(`|${_.pad("Date: {{build-date}}", 71)}|`);
367console.log(`|${_.pad("Author: lincong1987@gmail.com", 71)}|`);
368//console.log("| |");
369console.log("|_______________________________________________________________________|");
370
371
372// return {
373// FireBird: FireBird,
374// jQuery: jQuery,
375// $: $,
376// Router: Router,
377// Store: Store,
378// dayjs: dayjs,
379// version: version,
380// _: _,
381// lodash: _
382// };
383
384// exports.FireBird = FireBird;
385// exports.Store = Store;
386// exports.Router = Router;
387// exports.jQuery = jQuery;
388// exports.$ = jQuery;
389// exports._ = _;
390// exports.dayjs = dayjs;
391// exports.version = version;
392
393// export default {
394// FireBird, Store, Router, jQuery, $, _, dayjs, version
395// }
396
397let $ = jQuery, lodash = _;
398
399module.exports = {
400 FireBird,
401 Store: Store.Store,
402 Router,
403 jQuery,
404 $,
405 _,
406 lodash,
407 dayjs,
408 uuid: shortid,
409 version
410};