This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/adapters/rest.js.es6
Sam a3e8c3cd7b FEATURE: Native theme support
This feature introduces the concept of themes. Themes are an evolution
of site customizations.

Themes introduce two very big conceptual changes:

- A theme may include other "child themes", children can include grand
children and so on.

- A theme may specify a color scheme

The change does away with the idea of "enabled" color schemes.

It also adds a bunch of big niceties like

- You can source a theme from a git repo

- History for themes is much improved

- You can only have a single enabled theme. Themes can be selected by
    users, if you opt for it.

On a technical level this change comes with a whole bunch of goodies

- All CSS is now compiled using a custom pipeline that uses libsass
    see /lib/stylesheet

- There is a single pipeline for css compilation (in the past we used
    one for customizations and another one for the rest of the app

- The stylesheet pipeline is now divorced of sprockets, there is no
   reliance on sprockets for CSS bundling

- CSS is generated with source maps everywhere (including themes) this
    makes debugging much easier

- Our "live reloader" is smarter and avoid a flash of unstyled content
   we run a file watcher in "puma" in dev so you no longer need to run
   rake autospec to watch for CSS changes
2017-04-12 10:53:49 -04:00

118 lines
3.1 KiB
JavaScript

import { ajax } from 'discourse/lib/ajax';
import { hashString } from 'discourse/lib/hash';
const ADMIN_MODELS = ['plugin', 'theme', 'embeddable-host', 'web-hook', 'web-hook-event'];
export function Result(payload, responseJson) {
this.payload = payload;
this.responseJson = responseJson;
this.target = null;
}
// We use this to make sure 404s are caught
function rethrow(error) {
if (error.status === 404) {
throw "404: " + error.responseText;
}
throw(error);
}
export default Ember.Object.extend({
storageKey(type, findArgs, options) {
if (options && options.cacheKey) {
return options.cacheKey;
}
const hashedArgs = Math.abs(hashString(JSON.stringify(findArgs)));
return `${type}_${hashedArgs}`;
},
basePath(store, type) {
if (ADMIN_MODELS.indexOf(type.replace('_', '-')) !== -1) { return "/admin/"; }
return "/";
},
appendQueryParams(path, findArgs) {
if (findArgs) {
if (typeof findArgs === "object") {
const queryString = Object.keys(findArgs)
.reject(k => !findArgs[k])
.map(k => k + "=" + encodeURIComponent(findArgs[k]));
if (queryString.length) {
return path + "?" + queryString.join('&');
}
} else {
// It's serializable as a string if not an object
return path + "/" + findArgs;
}
}
return path;
},
pathFor(store, type, findArgs) {
let path = this.basePath(store, type, findArgs) + Ember.String.underscore(store.pluralize(type));
return this.appendQueryParams(path, findArgs);
},
findAll(store, type, findArgs) {
return ajax(this.pathFor(store, type, findArgs)).catch(rethrow);
},
find(store, type, findArgs) {
return ajax(this.pathFor(store, type, findArgs)).catch(rethrow);
},
findStale(store, type, findArgs, options) {
if (this.cached) {
return this.cached[this.storageKey(type, findArgs, options)];
}
},
cacheFind(store, type, findArgs, opts, hydrated) {
this.cached = this.cached || {};
this.cached[this.storageKey(type,findArgs,opts)] = hydrated;
},
jsonMode: false,
getPayload(method, data) {
let payload = {method, data};
if (this.jsonMode) {
payload.contentType = "application/json";
payload.data = JSON.stringify(data);
}
return payload;
},
update(store, type, id, attrs) {
const data = {};
const typeField = Ember.String.underscore(type);
data[typeField] = attrs;
return ajax(this.pathFor(store, type, id), this.getPayload('PUT', data))
.then(function(json) {
return new Result(json[typeField], json);
});
},
createRecord(store, type, attrs) {
const data = {};
const typeField = Ember.String.underscore(type);
data[typeField] = attrs;
return ajax(this.pathFor(store, type), this.getPayload('POST', data))
.then(function (json) {
return new Result(json[typeField], json);
});
},
destroyRecord(store, type, record) {
return ajax(this.pathFor(store, type, record.get('id')), { method: 'DELETE' });
}
});