MoxieManager TinyMCE Integration - javascript

I just purchased the MoxieManager plugin for Tiny MCE and got it up and running very quickly. However, I need to add in some logic to only show the images YOU uploaded.
Thus, I created a insert to take place into a database table when you upload an image. This will store the user_id, company_id and the name of the image. So I can reference whose images are whose.
Problem: I can't seem to get the API to work. I found the following which "intercepts" any uploads:
moxman.upload({
path: '/files/images',
onupload: function(args) {
console.log(args.files);
}
});
I was going to change the "path:" to my controller function that does the insert. However, I can't seem to find where to place this Javascript above. What file does it go in?
Am I on the right track? Thanks!
Source: http://www.moxiemanager.com/documentation/index.php/js_upload_onupload
"This callback enables you to intercept the uploaded files."

I expect you might have found a work around for this question, but to anyone else trying to work this out here is my solution:
tinymce.init({
//tinymce configuration
...
moxiemanager_onupload: function(args) {
//args is an Object containing File Objects
}
});
I don't think this is documented anywhere really, but I found inspiration from http://www.moxiemanager.com/documentation/index.php/js_browse

Related

Node/Javascript Redirect Not Working?

I have built a form to add some dummy data to a Mongo collection for testing purposes. The function to add the data seems to be working ok in that the data is being added ok, but, for some reason, the redirect command at the end of the function is not doing its thing. The data comes from a bog standard HTML form. Here is the code for adding the data:
router.post('/addacc',function(req,res){
var sljEdate = "";
var pljEdate = "";
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/stc';
MongoClient.connect(url,function(err,db){
if(err){
console.log("Connection Error");
}else{
console.log("connected to mongo");
var collection = db.collection('account');
var query = {accountID: req.body.acc_id,companyName: req.body.acc_name,companyEmail:req.body.acc_email,companyPhone:req.body.acc_phone,companyPostCode:req.body.acc_postcode,
photographers: {phtID:"",phtName:"",phtPhone:"",phtEmail:"",phtLoc:"",preferredContact:"",lastJob:"",pljEpoch:pljEdate,phtNotes:""},
schools: {schoolID:"",schoolName:"",schoolPhone:"",schoolEmail:"",schoolPostCode:"",lastJob:"",sljEpoch:sljEdate,notes:""}};
collection.insert(query,function(err,result){
if(err){
console.log("Error inserting Account record",err);
}else{
console.log("Data Insert Success");
res.redirect("/forms");
}
db.close();
});
}
});
});
I added lines to log to the console in a few places so that I could check progress and they all fire, including the final 'Data Insert Success' one. The res.redirect which follows it, however, does nothing. In the browser the URL remains as '/addacc', though the node console shows a subsequent GET for the '/forms' URL.
I've tried a few variations for the redirect, using e.g. res.send("test") just to try and output something, also a res.render function, but none of them seem to work.
I'm pretty sure that I have missed something simple, but I've been staring at the code for too long and I can't see anything wrong with it. In fact, I've had nearly identical code working lots of times. The only thing I'm doing differently this time is that I'm inserting into a Mongo collection which includes embedded documents, but the data insert is not the problem as the data gets in fine.
Help please!
Ok, it seems that the Node problem above was a bit of a red herring. The redirect is actually working ok (MrWillihog's comment above is duly noted for future reference anyway). I thought it must be as all the response codes on the npm console checked out fine. The problem is actually to do with the rendering of the Jade template. Specifically something to do with the the file layout.jade which I use to set out the HTML head. For some reason (yet to be determined) things work fine when calling the /forms page directly in the URL. The file forms.jade starts in the standard way with
extends layout
block content
With the opening BODY tag in layout.jade the page fails to load properly when called from the redirect: the layout.jade file renders (I put an H1 in there to check), but forms.jade doesn't.
I moved the BODY down in to forms.jade (under block.content) and it started working though, interestingly, the URL in the address bar remains as '/accadd'
Again I'm a bit stumped as I have used this pattern quite a few times with no issue. I'm guessing that I've just forgotten something simple. I'll keep digging until I find the answer, but at least now I should be looking in the right place.

Three .json files 'StockChart' - highcharts

Please help by taking StockChart. Pinning three .json files as in the example , But does not work. The code is below:
www.jsfiddle.net/d8xwjxg7/2
You have an error in your code:
$.getJSON('http://www.iklimat.pl/'+name()+'.php', function (data) {
Should be:
$.getJSON('http://www.iklimat.pl/'+ name +'.php', function (data) {
Since name is a string not a function.
Also, this will not work in a JSFiddle since you cannot load files not from the site the javascript is running on without the endpoint you are accessing setting the access-allow-control-origin header.
EDIT:
I have sorted out the issue you were having getting data, however there is an issue with that data.
http://jsfiddle.net/d8xwjxg7/5/

WP "TypeError: blacklist is undefined" password-strength-meter.js

I am creating a custom profile page for one of my WP plugins, where I want the user to have a regular page instead of seeing the WP Dashboard. All works fine so far, except the script for the password-strength. When I start typing the password, I get this in my Firebug console:
TypeError: blacklist is undefined
blacklist = [ blacklist.toString() ];
Which is thrown when the "meter" event is called at the beginning:
meter : function( password1, blacklist, password2 ) {
if ( ! $.isArray( blacklist ) )
blacklist = [ blacklist.toString() ];
I have no idea what it's working fine in the WP dashboard. I spend about an hour searching, then I decided to use a workaround. If anybody could point me into the right direction, what I am missing here, I would appreciate that.
My workaround for now is to put this into a try/catch block:
meter : function( password1, blacklist, password2 ) {
try {
if ( ! $.isArray( blacklist ) )
blacklist = [ blacklist.toString() ];
}
catch (e){}
Which works fine now, even when I use it on the WP front end. But it's not an optimal solution, as I have to copy the JS files password-strenght-meter.js to my own plugin and enqueue it from there. Since I also need to use the user-profile.js, I also have to copy this too, so I need to duplicate these two JS files which is not a good solution.
Any ideas, why jQuery is stopping the script when used on a front end page, while it works fine on the admin backend?
==== EDIT ========
I just found that the problem is related to another plugin "WP User Frontend" which seems to override the password-strenght-meter script with their own code. So I think I will be able to fix this now.
Sorry for the misleading question. I have checked that this problem was related to having another plugin "WP User Frontent" activated, that did interfere with the existing admin scripts from WordPress. So here is how I solved it.
First of all, I put this into a custom page template, so this needs to be placed in the theme directory:
<?php
/**
* Template Name: User Profile Dashboard
*/
// remove other conflicting scripts
if (class_exists('WPUF_Main'))
{
global $wpuf;
remove_action( 'wp_enqueue_scripts', array($wpuf, 'enqueue_scripts') );
}
get_header();
wp_enqueue_script('user-profile');
wp_enqueue_style('wp-admin');
include_once(ABSPATH . '/wp-admin/includes/template.php');
... other code that handles the user profile fields ...
<?php get_footer() ?>
The tricky part was to get rid of the already enqueued scripts that came from the WPUF_Main class. It seems that WP User Frontend instantiates the class into a global variable $wpuf when the plugin is active, so I had to use this variable to remove the action for enqueuing the scripts. Then I enqueued the scripts and styles for user-profile and wp-admin, and included template.php from wp-admin, because I needed the function submit_button() which is declared here.
I hope this will help others who come up with the same problem, because the combination of WP User Frontend and having a custom Profile page, does make sense, when the webmaster don't want to have their users to see the WP Dashboard at all.

javascript/jquery local app using json as my DB

i'm quite new to javascript/jQuery/Json. i'm building myself a local app ( no client side for now). right now i have a simple form (inputs and submit) and would like to get the inputs from the user with javascrip/JQuery and then build a JSON object and store it on a file. i managed to get the inputs using jQuery ,and using JSON.strigify() i have a JSON object. only thing is that i dont know how to write to a file with JS. i searched for a solution and understand that i might need to use PHP for that as JS is not meant for changing files.
here is my code:
HTML form:
<form name="portfolio" id="portfolio" method="post" onsubmit="getform()">
<p>General</p>
Portfolio Name: <input type="text" id="portfolioName" name="portfolioName"><br>
Owner First Name: <input type="text" id="ownerFName" name="ownerFName"><br>
Owner Last Name: <input type="text" id="ownerLName" name="ownerLName"><br>
<p>Risk Management</p>
%stocks : <input type="text" id="stocksPerc" name="stocksPerc"><br>
<input type="submit" value="submit">
</form>
JS code
function getform() {
var portfolioName = document.portfolio.portfolioName.value;
var ownerFname = document.portfolio.ownerFName.value;
var ownerLname = document.portfolio.ownerLName.value;
var stocksPerc = document.portfolio.stocksPerc.value;
var myJsonObject =JSON.stringify({
"general": {
"portfolioName": portfolioName,
"ownerFname": ownerFname,
"ownerLname": ownerLname
},
"riskManagement": {
"stocksPerc": stocksPerc
}
});
alert(myJsonObject);
event.preventDefault();
};
now in "myJsonObject" i have the JSON object which i would like to write to a local file.
later on i would like to read this file ,and maybe update some of the values there.
can someone please help me understand how do i write it to a file ?
you can try and load this page which runs my code. hope it works for you.
note: programming is my area of interest but i didnt study it ,i'm learning all by myself so i'm sorry if i askqdo things that make you blind for a moment :). also this is the first question i post here ,feel free to say if i need to improve.
Thanks
Sivan
update + clarification : Thanks for the answers guys ,localStorage is something i didnt know about. from what i understand about localStorage its only good for working in a single domain/location. (i encountered this question on site). what if i want the option of running the app from different locations - lets say there will be only one person updating the JSON data, no need for sync/lock and stuff like that. right now my files (JS,JSON..) are saved in dropbox ,this is how i can use the app from different locations today , i dont have any other server.
2'nd update : i tried the localStorage solution i've been offered and even though its a great capability ,its not exactly what i'm looking for since i need the JSON data available in more then one location (i'll be using my desktop and my laptop for instance).
i'd be glad if you have other suggestions.
Thanks Again.
Check out the HTML5 localStorage API. You will be able to store your JSON objects there and retrieve them. They will be stored as key-value pairs. You can't write to a file using JS AFAIK.
Don't use a file as storage, use localStorage: http://diveintohtml5.info/storage.html. If you need to save information on a session scope, you should use sessionStorage, mind though that the latter is not persistant.
An example of how you would use it:
var item = {
"general": {
"portfolioName": portfolioName,
"ownerFname": ownerFname,
"ownerLname": ownerLname
},
"riskManagement": {
"stocksPerc": stocksPerc
}
}
// set item, you should think up of a unique key for each item
localStorage.setItem('your-key', item);
// remove it if no longer needed, you don't have a lot of space
localStorage.removeItem('your-key');
Saving files is possible in some browsers but this will probably be removed in the future - so I wouldn't use it unless you must.
This article shows how to -
http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side
You can post the json to a server and use the server to generate a download file (ask a new question if thats what you seek)
and finally - are you sure you want to save to file? if all you want is to save and restore data then there are better alternatives (such as localStorage, cookies, indexDb)

Jira Gadget: Configuration isn't saved for reconfiguration

I am writing a gadget for Jira with some configuration options. One of these configuration options is a "project or filter picker".
My problem lies in the part, when I want to reconfigure the gadget's preferences. I have read the code of the timesince-gadget as an example and I think the relevant part is the following:
if (/^jql-/.test(gadget.getPref("projectOrFilterId"))){
projectAndFilterPicker =
{
userpref: "projectOrFilterId",
type: "hidden",
value: gadgets.util.unescapeString(this.getPref("projectOrFilterId"))
};
} else {
projectAndFilterPicker = AJS.gadget.fields.projectOrFilterPicker(gadget, "projectOrFilterId", args.options);
}
Basicly I've copied the code from the timesince-gadget. Unfortunately even if already configured, the javascript always enters the else part.
A problem is, that I ve no experience with jql and don't totally understand the if clause.
But usually (e.g. when calling the rest api and processing the config infos)
gadget.getPref("projectOrFilterId")
returns a string containing the id of the picked project or filter.
Question is now: How can I make my gadget remember the last configuration like it's done with some many other Jira gadgets?
I really hope anyone can help me with that.
It turnes out, the answer is even simplier then I thought.
First: In the descriptor you can totally forget the if part from above. Just
var projectAndFilterPicker = AJS.gadget.fields.projectOrFilterPicker(gadget, "projectOrFilterId", args.options);
is needed.
Second: Retrieve the project's or filter's name in your rest resource, which shouldn't be a problem, since you already want to use the processed id. Then return this name back to the view part of your javascript and type in something like
this.projectOrFilterName = args.myrestclasskey.projectOrFilterName;
And tada: reconfiguration will display the old configured name!
I had this problem once when I forgot to specify the option in the Gadget XML file. I solved it by adding this to the XML:
<UserPref name="projectOrFilterId" datatype="hidden"/>

Categories