UNPKG

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